Search code examples
rloopsr-colnames

I want to change the columns names with a loop


i have a datasets column names looking like that

state.abb, state.area, state.division, state.region

i want to change the names of the columns and delete the "state." part to leave only "abb", "area","division", and "region". i wrote this code using a loop over the df columns using substr func but it doesn't work nor give errors. what's wrong with it please ?


    for(e in 1:ncol(df)){
      colnames(df[e])<-substring(colnames(df[e]),7)
    }


Solution

  • Here, we can change the colnames(df[e]) to colnames(df)[e]

    for(e in seq_along(df)){
         colnames(df)[e] <- substring(colnames(df)[e],7)
    }
    

    substring is vectorized so we could directly do this without any for loop

    colnames(df) <- substring(colnames(df), 7)
    

    Also, if we are removing the prefix including the ., a generalized option assuming that the prefix can be of any length is sub

    colnames(df) <- sub(".*\\.", "", colnames(df))
    

    An an example,

    data(mtcars)
    colnames(mtcars[1]) <- "hello"
    colnames(mtcars[1])
    #[1] "mpg" # no change
    colnames(mtcars)[1] <- "hello"
    colnames(mtcars[1])
    #[1] "hello" # changed