您有 2 个解决方案。你可以
(a) 分配给一个新列表newList = lapply(scenbase, function(x) { x[33:152,,drop=F]} )
(b) 使用<<- 运算符将在lapply(1:length(scenbase), function(x) { scenbase[[x]] <<- scenbase[[x]][33:152,,drop=F]} ) 处分配修剪后的数据。
您的呼叫不起作用,因为i 不在全局范围内。您可以通过调用<<- 运算符来解决这个问题,该运算符分配给它在连续父环境中找到的第一个变量。或者通过创建一个新的修剪列表。
这是一些重现解决方案 (a) 的代码:
listOfDfs = list()
for(i in 1:10) { listOfDfs[[i]] = data.frame("x"=sample(letters,200,replace=T),"y"=sample(letters,200,replace=T)) }
choppedList = lapply(listOfDfs, function(x) { x[33:152,,drop=F]} )
这是一些重现解决方案 (b) 的代码:
listOfDfs = list()
for(i in 1:10) { listOfDfs[[i]] = data.frame("x"=sample(letters,200,replace=T),"y"=sample(letters,200,replace=T)) }
lapply(1:length(listOfDfs), function(x) { listOfDfs[[x]] <<- listOfDfs[[x]][33:152,,drop=F]} )