La Machine Infernale La Machine Infernale - 3 months ago 10
R Question

r: append mean of a subset of columns by name

I have this df:

webvisits1 webvisits2 webvisits3 webvisits4
s001 2 0 11 2
s002 11 2 23 3
s003 12 1 1 5
s004 13 5 5 0
s005 4 3 9 3


I need to create an output dataframe with an added columns containing the difference between the mean of webvisits(3-4) and webvisits (1-2), like so:

webvisits1 webvisits2 webvisits3 webvisits4 difference_mean
s001 2 0 11 2 -5.5
s002 11 2 23 3 -6.5
s003 12 1 1 5 3.5
s004 13 5 5 0 6.5
s005 4 3 9 3 -2.5


Is there an easy way to do so, considering that column names (webvisits) are important?
Thank you

Answer

We subset the dataset into two (df[1:2], df[3:4]), get the difference and then with rowMeans we find the mean, create a new column 'differenceMean' using transform.

df <- transform(df, differenceMean = rowMeans(df[1:2]- df[3:4]))
df 
#     webvisits1 webvisits2 webvisits3 webvisits4 differenceMean
#s001          2          0         11          2           -5.5
#s002         11          2         23          3           -6.5
#s003         12          1          1          5            3.5
#s004         13          5          5          0            6.5
#s005          4          3          9          3           -2.5
Comments