0

I created a function that joint specific column from 3 different data.frame(Ini, Aug_64, Dim_2). I want to repeat this operation 1259 times. I don’t know how to create this automatically. I found in some of my research that I should use replicate but I don’t know how to construct the code, because I want the value of Q to change each time.I was also wondering if there was a way to save the data.frame created a swell automatically at the same time.

All my data.frame are 83 rows by 1259 columns.

Here is the function I created.

tr4<-function(Q){
left_join(select(Ini, c("ID",Q)), select(Aug_64, c("ID",Q)), by="ID") %>%
   left_join(., select(Dim_2, c("ID",Q)), by="ID") }

write.csv(data.frame(tr4(53)), "/Users/T/Desktop/Rstudio/53", row.names = T)

2 Answers2

0

I am not sure if the code like below works for your case

for (k in 1:1259) {
  write.csv(data.frame(tr4(k)), paste0("/Users/T/Desktop/Rstudio/",k), row.names = T)
}
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
0

Along the lines of Thomas' answer, but including your full code-block, try this:

library(dplyr)

for (Q in as.character(1:1259)) {
  Ini %>%
    select(ID, .data[[Q]]) %>%
    left_join(
      Aug_64 %>% select(ID, .data[[Q]]),
      by = 'ID'
    ) %>%
    left_join(
      Dim_2 %>% select(ID, .data[[Q]]),
      by = 'ID'
    ) %>%
    write.csv(paste0('/Users/T/Desktop/Rstudio/',Q,'.csv'), row.names = T)
}

(I've also taken the freedom to untangle your slightly hard-to-read dplyr)

Moreover, without a reproducible example of your data or a clearer explanation, I can only assume how to select the correct variable from your three dataframes using Q - if the column name is Q, the above should work.

alex_jwb90
  • 1,663
  • 1
  • 11
  • 20