2

I have a list of data frames. All data frames in the list have the same variables. I have another data frame out side of the list. This data frame also has the same variables. How do I combine the data frame that are not in the list with all the individual data frames in the list?

Example: In the list my_list there is some data frames df1, df2, df3 etc. Not in the list there is another data frame dfX. All the data frames have the same variables. How do I combine dfX to all the df's in my_list. dfX should copied and added to all the data frames in my_list.

I suppose I need a for loop and rbind, but I can't seem to get it right. I'm not used to working with lists (and quite new to R in general). Can anyone please help me?

2 Answers 2

2

Create some dummy data:

foo <- list(df1=data.frame(A=rnorm(3),B=rnorm(3)),
    df2=data.frame(A=rnorm(3),B=rnorm(3)),
    df3=data.frame(A=rnorm(3),B=rnorm(3)))
dfX <- data.frame(A=rnorm(3),B=rnorm(3))

Then use lapply():

lapply(foo,function(xx)rbind(xx,dfX))
Sign up to request clarification or add additional context in comments.

1 Comment

Simple solution, as I expected. Thank you
1

Another option would be (@Stephan Kolassa's data)

  Map(`rbind`, foo,list(dfX))

Testing the results

 res1 <- lapply(foo,function(xx)rbind(xx,dfX))
 res2 <-  Map(`rbind`, foo,list(dfX))
 identical(res1, res2)
 #[1] TRUE

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.