简体   繁体   English

如何重命名部分数据框中的列名?

[英]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'.例如,“DRT (24)”将变成“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).此外,请注意,这些列只是我整个数据框的一部分(它们是第 62-99 列)。

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"

如果您只有 2 位数字,则可以使用substr

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

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"

An option with parse_number from readr来自readr带有parse_number的选项

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM