A more efficient way to strip a character from column names in an R dataframe?

Viewed 65

I want to strip the X's from the column names in this R data frame.

> d
     days X2000 X2001 X2002 X2003
1 June-01    90    85    88    75
2 June-02    93    84    88    81
3 June-03    94    83    85    83

Here's what I've got so far. It gets me the column names that I want, by taking the substring of each of the columns 2 through 5.

> new_colnames <- c()
> for (name in colnames(d[2:5])) { new_colnames <- c(new_colnames, substring(name, 2)) }
> colnames(d) <- c('days', new_colnames)
> d
     days 2000 2001 2002 2003
1 June-01   90   85   88   75
2 June-02   93   84   88   81
3 June-03   94   83   85   83

Is there a more efficient way of doing this? What is "best practice"?

1 Answers

As someone already suggested above, you just need to use the sub command. I recreated your database above and did just this:

# Load piping library:
library(tidyverse)

# Create days variable:
days <- c("June-01",
          "June-02",
          "June-03")

# Create year variables:
X2000 <- c(90,93,94)
X2001 <- c(85,84,83)
X2002 <- c(88,88,85)
X2003 <- c(75,81,83)

# Make data frame with variables:
df <- data.frame(days,
           X2000,
           X2001,
           X2002,
           X2003)

At this point you should have the same data frame as what you have above:

    Xdays X2000 X2001 X2002 X2003
1 June-01    90    85    88    75
2 June-02    93    84    88    81
3 June-03    94    83    85    83

From here you just need to change the names:

# Change names:
names(df) <- sub("X", "", names(df))

# Print:
df

This should print this new table:

     days 2000 2001 2002 2003
1 June-01   90   85   88   75
2 June-02   93   84   88   81
3 June-03   94   83   85   83
Related