How to get a mean off multiple column values in R

In SAS, this would be:

newvariable=mean(of x1, x2, x3, x4);

Stockoverflow

https://stackoverflow.com/questions/57015735/how-to-get-a-mean-of-multiple-column-values-using-r-dplyr/57015890#57015890

R

Approach 1:

x2 <-subset(time1data,select=c(x1, x2, x3, x4, x5))
time1data$newvar<-rowMeans(x2,na.rm=TRUE)

Approach 2

time1data$newvar<-rowMeans(time1data[,c("q0008_0001", "q0008_0002", "q0008_0003", "q0008_0004", "q0008_0005", "q0008_0006", "q0008_0007", "q0008_0008")])

Approach 3

time1data$newvar<-rowMeans(time1data[,c("q0008_0001", "q0008_0002", "q0008_0003", "q0008_0004", "q0008_0005", "q0008_0006", "q0008_0007", "q0008_0008")],na.rm=TRUE)

CategoriesR

Leave a Reply