0

I have 12 dataframes that I need to combine. It appears that they all have the same column names, but it's possible there are slight differences in spacing. I want to rename the columns in df 2 - 12 to the column name of df 1 so that they can properly full_join. Each dataframe has 7 columns. What is the best way to go about doing this?

  • 3
    `dflist <- lapply(dflist, \`names<-\`, names(dflist[[1]]))` (embedded backticks are intentional/necessary). This is assuming that the 12 frames are in a single `list`. If not, then just `names(df2) <- names(df1)` for each of your frames `df2` through `df12`. – r2evans Mar 04 '21 at 16:51
  • names(H_Bhiwani) <- names(H_Ambala) Haryana_2010 <- H_Ambala %>% bind_rows(H_Bhiwani) View(Haryana_2010) – Emma Williamson Mar 04 '21 at 17:03
  • When I do this is throws the error: New names: * NA -> ...8 * NA -> ...9 * NA -> ...10 * NA -> ...11 – Emma Williamson Mar 04 '21 at 17:03
  • Do you know why? – Emma Williamson Mar 04 '21 at 17:03
  • 1
    Emma Williamson, welcome to SO! I don't think anybody can help without knowing a little more about the structure of your frames. Please read from among https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. Then reduce this problem down to 2-3 frames that give you a problem, and include only a few rows and columns in order to reproduce the problem. Thanks! – r2evans Mar 04 '21 at 17:06

0 Answers0