Hi, Let us say that we have some data frames named S1,S2,S3... that I want to join independently by column names to dataframe X. This is how they look like:
S1 <- data.frame(
values = c (1:5),
numbers = c(11:15))
S1
S2 <- data.frame(
values = c (1:5),
numbers = c(11:15))
S2
S3 <- data.frame(
values = c (1:5),
numbers = c(16:20))
S3
#Dataframe X
X <- data.frame(
values = c (26:30),
numbers = c(501:505))
X
S1
S2
S3
X
Using rbind, it is really easy to merge/join them according to column names. As below:
S1_X<- rbind(S1,X)
S1_X
S2_X<- rbind(S2,X)
S2_X
S3_X<- rbind(S3,X)
S3_X
Now the problem is that I have 500 plus data frames (S1,S2,S3,S4,S5.....S500) that I want to join to dataframe "X" by any function. For example rbind. Is it possible to use some loop/function for that.
What I have best tried is create a list (nested list of dataframes) that I can later split into individual dataframes:
output = lapply(list(S1=S1,S2=S2,S3=S3),rbind,X)
The output is what I expected. So it works The only problem is, how do I specify S1 to S500?
Thank you in advance!
1 answer
lapply(paste0("S", seq_len(500)), \(y) rbind(get(y), X))
Also, how did you end up with 500 data.frames assigned to distinct variables? Generally for problems such as this in R a list would be the proper data type. It may be worth reevaluating your code up to this point to avoid assigning everything to separate variables.
Log in to answer this question.