Home > Back-end >  Creating a loop that full jon dataframes in R
Creating a loop that full jon dataframes in R

Time:04-01

I have six dataframes: A2020, B2020, A2021, B2021, A2022, and B2022.

All six dataframes have a common key variable called "id".

I want to do three full_join:

  • A2020 and B2020 using "id" as key variable
  • A2021 and B2021 using "id" as key variable
  • A2022 and B2022 using "id" as key variable

As result, I would have three dataframes: AB2020, AB2021, AB2022

I tried this, but R doesn't recognize A or B dataframes:

for(i in c(2020:2022) {
    A <- get(A[[i]])
    B <- get(B[[i]])
    tmp <- full_join(A,B,by="id")
    assign(paste("AB",i,sep = ""),tmp)
}

CodePudding user response:

How about A <- get(paste0("A", i)

inside the loop instead of A <- get(A[[i]])

  • Related