(Julia) Assigning DataFrame column sum to a new column

Viewed 64

My DataFrame looks like this

 poly-d bias      var   irreducible_error   mse
Float64 Float64 Float64 Float64 Float64
  0.0  0.186675     0.00042362   0.01  0.197262
  1.0  0.0411247    0.000571736  0.01  0.051418
  2.0  0.0434971    0.000984634  0.01  0.0545198
  3.0  1.85323e-5   0.00144489   0.01  0.011316
  4.0  4.88342e-5   0.00140199   0.01  0.0110883
  5.0  1.6811e-6    0.00220646   0.01  0.0121978
  6.0  1.05011e-5   0.00213953   0.01  0.0122632
  7.0  4.17582e-9   0.00250119   0.01  0.0119988
  8.0  0.000105478  0.00314533   0.01  0.0135444
  9.0  4.51571e-6   0.00291042   0.01  0.0127425
 10.0  1.07721e-6   0.00381774   0.01  0.0142986
 11.0  5.78187e-6   0.00398581   0.01  0.0147937
 12.0  7.96598e-7   0.00461939   0.01  0.0134817
 13.0  2.95272e-5   0.00490143   0.01  0.0136078
 14.0  1.03758e-5   0.00558545   0.01  0.0160345
 15.0  1.36473e-6   0.00655103   0.01  0.0157278
 16.0  3.65874e-6   0.00706143   0.01  0.0185338
 17.0  4.49153e-6   0.00653606   0.01  0.0156871
 18.0  2.79525e-6   0.00774455   0.01  0.0178697
 19.0  3.0369e-5    0.00919628   0.01  0.018715  

I take the sum of the columns the Julia way like this

sum(Matrix(res[:,["bias","var","irreducible_error"]]),dims=2)

I'd like to assign this output as a new column mse_explained.

It seems like this should work

res[:mse_explained] = sum(Matrix(res[:,["bias","var","irreducible_error"]]),dims=2)

But there's a DimensionMismatch. I checked and sum(Matrix(res[:,["bias","var","irreducible_error"]]),dims=2) is a 20x1 array so I'm not sure why Julia is having a hard time.

I tried this as well which didn't work

res[:mse_explained] .= sum(Matrix(res[:,["bias","var","irreducible_error"]]),dims=2)
1 Answers

this should work:

res.mse_explained = vec(sum(Matrix(res[:,["bias","var","irreducible_error"]]),dims=2))

However, a standard way to do what you want would be:

transform!(res, ["bias","var","irreducible_error"] => (+) => "mse_explained")
Related