buhtz buhtz - 2 months ago 7x
R Question

Add an element to a named list of data.frames in R

I am new to R and don't know the correct term for a list of that kind. I would call it a element named list of data frames.

dfA = data.frame(a=c(1,2,3), b=c(1,2,3))
dfB = data.frame(a=c(1,2,3), b=c(1,2,3))
mylist = list ('a' = dfA, 'b' = dfB)

But now I want to add a new element to it

dfC = data.frame(a=c(1,2,3), b=c(1,2,3))

I don't know how to do this. I couldn't find examples fitting in my use case.


We can assign 'dfC' by either numeric index i.e. 3 or with a name

mylist[['c']] <- dfC

Or use c or append

mylist <- c(mylist, list(c= dfC))