# Cumulative sum on rows of a dataframe

I am a beginner in Julia…
I am trying to add a new column with cumulative values in regards with some other columns

``````function weighted_sum(input_df::DataFrame, weight::Array{Int64,1}, start_col=2)
somme_df = deepcopy(input_df)
l,end_col=size(input_df)
for i in start_col:end_col
somme_df[!,i]=weight[i-1]*input_df[!,i]
end
somme_df.cumValue = [sum(somme_df[!, j]) for j in start_col:end_col]
return somme_df
end
``````

But i doesn’t work : no new column cumValue is added.

The cumulative section here may help.

Do you really want a cumulative sum on the rows? From your code it looks that you rather want a linear combination of columns…

Here are some ways to do a linear combination of columns. In these examples we want to combine columns 2,3,4 with respective weights 10,1,0:

``````julia> weights = [10, 1, 0];

julia> df = DataFrame(rand(0:9, 3, 4), :auto)
3×4 DataFrame
Row │ x1     x2     x3     x4
│ Int64  Int64  Int64  Int64
─────┼────────────────────────────
1 │     9      4      5      6
2 │     1      2      3      9
3 │     2      3      4      2
``````

One way is using linear algebra, putting the columns in a matrix:

``````df.cumValue = Array(df[!, 2:end]) * weights

# or
transform(df, 2:4 => ((cols...) -> hcat(cols...) * weights) => :cumValue)

# result:
3×5 DataFrame
Row │ x1     x2     x3     x4     cumValue
│ Int64  Int64  Int64  Int64  Int64
─────┼──────────────────────────────────────
1 │     9      4      5      6        45
2 │     1      2      3      9        23
3 │     2      3      4      2        34
``````

Another way is to implement the linear combination with `mapreduce`:

``````transform(df, 2:4 => ((c...) -> mapreduce(*, +, c, weights)) => :cumValue)
``````

I think the best is to define the linear combination as a function:

``````linear_combination(weights) = (cols...) -> mapreduce(*, +, cols, weights)

transform(df, 2:4 => linear_combination(weights) => :cumValue)
``````
1 Like

It’s exactly what i need : thanks a lot !

1 Like