简体   繁体   English

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

[英]R Data Frame, combine columns by name and create new column

My issue is that data is stored in the column names. 我的问题是数据存储在列名称中。 NAME_X_Y_OTHER . NAME_X_Y_OTHER I need to combine multiple columns into one, but I need to keep the X and Y values. 我需要将多列合并为一列,但需要保留XY值。

The original data would look like this. 原始数据将如下所示。

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

I would like to transform it to this. 我想将其转换为此。

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

I can combine the columns with c(t(my_df)) , but I lose the X and Y values. 我可以将列与c(t(my_df)) ,但会丢失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))
#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 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

You can make use of the below code: 您可以使用以下代码:

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

暂无
暂无

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

相关问题 如何在R数据帧中的列之间循环并在每次迭代中使用列名创建新的数据帧? - How to loop through the columns in an R data frame and create a new data frame using the column name in each iteration? 从 R 数据框中的两列创建新的向量列 - Create new column of vectors from two columns in R data frame 汇总R数据框中的字符列以创建新列 - Summing columns of characters in R data frame to create a new column R合并数据帧列并转置为新数据帧 - R combine data frame columns and transpose into new data frame 将数据框中的多个列与向量进行比较,并将新列添加到数据框中,指示 R 中匹配的 cloumn 名称 - compare multiple columns in a data frame with vector and add new column to the data frame indicating the matched cloumn name in R 根据 R 中的列名创建一个包含来自另一个数据框中的列的新数据框 - create a new data frame with columns from another data frame based on column names in R 如何组合列的成员,将它们收集在数据框中并给它们一个新名称,在 R 中? - How to combine member of a column, collect them in a data frame and give them a new name, in R? R在数据框中合并2列 - R combine 2 columns in data frame 如何将多个字符列合并为 R 数据框中的单个列 - How to combine multiple character columns into a single column in an R data frame 如何将r数据帧的多个列合并为列表的单个列 - How to combine multiple columns of an r data frame into a single column that is a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM