Rename columns in multiple dataframes, R

前端 未结 3 1666
滥情空心
滥情空心 2020-12-16 19:34

I am trying to rename columns of multiple data.frames.

To give an example, let\'s say I\'ve a list of data.frames dfA, d

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-16 20:02

    I had the problem of importing a public data set and having to rename each dataframe and rename each column in each dataframe to trim whitespaces, lowercase, and replace internal spaces with periods.

    Combining the above methods got me:

    for (eachdf in dfs)
      df.tmp <- get(eachdf) 
        for (eachcol in 1:length(df.tmp))
          colnames(df.tmp)[eachcol] <-
          str_trim(str_to_lower(str_replace_all(colnames(df.tmp)[eachcol], " ", ".")))
          }
      assign(eachdf, df.tmp) 
    }
    

提交回复
热议问题