【发布时间】:2019-11-15 10:29:09
【问题描述】:
所以,我有一个数据框列表,命名为 "D1.txt", "D2.txt"................"D45.txt".
Each of the file contains2 列,每个文件有 1000 行。
我正在尝试通过以下代码向列表中的每个数据框添加一个新列,但它显示错误为
incorrect number of subscripts on matrix.
我使用的代码是,
L <- lapply(seq_along(L), function(i) {
L[[i]][, paste0('DF', i)] <- 1
L[[i]]
})
其中L 是包含数据框的列表的名称。
为什么会出现这个错误?谢谢! :)
编辑:一个可重现的例子:
# Create dummy data
L <- replicate(5, expand.grid(1:10, 1:10)[sample(100, 10), ], simplify=FALSE)
# Add a column to each data.frame in L.
# This will indicate presence of the pair when we merge.
L <- lapply(seq_along(L), function(i) {
L[[i]][, paste0('DF', i)] <- 1
L[[i]]
})
【问题讨论】:
-
一个可重现的小例子会很好。
-
我应该上传一些文件的样本吗?
-
谢谢!它在我的机器上运行没有错误...
-
不,我给出的示例运行良好。但是,当我对我的文件执行此操作时,它会出现这些错误。
-
http://stackoverflow.com/questions/24280263/merging-multiple-text-files-in-r-with-a-constraint。请参阅此中的第一个答案。我说的是这个。
标签: r