Rename columns of a data frame by searching column name

后端 未结 4 1876
南旧
南旧 2021-02-06 14:44

I am writing a wrapper to ggplot to produce multiple graphs based on various datasets. As I am passing the column names to the function, I need to rename the column names so tha

4条回答
  •  你的背包
    2021-02-06 15:15

    I second @justin's aes_string suggestion. But for future renaming you can try.

    require(stringr)
    df <- data.frame(col1=1:3,col2=3:5,col3=6:8)
    oldNames <- c("col1", "col2", "col3")
    newNames <- c("new_col1", "new_col2", "new_col3")
    names(df) <- str_replace(string=names(df), pattern=oldNames, replacement=newNames)
    

提交回复
热议问题