Yes, definitely use a function. There are performance benefits to using functions in that Julia can infer types better when things are inside a function. Defining a function and then using it in right there is costless, and makes it easier to write code.
Looking at this code, I’m more convinced you want to be using missing
instead of NaN
. missing
is designed to mean “unknown value”, whereas NaN
is something different.
Yes, findfirst
is better than occursin
because it returns an element of the array. (findfirst
returns an integer index, but occursin
returns an array of Bool
s.
Here is another way of writing the above
julia> SP = DataFrame(
Item = ["a", "a", "b", "b"],
Name = ["searchLow", "searchUpp", "KPI_1", "KPI_2"],
Vel_B1_FTF = [NaN, 0.1, 0.4, NaN])
4×3 DataFrame
Row │ Item Name Vel_B1_FTF
│ String String Float64
─────┼───────────────────────────────
1 │ a searchLow NaN
2 │ a searchUpp 0.1
3 │ b KPI_1 0.4
4 │ b KPI_2 NaN
julia> KPI_2_exists = let
ind = findfirst(==("KPI_2"), SP.Name)
!isnan(SP[!,par][ind])
end;
julia> Nsta = KPI_2_exists ? 3 : 2
2