887
887

Reputation: 619

How to rename column names in part of data frame?

I'm working with the following data frame:


structure(list(`DLT (16)` = c(1L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 
1L, 1L), `DLT (17)` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), `DLT (18)` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), `DLT (19)` = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), `DLT (20)` = c(0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L), `DRT (24)` = c(0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L), `DRT (25)` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L), `DRT (26)` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L), `DRT (27)` = c(0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L), 
    `DRT (28)` = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L), `LE (13)` = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), `LE (14)` = c(0L, 0L, 
    0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), `LE (15)` = c(0L, 0L, 1L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L), `LEO (01)` = c(0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L), `LEO (04)` = c(0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L), `LEO (05)` = c(0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L), `LEO (06)` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L), `LEO (07)` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L), `LEO (08)` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L), `LEO (09)` = c(0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L
    ), `LEO (10)` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    `LEO (11)` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), `LEO (12)` = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), `NLT (21)` = c(0L, 0L, 
    0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L), `NRT (23)` = c(0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L), `NT (22)` = c(1L, 1L, 1L, 1L, 
    1L, 1L, 0L, 1L, 1L, 0L), `RE (29)` = c(1L, 0L, 0L, 0L, 0L, 
    0L, 0L, 1L, 1L, 1L), `RE (30)` = c(0L, 1L, 1L, 1L, 0L, 0L, 
    0L, 0L, 0L, 0L), `RE (31)` = c(0L, 0L, 0L, 0L, 0L, 1L, 0L, 
    0L, 0L, 0L), `REO (32)` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L), `REO (33)` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L), `REO (34)` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
    ), `REO (35)` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    `REO (36)` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), `REO (37)` = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), `REO (38)` = c(0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), `REO (39)` = c(0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L), `REO (40)` = c(0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L)), row.names = c(2L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L), class = "data.frame")

I'm looking for a way to re-name the column names. I would like the column names to contain just the number, and eliminate the letters, space, and parentheses. For example, 'DRT (24)' would become just '24'. I have tried this code:

colnames(dat) = gsub("DLT", "", colnames(dat))

But it does not work. Additionally, please note that these columns are only part of my overall dataframe (they are columns 62-99).

Upvotes: 0

Views: 45

Answers (4)

akrun
akrun

Reputation: 886928

An option with parse_number from readr

library(dplyr)
df <-  df %>%
          rename_all(~ as.character(readr::parse_number(.)))

names(df)
#[1] "16" "17" "18" "19" "20" "24" "25" "26" "27" "28" "13" "14" "15" "1"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12"
#[24] "21" "23" "22" "29" "30" "31" "32" "33" "34" "35" "36" "37" "38" "39" "40"

Upvotes: 0

Karthik S
Karthik S

Reputation: 11584

Does this work:

> names(df)
 [1] "DLT (16)" "DLT (17)" "DLT (18)" "DLT (19)" "DLT (20)" "DRT (24)" "DRT (25)" "DRT (26)" "DRT (27)" "DRT (28)" "LE (13)"  "LE (14)" 
[13] "LE (15)"  "LEO (01)" "LEO (04)" "LEO (05)" "LEO (06)" "LEO (07)" "LEO (08)" "LEO (09)" "LEO (10)" "LEO (11)" "LEO (12)" "NLT (21)"
[25] "NRT (23)" "NT (22)"  "RE (29)"  "RE (30)"  "RE (31)"  "REO (32)" "REO (33)" "REO (34)" "REO (35)" "REO (36)" "REO (37)" "REO (38)"
[37] "REO (39)" "REO (40)"
> names(df) <- gsub('.*\\((\\d+)\\)','\\1', names(df))
> names(df)
 [1] "16" "17" "18" "19" "20" "24" "25" "26" "27" "28" "13" "14" "15" "01" "04" "05" "06" "07" "08" "09" "10" "11" "12" "21" "23" "22" "29" "30"
[29] "31" "32" "33" "34" "35" "36" "37" "38" "39" "40"

Upvotes: 1

Duck
Duck

Reputation: 39585

Maybe try this:

#Code
names(df) <- gsub("[^0-9.-]", "", gsub('[[:punct:] ]+','',names(df)))

Output for names:

names(df)
 [1] "16" "17" "18" "19" "20" "24" "25" "26" "27" "28" "13" "14" "15" "01" "04" "05" "06" "07" "08"
[20] "09" "10" "11" "12" "21" "23" "22" "29" "30" "31" "32" "33" "34" "35" "36" "37" "38" "39" "40"

Upvotes: 0

Ricardo Semi&#227;o
Ricardo Semi&#227;o

Reputation: 4456

If you'll only have numbers with 2 digits, you can use substr:

colnames(df) = substr(colnames(df), nchar(colnames(df))-2, nchar(colnames(df))-1)

Upvotes: 0

Related Questions