【发布时间】:2017-10-28 10:02:24
【问题描述】:
我想从列表中删除部分,以将列表减少为具有一定数量列的元素。
这是我正在尝试做的一个虚拟示例:
#1: define the list
tables = list(mtcars,iris)
for(k in 1:length(tables)) {
# 2: be sure that each element is shaped as dataframe and not matrix
tables[[k]] = as.data.frame(tables[[k]])
# 3: remove elements that have more or less than 5 columns
if(ncol(tables[[k]]) != 5) {
tables <- tables[-k]
}
}
我尝试的另一个选项:
#1: define the list
tables = list(mtcars,iris)
for(k in 1:length(tables)) {
# 2: be sure that each element is shaped as dataframe
tables[[k]] = as.data.frame(tables[[k]])
# 3: remove elements that have more or less than 5 columns
if(ncol(tables[[k]]) != 5) {
tables[[-k]] <- NULL
}
}
我来了
表格中的错误[[k]]:下标超出范围。
是否有替代且正确的方法?
【问题讨论】: