Using lapply to subset rows from data frames — incorrect number of dimensions error

馋奶兔 提交于 2019-12-13 16:12:08

问题


I have a list called "scenbase" that contains 40 data frames, which are each 326 rows by 68 columns. I would like to use lapply() to subset the data frames so they only retain rows 33-152. I've written a simple function called trim() (below), and am attempting to apply it to the list of data frames but am getting an error message. The function and my attempt at using it with lapply is below:

trim <- function(i) { (i <- i[33:152,]) }

lapply(scenbase, trim)

Error in i[33:152, ] : incorrect number of dimensions

When I try to do the same thing to one of the individual data frames (soil11base.txt) that are included in the list (below), it works as expected:

soil11base.txt <- soil11base.txt[33:152,]

Any idea what I need to do to get the dimensions correct?


回答1:


You have 2 solutions. You can either

(a) assign to a new list newList = lapply(scenbase, function(x) { x[33:152,,drop=F]} )

(b) use the <<- operator will assign your trimmed data in place lapply(1:length(scenbase), function(x) { scenbase[[x]] <<- scenbase[[x]][33:152,,drop=F]} ).

Your call does not work because the i is not in the global scope. You can work your way around that by using calls to the <<- operator which assigns to the first variable it finds in successive parent environments. Or by creating a new trimmed list.

Here is some code that reproduces solution (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]} )

Here is some code that reproduces solution (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]} )


来源:https://stackoverflow.com/questions/21247625/using-lapply-to-subset-rows-from-data-frames-incorrect-number-of-dimensions-e

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!