0

I want a function that I can call several times throughout a data analysis script, each time appending a new data frame to an existing list.

myList <- list()

The function creates a new data frame after subsetting an existing data frame, and then appends this new data frame to my list (in theory).

appendList = function(){
  df = mydf[mydf$myData < 0.5, ]
  myList[[(length(myList)+1)]] <- df
}

In my real-world problem I have several different code chunks, each with a different set of data in the column 'myData'.

I thought I could just use my function above like this:

mydf <- data.frame(myData = runif(10))
appendList()

mydf <- data.frame(myData = rnorm(10))
appendList()

But my list remains unchanged:

length(myList)
>[1] 0

Is it an environment issue?

My goal is for 'myList' to contain all of these different data frames.

Bonus: Perhaps there is a better way to complete this kind of task?

BDA
  • 65
  • 1
  • 6

0 Answers0