Two seemingly equal ways of changing column names for R data.frame - only the other one works

后端 未结 2 996
醉梦人生
醉梦人生 2021-01-26 04:25

I have a dataframe and I need to add suffix to some of the variable names. In my case that\'s all numerical variables after spreading a variable to wide format. Could someone ex

2条回答
  •  清歌不尽
    2021-01-26 04:51

    You are subsetting your df, in essence creating a second data.frame, and renaming it. This does not reflect on your original data.frame.

    colnames(df[,2:ncol(df)]) <- paste0(names(df[, 2:ncol(df)]), "_X")
    

    would be equal to

    df2 <- df[,2:ncol(df)]
    colnames(df2) <- paste0(names(df[, 2:ncol(df)]), "_X")
    
    > df
      ID var1 var2 var3
    1 id    1    2    3
    > df2
      var1_X var2_X var3_X
    1      1      2      3
    

    The correct way would be

    colnames(df)[2:ncol(df)] <- paste0(names(df[, 2:ncol(df)]), "_X")
    

    or using sprintf

    colnames(df)[2:ncol(df)] <- sprintf("%s_X", names(df)[2:ncol(df)])
    

提交回复
热议问题