r: append mean of a subset of columns by name -


i have 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 create output dataframe added columns containing difference between mean of webvisits(3-4) , webvisits (1-2), 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 easy way so, considering column names (webvisits) important? thank you

we subset dataset 2 (df[1:2], df[3:4]), difference , rowmeans find mean, create 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

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -