nofunsally
nofunsally

Reputation: 2091

Arithmetic then row means for select columns of dataframe

Starting with dataframe df I would like to multiply select columns by a constant, and then create a new column/variable using the row means of these select columns to populate it. Right now, the code below is the way I know how to do this. Is there way to this where the multiplication and subsequent averaging could be made into a more succinct expression?

# create dataframe
df<-data.frame(replicate(6,sample(0:100,10,rep=TRUE)))

# multiply some of the columns by 1.05 
df$X1 <- df$X1 * 1.05
df$X2 <- df$X2 * 1.05 
df$X3 <- df$X3 * 1.05 
df$X4 <- df$X4 * 1.05 

# get rowmeans for selected / subsetted columns
df$XX <- apply(subset(df, select = c("X1","X2","X3","X4")),
                       1, function(d) mean(d, na.rm=TRUE))

Upvotes: 0

Views: 1186

Answers (1)

David
David

Reputation: 9405

You can do operations across multiple columns of a data frame and use the rowMeans() function:

> df<-data.frame(replicate(6,sample(0:100,10,rep=TRUE)))
> head(df)
  X1 X2 X3 X4 X5 X6
1 76 66 69 73 37 55
2 36 91 77 84 48 48
3 45 40  4 18 96 25
4 20 63 72 98 63 88
5 92 92 74 27 74  8
6 60 19 35 20 71 59
> df[,1:4] <- df[,1:4]*1.05
> head(df)
     X1    X2    X3     X4 X5 X6
1 79.80 69.30 72.45  76.65 37 55
2 37.80 95.55 80.85  88.20 48 48
3 47.25 42.00  4.20  18.90 96 25
4 21.00 66.15 75.60 102.90 63 88
5 96.60 96.60 77.70  28.35 74  8
6 63.00 19.95 36.75  21.00 71 59
> df$XX <- rowMeans(df[,1:4])
> head(df)
     X1    X2    X3     X4 X5 X6      XX
1 79.80 69.30 72.45  76.65 37 55 74.5500
2 37.80 95.55 80.85  88.20 48 48 75.6000
3 47.25 42.00  4.20  18.90 96 25 28.0875
4 21.00 66.15 75.60 102.90 63 88 66.4125
5 96.60 96.60 77.70  28.35 74  8 74.8125
6 63.00 19.95 36.75  21.00 71 59 35.1750

Upvotes: 1

Related Questions