R数据框,按名称组合列并创建新列

时间:2013-12-20 20:18:09

标签: r

我的问题是数据存储在列名中。 NAME_X_Y_OTHER。我需要将多个列合并为一个,但我需要保留XY值。

原始数据看起来像这样。

my_df <- data.frame(MISC=c("a","a","b"),
                NAME_10_03_OTHER=c(1,2,3),
                NAME_10_04_OTHER=c(4,5,6),
                NAME_11_04_OTHER=c(7,8,9))


  MISC NAME_10_03_OTHER NAME_10_04_OTHER NAME_11_04_OTHER
1    a                1                4                7
2    a                2                5                8
3    b                3                6                9

我想将其改为此。

my_new_df <- data.frame(MISC=c("a","a","b","a","a","b","a","a","b"),
                    NAME_OTHER=c(1,2,3,4,5,6,7,8,9),
                    x=c(10,10,10,10,10,10,11,11,11),
                    y=c(3,3,3,3,3,3,4,4,4))
  MISC NAME_OTHER  x y
1    a          1 10 3
2    a          2 10 3
3    b          3 10 3
4    a          4 10 3
5    a          5 10 3
6    b          6 10 3
7    a          7 11 4
8    a          8 11 4
9    b          9 11 4

我可以将列与c(t(my_df))合并,但我会丢失XY值。

2 个答案:

答案 0 :(得分:1)

my_df <- data.frame(MISC=c("a","a","b"),
                    NAME_10_03_OTHER=c(1,2,3),
                    NAME_10_04_OTHER=c(4,5,6),
                    NAME_11_04_OTHER=c(7,8,9))
#require(reshape2)
mydf2<-melt(my_df)
mydf2$x=as.numeric(unlist(lapply(strsplit(as.character(mydf2$variable),"_", fixed=T),"[[",2)))
mydf2$y=as.numeric(unlist(lapply(strsplit(as.character(mydf2$variable),"_", fixed=T),"[[",3)))
  

mydf2

  MISC         variable value  x y
1    a NAME_10_03_OTHER     1 10 3
2    a NAME_10_03_OTHER     2 10 3
3    b NAME_10_03_OTHER     3 10 3
4    a NAME_10_04_OTHER     4 10 4
5    a NAME_10_04_OTHER     5 10 4
6    b NAME_10_04_OTHER     6 10 4
7    a NAME_11_04_OTHER     7 11 4
8    a NAME_11_04_OTHER     8 11 4
9    b NAME_11_04_OTHER     9 11 4

答案 1 :(得分:1)

您可以使用以下代码:

s = "NAME_10_03_OTHER"
s1 = unlist(strsplit(s, split='_', fixed=TRUE))[2]
s2 = unlist(strsplit(s, split='_', fixed=TRUE))[3]