我一直在搜索并尝试多种不同的方法来平均data.frame
中的每第10列。数据集是52行×60列。标题为data.frame
的{{1}}对于前两行看起来像这样:
data
(4和5以及顶行只是文件中的占位符。
正在从X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20 X21 X22 X23 X24 X25 X26 X27 X28 X29 X30 X31 X32 X33 X34 X35 X36 X37 X38 X39 X40 X41 X42 X43 X44 X45 X46 X47 X48 X49 X50 X51 X52 X53 X54 X55 X56 X57 X58 X59 X60
4 14.7637 14.2117 14.1237 13.6637 12.9837 13.3237 13.8877 15.0997 15.5717 16.5157 15.0597 13.5317 13.6957 13.2637 13.5117 13.4237 14.1277 13.8437 12.8357 13.6277 13.2077 14.9837 16.1277 15.6197 15.7517 16.8557 15.9757 15.9677 16.1677 17.1557 16.1157 16.3557 16.2037 16.8077 16.6757 16.4837 16.7877 16.1037 16.3117 16.0637 16.1077 16.2477 17.1917 18.1236 18.5036 18.2956 20.9516 18.0636 18.5516 19.1756 19.5996 19.2036 18.1996 16.7117 16.7037 16.7877 16.5837 17.6636 18.8596 18.3356
5 16.9597 15.9037 15.3917 15.6797 15.6797 15.8397 17.1517 18.0796 18.6236 20.4796 18.8796 16.2877 16.7997 15.6157 16.9917 16.8317 16.9917 17.5356 16.3517 15.1357 16.5437 17.4077 18.4316 17.0557 17.3117 19.1676 18.2396 16.7037 17.2157 19.1676 18.2076 16.7677 18.7196 19.4236 18.2716 17.5356 18.7196 17.8876 17.2477 16.9597 17.2797 18.3996 19.5516 19.2636 20.0956 20.4476 21.5356 18.4316 20.7356 22.1436 21.6636 20.7676 19.7436 18.5596 17.9516 17.8876 18.1116 19.2956 20.3516 19.4876
文件中读取和提取数据,我想平均每10列将其从60列更改为6.以下是我见过的人们之前要求的一些额外信息:< / p>
.txt
最近我尝试过:
> class(data)
[1] "data.frame"
> str(data)
'data.frame': 52 obs. of 60 variables:
$ X1 : Factor w/ 53 levels "0","0.0319994",..: 31 32 34 30 51 48 45 39 36 28 ...
$ X2 : Factor w/ 48 levels "0","0.0319994",..: 27 30 29 26 46 42 39 31 23 19 ...
和
dataMean <- data.frame(Means=rowMeans(data), ncol=10)
并且两者都给出了关于'x'的相同错误必须是数字。任何人可以提供的帮助将不胜感激。
提前致谢!
编辑:所需的结果将是这样的,其中列数已减少,并且每10列计算算术平均值:
dataMean <- rowMeans(data.frame(data, ncol=10))
EDIT2:
X1 X2 X3 X4 X5 X6
4 14.4145 13.6921 15.7813 16.3909 18.12123 17.86484
5 16.97887 16.74208 17.72446 17.97403 19.78841 19.382
答案 0 :(得分:4)
我们可以使用split
和rowMeans
:
as.data.frame(sapply(
split(seq_along(df),(seq_along(df)-1) %/%10),
function(x) rowMeans(df[x])
))
# 0 1 2 3 4 5
# 4 14.41450 13.69210 15.78130 16.39090 18.12123 17.86484
# 5 16.97887 16.74208 17.72446 17.97403 19.78841 19.38200
数据强>
df <- read.table(header=TRUE,stringsAsFactors=FALSE,text="X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20 X21 X22 X23 X24 X25 X26 X27 X28 X29 X30 X31 X32 X33 X34 X35 X36 X37 X38 X39 X40 X41 X42 X43 X44 X45 X46 X47 X48 X49 X50 X51 X52 X53 X54 X55 X56 X57 X58 X59 X60
4 14.7637 14.2117 14.1237 13.6637 12.9837 13.3237 13.8877 15.0997 15.5717 16.5157 15.0597 13.5317 13.6957 13.2637 13.5117 13.4237 14.1277 13.8437 12.8357 13.6277 13.2077 14.9837 16.1277 15.6197 15.7517 16.8557 15.9757 15.9677 16.1677 17.1557 16.1157 16.3557 16.2037 16.8077 16.6757 16.4837 16.7877 16.1037 16.3117 16.0637 16.1077 16.2477 17.1917 18.1236 18.5036 18.2956 20.9516 18.0636 18.5516 19.1756 19.5996 19.2036 18.1996 16.7117 16.7037 16.7877 16.5837 17.6636 18.8596 18.3356
5 16.9597 15.9037 15.3917 15.6797 15.6797 15.8397 17.1517 18.0796 18.6236 20.4796 18.8796 16.2877 16.7997 15.6157 16.9917 16.8317 16.9917 17.5356 16.3517 15.1357 16.5437 17.4077 18.4316 17.0557 17.3117 19.1676 18.2396 16.7037 17.2157 19.1676 18.2076 16.7677 18.7196 19.4236 18.2716 17.5356 18.7196 17.8876 17.2477 16.9597 17.2797 18.3996 19.5516 19.2636 20.0956 20.4476 21.5356 18.4316 20.7356 22.1436 21.6636 20.7676 19.7436 18.5596 17.9516 17.8876 18.1116 19.2956 20.3516 19.4876")
答案 1 :(得分:2)
这是tidyverse
可能性
library(tidyverse)
df %>%
rowid_to_column("row") %>%
gather(k, v, -row) %>%
mutate(group = (as.numeric(sub("X", "", k)) - 1) %/% 10) %>%
group_by(group, row) %>%
summarise(v.mean = mean(v)) %>%
spread(group, v.mean) %>%
select(-row)
## A tibble: 2 x 6
# `0` `1` `2` `3` `4` `5`
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 14.4 13.7 15.8 16.4 18.1 17.9
#2 17.0 16.7 17.7 18.0 19.8 19.4
如果您有超过2行,则同样有效。以下是使用50x60 data.frame
。
ncol <- 60;
nrow <- 50;
df <- data.frame(matrix(runif(nrow * ncol), ncol = ncol))
df %>%
rowid_to_column("row") %>%
gather(k, v, -row) %>%
mutate(group = (as.numeric(sub("X", "", k)) - 1) %/% 10) %>%
group_by(group, row) %>%
summarise(v.mean = mean(v)) %>%
spread(group, v.mean) %>%
select(-row)
## A tibble: 50 x 6
# `0` `1` `2` `3` `4` `5`
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 0.372 0.514 0.400 0.565 0.489 0.412
# 2 0.344 0.465 0.625 0.421 0.602 0.519
# 3 0.393 0.389 0.465 0.607 0.504 0.539
# 4 0.545 0.599 0.530 0.552 0.661 0.568
# 5 0.589 0.456 0.590 0.557 0.441 0.494
# 6 0.588 0.602 0.362 0.524 0.526 0.644
# 7 0.432 0.624 0.457 0.539 0.530 0.481
# 8 0.494 0.519 0.661 0.568 0.709 0.610
# 9 0.397 0.413 0.398 0.370 0.720 0.570
#10 0.639 0.495 0.551 0.717 0.721 0.496
## ... with 40 more rows
df <- read.table(text =
"X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20 X21 X22 X23 X24 X25 X26 X27 X28 X29 X30 X31 X32 X33 X34 X35 X36 X37 X38 X39 X40 X41 X42 X43 X44 X45 X46 X47 X48 X49 X50 X51 X52 X53 X54 X55 X56 X57 X58 X59 X60
4 14.7637 14.2117 14.1237 13.6637 12.9837 13.3237 13.8877 15.0997 15.5717 16.5157 15.0597 13.5317 13.6957 13.2637 13.5117 13.4237 14.1277 13.8437 12.8357 13.6277 13.2077 14.9837 16.1277 15.6197 15.7517 16.8557 15.9757 15.9677 16.1677 17.1557 16.1157 16.3557 16.2037 16.8077 16.6757 16.4837 16.7877 16.1037 16.3117 16.0637 16.1077 16.2477 17.1917 18.1236 18.5036 18.2956 20.9516 18.0636 18.5516 19.1756 19.5996 19.2036 18.1996 16.7117 16.7037 16.7877 16.5837 17.6636 18.8596 18.3356
5 16.9597 15.9037 15.3917 15.6797 15.6797 15.8397 17.1517 18.0796 18.6236 20.4796 18.8796 16.2877 16.7997 15.6157 16.9917 16.8317 16.9917 17.5356 16.3517 15.1357 16.5437 17.4077 18.4316 17.0557 17.3117 19.1676 18.2396 16.7037 17.2157 19.1676 18.2076 16.7677 18.7196 19.4236 18.2716 17.5356 18.7196 17.8876 17.2477 16.9597 17.2797 18.3996 19.5516 19.2636 20.0956 20.4476 21.5356 18.4316 20.7356 22.1436 21.6636 20.7676 19.7436 18.5596 17.9516 17.8876 18.1116 19.2956 20.3516 19.4876", header = T)
答案 2 :(得分:1)
rollmean
的 zoo
可能会对您有所帮助:
library(zoo)
m <- apply(df,1,rollmean,10)
t(m[seq(nrow(m)) %% 10 ==1,])
# X5 X15 X25 X35 X45 X55
# 4 14.41450 13.69210 15.78130 16.39090 18.12123 17.86484
# 5 16.97887 16.74208 17.72446 17.97403 19.78841 19.38200
我从Moody_Mudskipper的回答中重用了df
。
答案 3 :(得分:-1)
我昨天晚上发布了这个问题,但最终通过更多搜索找到了解决方案。我发现,我不得不将data.frame
转换为matrix
,然后将transpose
转换为矩阵,以平均10列中的每一列。然后,我将数据重新转换回我想要的形状。
y <- apply(as.matrix(data), 2, as.numeric)
z <- t(y)
n=10
MatrixMeanD <- colMeans(matrix(z, nrow=10))
#dont know why but rowMeans didnt work for me, while colMeans did?
x <- t(MatrixMeanD)
MatrixMean <- t(matrix(x,,52))
write.csv(MatrixMean,"file")
谢谢所有给我建议并试图帮助我修复代码的人!