Lapply()R中的函数出错

时间:2017-07-06 08:38:21

标签: r lapply

我在脚本中出现了一个非常奇怪的错误,过去工作得很好而且我不知道问题是什么。我开始创建一个很长的列表,其中包含几个具有确切列数的数据帧。该列表名为lst。然后我想用手段和sd做一个总结表。这是脚本:

 w1 <- lapply(lst, function(i) t(cbind(Mean = colMeans(i[, c(6,7,8,9)], na.rm = TRUE), 
                                  Sds = colSds(as.matrix(i[, c(6,7,8,9)]), na.rm = TRUE),
                                  N = length(i[,2]),
                                  len.max=max(i[,6]))))

列数是正确的。但是,当我首先运行脚本时,我得到了调试位置,当我停止时,我收到此错误消息:

Error in t(cbind(Mean = colMeans(i[, c(6, 7, 8, 9)], na.rm = TRUE), Sds = colSds(as.matrix(i[,  : 
error in evaluating the argument 'x' in selecting a method for function 't': Error in `[.data.frame`(i, , c(6, 7, 8, 9)) : undefined columns selected

我不知道这个功能有什么问题。我尝试在互联网上搜索,我看到了一些关于更改的内容,data.matrix的矩阵。然而,这并没有成功。

事实上,我对另一个非常相似的函数也有同样的问题:

a1 <- lapply(lst, function(i) t(cbind(l1 = NROW(which(i[,6]>1)),
                                  l1.05 = NROW(which(i[,6]<=1)) - NROW(which(i[,6]>0.5)),
                                  l05.03 = NROW(which(i[,6]>0.3)) - NROW(which(i[,6]<=0.5)),
                                  l03 = NROW(which(i[,6]<=0.3)))))

结果相同:

Error in t(cbind(l1 = NROW(which(i[, 6] > 1)), l1.05 = NROW(which(i[,  : 
error in evaluating the argument 'x' in selecting a method for function 't': Error in `[.data.frame`(i, , 6) : undefined columns selected

有人能指出我是什么问题。你需要一些数据吗?谢谢!

我正在使用最后一个RStudio并使用以下软件包: plyr,matrixStats,dplyr

以下是列表的示例:

> lst
[[1]]
  X Chr new     pos1     pos2 len     nsnp    n.ind   per.ind
1 1   1   1 12900000 13700000 0.9 284.7560 23.77778  7.952434
2 2   1   2 17000000 17300000 0.4 126.5582 16.00000  5.351171
3 3   1   3 21200000 21500000 0.4 126.5582 40.75000 13.628763
4 4   1   4 45300000 45700000 0.5 158.1978 23.20000  7.759197
5 5   1   5 45900000 46600000 0.8 253.1165 31.12500 10.409699

[[2]]
  X Chr new     pos1     pos2 len      nsnp    n.ind  per.ind
1 1   1   1 12900000 13700000 0.9 312.90267 24.44444 4.288499
2 2   1   2 21200000 21500000 0.4 139.06785 38.00000 6.666667
3 3   1   3 32600000 33000000 0.5 173.83482 28.40000 4.982456
4 4   1   4 35800000 36100000 0.4 139.06785 37.25000 6.535088
5 5   1   5 36300000 36300000 0.1  34.76696 22.00000 3.859649

[[3]]
  X Chr new     pos1     pos2 len     nsnp    n.ind  per.ind
1 1   1   1 35700000 36500000 0.9 287.4214 12.22222 11.42264
2 2   1   2 45900000 46600000 0.8 255.4857 12.50000 11.68224
3 3   1   3 49400000 50700000 1.4 447.1000 21.78571 20.36048
4 4   1   4 51000000 52000000 1.1 351.2929 16.00000 14.95327
5 5   1   5 52200000 53000000 0.9 287.4214 19.66667 18.38006

dput(lst[1:3])
list(structure(list(X = 1:5, Chr = c(1L, 1L, 1L, 1L, 1L), new = 1:5, 
pos1 = c(12900000, 1.7e+07, 21200000, 45300000, 45900000), 
pos2 = c(13700000, 17300000, 21500000, 45700000, 46600000
), len = c(0.9, 0.4, 0.4, 0.5, 0.8), nsnp = c(284.756031128405, 
126.558236057069, 126.558236057069, 158.197795071336, 253.116472114137
), n.ind = c(23.7777777777778, 16, 40.75, 23.2, 31.125), 
per.ind = c(7.95243403939056, 5.35117056856187, 13.628762541806, 
7.75919732441472, 10.4096989966555)), .Names = c("X", "Chr", 
"new", "pos1", "pos2", "len", "nsnp", "n.ind", "per.ind"), row.names = c(NA, 
5L), class = "data.frame"), structure(list(X = 1:5, Chr = c(1L, 
1L, 1L, 1L, 1L), new = 1:5, pos1 = c(12900000, 21200000, 32600000, 
35800000, 36300000), pos2 = c(13700000, 21500000, 3.3e+07, 36100000, 
36300000), len = c(0.9, 0.4, 0.5, 0.4, 0.1), nsnp = c(312.90267141585, 
139.0678539626, 173.83481745325, 139.0678539626, 34.76696349065
), n.ind = c(24.4444444444444, 38, 28.4, 37.25, 22), per.ind = c(4.28849902534113, 
6.66666666666667, 4.98245614035088, 6.53508771929825, 3.85964912280702
)), .Names = c("X", "Chr", "new", "pos1", "pos2", "len", "nsnp", 
"n.ind", "per.ind"), row.names = c(NA, 5L), class = "data.frame"), 
structure(list(X = 1:5, Chr = c(1L, 1L, 1L, 1L, 1L), new = 1:5, 
    pos1 = c(35700000, 45900000, 49400000, 5.1e+07, 52200000
    ), pos2 = c(36500000, 46600000, 50700000, 5.2e+07, 5.3e+07
    ), len = c(0.9, 0.8, 1.4, 1.1, 0.9), nsnp = c(287.421428571429, 
    255.485714285714, 447.1, 351.292857142857, 287.421428571429
    ), n.ind = c(12.2222222222222, 12.5, 21.7857142857143, 
    16, 19.6666666666667), per.ind = c(11.4226375908619, 
    11.6822429906542, 20.3604806408545, 14.9532710280374, 
    18.380062305296)), .Names = c("X", "Chr", "new", "pos1", 
"pos2", "len", "nsnp", "n.ind", "per.ind"), row.names = c(NA, 
5L), class = "data.frame"))

0 个答案:

没有答案