Title may be a little confusing so I will try to explain better here.
Let's say I have a data frame:
> df = data.frame(a=c(8,6,4,2),b=c(9,7,4,3),c=c(10,6,3,3),d=c(8,6,3,2))
> df
a b c d
1 8 9 10 8
2 6 7 6 6
3 4 4 3 3
4 2 3 3 2
My desired output would be:
> dfDesired = data.frame(a=c(8,6,4,2),b=c(0.33,0.37,0.4,0.38),c=c(0.37,0.32,0.3,0.38)
+ ,d=c(0.3,0.32,0.3,0.25))
> dfDesired
a b c d
1 8 0.33 0.37 0.30
2 6 0.37 0.32 0.32
3 4 0.40 0.30 0.30
4 2 0.38 0.38 0.25
First, I only want computations done on specific columns, in this case, columns b,c,d. Second, I want to sum the values in the row at the specified columns. So for row 1, 9+10+8=27. Then, I want to find the ratio of each cell with respect to the row sum. So, again for row 1, 9/27=0.33, 10/27=0.37,8/27=0.3, etc. for the other rows.
How can this be accomplished in R?