buhtz buhtz - 1 year ago 105
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.

Answer Source

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))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download