将数字分组时将其用作列名

时间:2019-05-10 06:48:15

标签: r

这是我的样本数据;

mydata<-structure(list(x1 = c(0, 8.6, 11.2, 8.4, 0, 0), x2 = c(0, 0, 
7.8, 7.6, 1.2, 10.2), y1 = c(0, 0, 3.4, 21.4, 1.8, 1.4), y2 = c(7.8, 
7.6, 1.2, 10.2, 7, 0), z1 = c(0, 1.6, 7.6, 23.6, 3.2, 0), z2 = c(8.6, 
1.4, 0, 0, 0, 0)), .Names = c("x1", "x2", "y1", "y2", "z1", "z2"
), class = "data.frame", row.names = c(NA, -6L))


    x1   x2   y1   y2   z1  z2
1  0.0  0.0  0.0  7.8  0.0 8.6
2  8.6  0.0  0.0  7.6  1.6 1.4
3 11.2  7.8  3.4  1.2  7.6 0.0
4  8.4  7.6 21.4 10.2 23.6 0.0
5  0.0  1.2  1.8  7.0  3.2 0.0
6  0.0 10.2  1.4  0.0  0.0 0.0

使用以下代码,可以将列分组为xyz

grps <- unique(gsub("[0-9]", "", colnames(mydata)))
# [1] "x" "y" "z"

但是当我这样重命名列时

myd<-structure(list(X2005 = c(0, 8.6, 11.2, 8.4, 0, 0), X2005.1 = c(0, 
0, 7.8, 7.6, 1.2, 10.2), X2006 = c(0, 0, 3.4, 21.4, 1.8, 1.4), 
    X2006.1 = c(7.8, 7.6, 1.2, 10.2, 7, 0), X2007 = c(0, 1.6, 
    7.6, 23.6, 3.2, 0), X2007.1 = c(8.6, 1.4, 0, 0, 0, 0)), .Names = c("X2005", 
"X2005.1", "X2006", "X2006.1", "X2007", "X2007.1"), row.names = c(NA, 
6L), class = "data.frame")


  X2005 X2005.1 X2006 X2006.1 X2007 X2007.1
1   0.0     0.0   0.0     7.8   0.0     8.6
2   8.6     0.0   0.0     7.6   1.6     1.4
3  11.2     7.8   3.4     1.2   7.6     0.0
4   8.4     7.6  21.4    10.2  23.6     0.0
5   0.0     1.2   1.8     7.0   3.2     0.0
6   0.0    10.2   1.4     0.0   0.0     0.0

我想看看;

# [1] "2005" "2006" "2007"

2 个答案:

答案 0 :(得分:1)

一种选择是使用sub并根据需要使用factor将名称转换为labels

names(mydata) <- factor(sub("[0-9]", "", names(mydata)), labels = 2005:2007)

然后检查您的列名

names(mydata)
#[1] "2005" "2005" "2006" "2006" "2007" "2007"

答案 1 :(得分:1)

我们可以使用gsub来匹配字符串开头(^的字母'X'或(| .的末尾加上数字($)替换为空白(""

names(myd) <- gsub("^X|\\.\\d+$", "", names(myd))
names(myd)
#[1] "2005" "2005" "2006" "2006" "2007" "2007"

unique(names(myd))
#[1] "2005" "2006" "2007"

如果我们知道位数和位置,那么substr会更快

substr(names(myd), 2, 5)