Julia equivalent of R's quantile function

As far as I can tell your function is not being passed dataframes at all, but just vectors. You are referencing what looks like a dataframe in the body of your function (history[OUT_GR == type, ACT_OUT_MINUTES]), just be aware that this type of reference to global variables inside functions is bound to make your code quite slow, so you should be passing it as an argument to your function.

Here’s one way of writing your function:

function func_get_percentile_value(df, type, mode, origin, destination, percentile)
    if !(percentile isa Number) || percentile<0 || percentile>100
        return missing
    end
    
    prob = percentile/100
            
    if origin == '*' && destination == '*' # What happens if this is not the case?
        if mode == "TO"
            return quantile(df[df.OUT_GR .== type, :ACT_OUT_MINUTES], prob)
        elseif mode == "TI"
            return quantile(df[df.IN_GR .== type, :ACT_TIN_MINUTES], prob)
        else
            error("mode has to be either TO or TI")
        end
    end
end

and if I understand your code correctly the loop can be replaced by a simple broadcasted invocation of that function, the second line in the loop (which generates the TO_MAX column) would be

percentile_to.dt.TO_MAX = func_get_percentile_value.("ABC", "TO", 
        percentile_to.dt.arrival, percentile_to.dt.destination, percentile_to.dt.TO_Percentile_Threshold)
1 Like