Apply some functions to columns of a dataframe

Hello,
I’m a beginner in Julia.
Here is my problem; i have:

  1. a dataframe with 4 columns and 10 rows
  2. 3 functions u1, u2, u3
  3. a vector of 3 weights

And i want to create a function (arguments : the dataframe, the 3 functions and the weights ) this function must return a vector built as follows

  • we apply the function u1 on values of column 2, the function u2 on values of column 3, the function u3 on values of column 4
  • then we do, for each row, a weighted sum with vector w
  • so, we have at the end a vector with 10 values

I don’t know how to do.
Thanks for your help.

Have you worked through any tutorials on DataFrames.jl or DataFramesMeta.jl.

This is very easy to do with those tools.

julia> using DataFrames, DataFramesMeta

julia> df = DataFrame(u1 = rand(10), u2 = rand(10), u3 = rand(10))
10Γ—3 DataFrame
 Row β”‚ u1          u2        u3       
     β”‚ Float64     Float64   Float64  
─────┼────────────────────────────────
   1 β”‚ 0.226441    0.46347   0.803374
   2 β”‚ 0.698363    0.367743  0.452573
   3 β”‚ 0.768411    0.168     0.6769
   4 β”‚ 0.0897352   0.959212  0.551368
   5 β”‚ 0.952901    0.407275  0.125132
   6 β”‚ 0.951374    0.327541  0.100814
   7 β”‚ 0.00756429  0.377843  0.60469
   8 β”‚ 0.274698    0.1681    0.874693
   9 β”‚ 0.174282    0.36525   0.805875
  10 β”‚ 0.319033    0.424992  0.686173

julia> f1(x) = x .+ 1; f2(x) = x .+ 2; f3(x) = x .+ 3;

julia> w = [.5, .2, .3];

julia> @transform df :z = w[1] * f1(:u1) + w[2] * f2(:u2) + w[3] * f3(:u3)
10Γ—4 DataFrame
 Row β”‚ u1          u2        u3        z       
     β”‚ Float64     Float64   Float64   Float64 
─────┼─────────────────────────────────────────
   1 β”‚ 0.226441    0.46347   0.803374  2.24693
   2 β”‚ 0.698363    0.367743  0.452573  2.3585
   3 β”‚ 0.768411    0.168     0.6769    2.42088
   4 β”‚ 0.0897352   0.959212  0.551368  2.20212
   5 β”‚ 0.952901    0.407275  0.125132  2.39545
   6 β”‚ 0.951374    0.327541  0.100814  2.37144
   7 β”‚ 0.00756429  0.377843  0.60469   2.06076
   8 β”‚ 0.274698    0.1681    0.874693  2.23338
   9 β”‚ 0.174282    0.36525   0.805875  2.20195
  10 β”‚ 0.319033    0.424992  0.686173  2.25037
3 Likes

Thanks for your help and for the links of tutorials !

I come back to you.
It works for all functions but not for functions obtained by linear Interpolation as below

using Interpolations
x1=sort(vf[1][:,2])
y1=reverse(vf[1][:,1])
f1=LinearInterpolation(x1,y1)

where vf[1] is a 3x2 matrix

The error is :
BoundsError: attempt to access 3-element extrapolate(interpolate((::Vector{Float64},), ::Vector{Float64}, Gridded(Linear())), Throw()) with element type Float64 at index [2]

Thanks in advance.

This is a very different question. Please post it in a new thread.

Additionally, take a look at a little guide for asking informative questions.

OK; i will post a new thread.
Thanks !