r Remove parts of column name after certain characters

后端 未结 3 461
既然无缘
既然无缘 2020-12-30 07:55

I have a large data set with thousands of columns. The column names include various unwanted characters as follows:

col1_3x_xxx
col2_3y_xyz
col3_3z_zyx
         


        
3条回答
  •  一生所求
    2020-12-30 08:08

    certainly late for this answer, but just in case someone is looking for a solution

    colnames(df1)[col] <-  sub("_3.*", "", colnames(df1)[col])
    

    And if you have multiple columns :

    for ( col in 1:ncol(df1)){
        colnames(df1)[col] <-  sub("_3.*", "", colnames(df1)[col])
    }
    

提交回复
热议问题