Home > Software engineering >  how to get row average for certain columns in r data frame?
how to get row average for certain columns in r data frame?

Time:03-21

I have data that looks like this

t=c(3,2,9,8)
u=c(5,6,7,8)
v=c(3,2,1,9)
w=c(5,6,7,8)
x=c(1,2,3,4)
y=c(4,3,2,1)
z=data.frame(t,u,v,w,x,y)

output:
  t u v w x y
1 3 5 3 5 1 4
2 2 6 2 6 2 3
3 9 7 1 7 3 2
4 8 8 9 8 4 1

I would like to get the mean of each row for the first three columns, and then get the mean of each row for the last three columns. Ex. mean of row 1, columns t-v and mean of row 1, columns w-y, and so on.

Desired output:

  t u v avg w x y avg2
1 3 5 3 3.6 5 1 4 3
2 2 6 2 3.3 6 2 3 3.6
3 9 7 1 5.6 7 3 2 4
4 8 8 9 8.3 8 4 1 4.3

How can I go about doing this?

CodePudding user response:

Use rowMeans(). Using column names:

z$avg <- rowMeans(z[c("t", "u", "v")])
z$avg2 <- rowMeans(z[c("w", "x", "y")])

Result:

  t u v w x y      avg     avg2
1 3 5 3 5 1 4 3.666667 3.333333
2 2 6 2 6 2 3 3.333333 3.666667
3 9 7 1 7 3 2 5.666667 4.000000
4 8 8 9 8 4 1 8.333333 4.333333

Alternative using column indices, with re-arranged output:

z$avg <- rowMeans(z[1:3])
z$avg2 <- rowMeans(z[4:6])
z <- z[c(1:3, 7, 4:6, 8)]

Result:

  t u v      avg w x y     avg2
1 3 5 3 3.666667 5 1 4 3.333333
2 2 6 2 3.333333 6 2 3 3.666667
3 9 7 1 5.666667 7 3 2 4.000000
4 8 8 9 8.333333 8 4 1 4.333333

CodePudding user response:

One more alternative using tidyverse is the rowwise() and c_across

z %>% 
  rowwise() %>% 
  mutate(avg=mean(c_across(1:6)))

  • Related