Help in sum columns

Do you want to sum rows or do you want a single number?

Here are the ways to produce both:

julia> df = DataFrame(rand(5, 4), :auto)
5×4 DataFrame
 Row │ x1         x2        x3        x4
     │ Float64    Float64   Float64   Float64
─────┼─────────────────────────────────────────
   1 │ 0.428323   0.757593  0.31246   0.640604
   2 │ 0.633038   0.169417  0.528082  0.669834
   3 │ 0.0376092  0.786284  0.399867  0.720137
   4 │ 0.673165   0.66684   0.437688  0.35086
   5 │ 0.614091   0.682412  0.511448  0.495272

julia> transform(df, [:x1, :x2, :x3, :x4] => (+) => :sum_of_rows)
5×5 DataFrame
 Row │ x1         x2        x3        x4        sum_of_rows
     │ Float64    Float64   Float64   Float64   Float64
─────┼──────────────────────────────────────────────────────
   1 │ 0.428323   0.757593  0.31246   0.640604      2.13898
   2 │ 0.633038   0.169417  0.528082  0.669834      2.00037
   3 │ 0.0376092  0.786284  0.399867  0.720137      1.9439
   4 │ 0.673165   0.66684   0.437688  0.35086       2.12855
   5 │ 0.614091   0.682412  0.511448  0.495272      2.30322

julia> transform(df, [:x1, :x2, :x3, :x4] => ((v...) -> sum(+(v...))) => :total_sum)
5×5 DataFrame
 Row │ x1         x2        x3        x4        total_sum
     │ Float64    Float64   Float64   Float64   Float64
─────┼────────────────────────────────────────────────────
   1 │ 0.428323   0.757593  0.31246   0.640604     10.515
   2 │ 0.633038   0.169417  0.528082  0.669834     10.515
   3 │ 0.0376092  0.786284  0.399867  0.720137     10.515
   4 │ 0.673165   0.66684   0.437688  0.35086      10.515
   5 │ 0.614091   0.682412  0.511448  0.495272     10.515

However, since you are struggling maybe using DataFramesMeta.jl will be easier for you:

julia> using DataFramesMeta

julia> @transform(df, :sum_of_rows = :x1 + :x2 + :x3 + :x4)
5×5 DataFrame
 Row │ x1         x2        x3        x4        sum_of_rows
     │ Float64    Float64   Float64   Float64   Float64
─────┼──────────────────────────────────────────────────────
   1 │ 0.428323   0.757593  0.31246   0.640604      2.13898
   2 │ 0.633038   0.169417  0.528082  0.669834      2.00037
   3 │ 0.0376092  0.786284  0.399867  0.720137      1.9439
   4 │ 0.673165   0.66684   0.437688  0.35086       2.12855
   5 │ 0.614091   0.682412  0.511448  0.495272      2.30322

julia> @transform(df, :total_sum = sum(:x1 + :x2 + :x3 + :x4))
5×5 DataFrame
 Row │ x1         x2        x3        x4        total_sum
     │ Float64    Float64   Float64   Float64   Float64
─────┼────────────────────────────────────────────────────
   1 │ 0.428323   0.757593  0.31246   0.640604     10.515
   2 │ 0.633038   0.169417  0.528082  0.669834     10.515
   3 │ 0.0376092  0.786284  0.399867  0.720137     10.515
   4 │ 0.673165   0.66684   0.437688  0.35086      10.515
   5 │ 0.614091   0.682412  0.511448  0.495272     10.515
2 Likes