【发布时间】:2019-08-03 06:24:22
【问题描述】:
我想在包含 60 列的数据框中找到每 3 列的平均值,这样一个新的数据框就会有 20 列左右。我将示例数据如下: 此外,如果我可以将它们作为字符串数组传递,新变量也会有所帮助。
structure(list(`1961` = c(0, 0, 0, 0, 0, 0, 0, 0, 4.633, 54.247,
0, 0, 0, 0, 0, 0, 0, 31.036, 3.18, 19.862), `1962` = c(0, 0,
0, 0, 0, 0, 0, 0, 4.168, 63.587, 0, 0, 0, 0, 0, 0, 0, 28.169,
2.913, 17.273), `1963` = c(0, 0, 0, 0, 0, 0, 0, 0, 3.284, 56.888,
0, 0, 0, 0, 0, 0, 0, 26.667, 2.653, 16.586), `1964` = c(0, 0,
0, 0, 0, 0, 0, 0, 2.689, 48.722, 0, 0, 0, 0, 0, 0, 0, 25.483,
3.873, 15.708), `1965` = c(0, 0, 0, 0, 0, 0, 0, 0, 3.304, 33.838,
0, 0, 0, 0, 0, 0, 0, 28.164, 3.927, 11.147), `1966` = c(0, 0,
0, 0, 0, 0, 0, 0, 2.871, 26.695, 0, 0, 0, 0, 0, 0, 0, 28.962,
4.434, 14.056), `1967` = c(0, 0, 0, 0, 0, 0, 0, 0, 2.752, 36.246,
0, 0, 0, 0, 0, 0, 0, 30.877, 4.739, 14.765), `1968` = c(0, 0,
0, 0, 0, 0, 0, 0, 3.537, 33.368, 0, 0, 0, 0, 0, 0, 0, 25.628,
5.445, 14.372), `1969` = c(0, 0, 0, 0, 0, 0, 0, 0, 2.484, 35.711,
0, 0, 0, 0, 0, 0, 0, 27.123, 5.286, 15.527)), row.names = c("Almonds, with shell",
"Anise, badian, fennel, coriander", "Apples", "Apricots", "Areca nuts",
"Asparagus", "Avocados", "Bananas", "Barley", "Bastfibres, other",
"Beans, dry", "Beans, green", "Berries nes", "Broad beans, horse beans, dry",
"Buckwheat", "Cabbages and other brassicas", "Carrots and turnips",
"Cashew nuts, with shell", "Cassava", "Castor oil seed"), class = "data.frame")
【问题讨论】:
标签: r multiple-columns mean