2

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 Answers1

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]} )
crogg01
  • 2,446
  • 15
  • 35