Rename columns of a data frame by searching column name

后端 未结 4 1928
南旧
南旧 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:08

    hmm, this might be way to complicated, but the first that come into my mind:

    lookup <- data.frame(search = c(col3_search,col2_search,col1_search),
                         replace = c(col3_replace,col2_replace,col1_replace))
    
    colnames(df) <- lookup$replace[match(lookup$search, colnames(df))]
    

提交回复
热议问题