我有一个数据帧列表,我已经计算了每个数据帧的每一列的 colmeans,除了数据帧的第 1 列和第 2 列。我的数据框列表的一小部分位于底部。
我用下面的行计算了colmeans
df_colmeans <- lapply(df, function(x) colMeans(x[-c(1:2)],na.rm = T))
我想将 colmeans 添加回我的数据帧列表中的每个数据帧,并尝试了下面的行,但结果不正确。
df <- rbind(df[-c(1:2)],df_colmeans)
我收到以下错误
在 rbind(df[-c(1:2)], df_colmeans) 中: 结果的列数不是向量长度的倍数(arg 2)
如果有一种方法可以一步完成,而无需创建单独的变量 (df_colmeans),那将是理想的。
非常感谢任何帮助和建议。
list(Fe.II. = structure(list(Lab = c("AD", "AD", "AK", "AK",
"AO", "AO", "BQ", "BQ", "CI", "CI", "CL", "CL", "CP", "CP", "CU",
"CU", "CW", "CW", "CZ", "CZ", "DA", "DA", "DC", "DC", "DF", "DF",
"EQ", "EQ", "EY", "EY", "FL", "FL", "FM", "FM", "FO", "FO", "FP",
"FP", "FR", "FR", "GB", "GB", "GC", "GC", "GL", "GL", "GM", "GM",
"GT", "GT", "GY", "GZ", "I", "I", "K", "K", "M", "M", "Q", "Q",
"S", "S", "U", "U", "V", "V", "W", "W"), `Sample Prep` = c(335,
337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337,
335, 337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337, 335,
337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337,
335, 337, 335, 337, 335, 337, 335, 337, 335, 337, 337, 337, 335,
337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337,
335, 337), `1` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.19,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 0.17, NA, 0.14, NA, NA, NA, NA, NA), `2` = c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 0.01, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.13, NA, 0.15, NA, NA,
NA, NA, NA), `3` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 0.02, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 0.23, NA, 0.29, NA, NA, NA, NA), `4` = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.02, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.22, NA,
0.29, NA, NA, NA, NA)), class = "data.frame", row.names = c(NA,
-68L)), SiO2 = structure(list(Lab = c("AD", "AD", "AK", "AK",
"AO", "AO", "BQ", "BQ", "CI", "CI", "CL", "CL", "CP", "CP", "CU",
"CU", "CW", "CW", "CZ", "CZ", "DA", "DA", "DC", "DC", "DF", "DF",
"EQ", "EQ", "EY", "EY", "FL", "FL", "FM", "FM", "FO", "FO", "FP",
"FP", "FR", "FR", "GB", "GB", "GC", "GC", "GL", "GL", "GM", "GM",
"GT", "GT", "GY", "GZ", "I", "I", "K", "K", "M", "M", "Q", "Q",
"S", "S", "U", "U", "V", "V", "W", "W"), `Sample Prep` = c(335,
337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337,
335, 337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337, 335,
337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337,
335, 337, 335, 337, 335, 337, 335, 337, 335, 337, 337, 337, 335,
337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337,
335, 337), `1` = c(3.65, NA, 3.595, NA, 3.645, NA, 3.66, NA,
3.644, NA, 3.547, NA, 3.648, NA, 3.655, NA, NA, NA, 3.666, NA,
3.686, NA, 3.674, NA, 3.667, NA, 3.59, NA, 3.63, NA, 3.689, NA,
3.64, NA, 3.58, NA, NA, NA, 3.46, NA, 3.71, NA, 3.66, NA, 3.65,
NA, 3.64, NA, 3.503, NA, NA, NA, 3.631, NA, 3.731, NA, 3.656,
NA, NA, NA, 3.75, NA, 3.656, NA, 3.667, NA, 3.684, NA), `2` = c(3.6,
NA, 3.591, NA, 3.646, NA, 3.65, NA, 3.625, NA, 3.548, NA, 3.648,
NA, 3.643, NA, NA, NA, 3.679, NA, 3.69, NA, 3.646, NA, 3.577,
NA, 3.6, NA, 3.63, NA, 3.692, NA, 3.66, NA, 3.59, NA, NA, NA,
3.512, NA, 3.703, NA, 3.62, NA, 3.63, NA, 3.63, NA, 3.49, NA,
NA, NA, 3.627, NA, 3.739, NA, 3.669, NA, NA, NA, 3.73, NA, 3.66,
NA, 3.664, NA, 3.669, NA), `3` = c(NA, 3.55, NA, 3.662, NA, 3.665,
NA, 3.65, NA, 3.67, NA, 3.576, NA, 3.631, NA, 3.683, NA, NA,
NA, 3.638, NA, 3.703, NA, 3.666, NA, NA, NA, 3.59, NA, 3.61,
NA, 3.657, NA, 3.67, NA, NA, NA, NA, NA, 3.8, NA, 3.684, NA,
3.69, NA, 3.66, NA, 3.69, NA, 4.398, 3.672, NA, NA, 3.678, NA,
3.769, NA, 3.678, NA, 3.718, NA, 3.75, NA, 3.636, NA, NA, NA,
3.603), `4` = c(NA, 3.57, NA, 3.69, NA, 3.624, NA, 3.66, NA,
3.673, NA, 3.551, NA, 3.64, NA, 3.684, NA, NA, NA, 3.678, NA,
3.673, NA, 3.696, NA, NA, NA, 3.56, NA, 3.62, NA, 3.615, NA,
3.66, NA, NA, NA, NA, NA, 3.642, NA, 3.684, NA, 3.67, NA, 3.67,
NA, 3.67, NA, 4.38, 3.672, NA, NA, 3.68, NA, 3.757, NA, 3.673,
NA, 3.759, NA, 3.78, NA, 3.64, NA, NA, NA, 3.597)), class = "data.frame", row.names = c(NA,
-68L)), CaO = structure(list(Lab = c("AD", "AD", "AK", "AK",
"AO", "AO", "BQ", "BQ", "CI", "CI", "CL", "CL", "CP", "CP", "CU",
"CU", "CW", "CW", "CZ", "CZ", "DA", "DA", "DC", "DC", "DF", "DF",
"EQ", "EQ", "EY", "EY", "FL", "FL", "FM", "FM", "FO", "FO", "FP",
"FP", "FR", "FR", "GB", "GB", "GC", "GC", "GL", "GL", "GM", "GM",
"GT", "GT", "GY", "GZ", "I", "I", "K", "K", "M", "M", "Q", "Q",
"S", "S", "U", "U", "V", "V", "W", "W"), `Sample Prep` = c(335,
337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337,
335, 337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337, 335,
337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337,
335, 337, 335, 337, 335, 337, 335, 337, 335, 337, 337, 337, 335,
337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337, 335, 337,
335, 337), `1` = c(0.04, NA, NA, NA, 0.055, NA, 0.053, NA, 0.055,
NA, 0.021, NA, 0.049, NA, 0.052, NA, NA, NA, 0.053, NA, 0.056,
NA, 0.054, NA, 0.036, NA, 0.032, NA, 0.06, NA, 0.053, NA, 0.05,
NA, 0.043, NA, NA, NA, NA, NA, 0.054, NA, 0.057, NA, 0.052, NA,
0.05, NA, 0.048, NA, NA, NA, 0.056, NA, 0.043, NA, 0.053, NA,
NA, NA, 0.052, NA, 0.076, NA, 0.051, NA, 0.047, NA), `2` = c(0.04,
NA, NA, NA, 0.054, NA, 0.053, NA, 0.055, NA, 0.023, NA, 0.05,
NA, 0.05, NA, NA, NA, 0.051, NA, 0.053, NA, 0.056, NA, 0.036,
NA, 0.032, NA, 0.06, NA, 0.053, NA, 0.05, NA, 0.043, NA, NA,
NA, NA, NA, 0.056, NA, 0.056, NA, 0.057, NA, 0.05, NA, 0.05,
NA, NA, NA, 0.053, NA, 0.043, NA, 0.053, NA, NA, NA, 0.052, NA,
0.076, NA, 0.051, NA, 0.047, NA), `3` = c(NA, 0.04, NA, NA, NA,
0.055, NA, 0.053, NA, 0.057, NA, 0.023, NA, 0.05, NA, 0.055,
NA, NA, NA, 0.053, NA, 0.053, NA, 0.054, NA, NA, NA, 0.045, NA,
0.06, NA, 0.052, NA, 0.05, NA, NA, NA, NA, NA, 0.05, NA, 0.054,
NA, 0.056, NA, 0.054, NA, 0.05, NA, 0.054, 0.057, NA, NA, 0.057,
NA, 0.053, NA, 0.054, NA, 0.052, NA, 0.05, NA, 0.045, NA, NA,
NA, 0.051), `4` = c(NA, 0.04, NA, NA, NA, 0.054, NA, 0.053, NA,
0.056, NA, 0.019, NA, 0.052, NA, 0.056, NA, NA, NA, 0.051, NA,
0.051, NA, 0.056, NA, NA, NA, 0.047, NA, 0.06, NA, 0.046, NA,
0.05, NA, NA, NA, NA, NA, 0.05, NA, 0.054, NA, 0.055, NA, 0.054,
NA, 0.05, NA, 0.052, 0.056, NA, NA, 0.055, NA, 0.053, NA, 0.053,
NA, 0.051, NA, 0.052, NA, 0.046, NA, NA, NA, 0.051)), class = "data.frame", row.names = c(NA,
-68L)))
答案 0 :(得分:1)
是否要将列均值作为新行添加到现有数据框中?
试试:
result <- lapply(df, function(x) rbind(x[-c(1:2)], colMeans(x[-c(1:2)],na.rm = TRUE)))