0

I have multiple data sets that are the same column structure but different row lengths. When I go to stack them, there is an occasional problem with rbind as one file may have the column names uppercase whereas another may have lowercase. I would like to run something at the beginning that converts everything to lowercase (as this is not case sensitive).

When I do the following one-off, the conversion works. However, I am messing something up trying to do this through a loop.

This works

names(fmli3) <- tolower(names(fmli3))

Does not work

df_qtr <- c(fmli1, fmli2, fmli3, fmli4, fmli5)

for (i in 1:length(df_qtr)){
  names(df_qtr[[i]]) <- tolower(names(df_qtr[[i]]))
}

Thanks in advance for the help.

EDIT: Thanks to JWilliman answer in the cross post (and Shea for pointing out), the following works:

dfs <- c("fmli1", "fmli2", "fmli3", "fmli4", "fmli5")

for(df in dfs) {
  df.tmp <- get(df)
  names(df.tmp) <- tolower(names(df.tmp)) 
  assign(df, df.tmp)
}
Community
  • 1
  • 1
Jebediah15
  • 754
  • 3
  • 18
  • 39
  • Possible duplicate of [Rename columns in multiple dataframes, R](http://stackoverflow.com/questions/18375969/rename-columns-in-multiple-dataframes-r) – shea Mar 07 '16 at 21:33

1 Answers1

0

The problem with original code is that df_qtr is not a list of 5 data.frame. You can check length(df_qtr) to verify.

Use the following syntax to connect multiple data.frame into a list.

 df_qtr <- list( fmli1, fmli2, ...)
thekingofkings
  • 75
  • 1
  • 11