从dataframe df
开始我想用常量乘以选择列,然后使用这些选择列的行方式创建一个新的列/变量来填充它。现在,下面的代码是我知道如何做到这一点的方式。有没有办法将乘法和后续平均化成一个更简洁的表达式?
# 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))
答案 0 :(得分:1)
您可以跨数据框的多个列执行操作,并使用rowMeans()
函数:
> 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