I am simply trying to sum certain columns in a row and then divide the cells within the row by that sum. And I am wanting to do this in every row. for example, if we take the data set:
| lose | draw | win | goals |
|---|---|---|---|
| 7 | 2 | 5 | 12 |
| 1 | 2 | 13 | 21 |
We will ignore the goals scored and sum the win, lose and draw columns for the row, then by dividing each cell by the sum value we obtain:
| lose | draw | win | goals |
|---|---|---|---|
| 7/14 | 2/14 | 5/14 | 12 |
| 1/16 | 2 /16 | 13/16 | 21 |
The code I have tried using is:
Df_new <-t(apply(Df, select = c(lose, draw, win), function(x) x/sum(x)))
However, I am getting the error:
Error in match.fun(FUN) : argument "FUN" is missing, with no default
which a google search says I am missing a margin. But I thought that I had specified this by electing the columns.
Thank you