我有一个表,如下所示:
Table1 <- data.frame(
"Random" = c("A", "B", "C"),
"Genes" = c("Apple", "Candy", "Toothpaste"),
"Extra" = c("Up", "", "Down"),
"Desc" = c("Healthy,Red,Fruit", "Sweet,Cavities,Sugar,Fruity", "Minty,Dentist")
)
给予:
Random Genes Extra Desc
1 A Apple Up Healthy,Red,Fruit
2 B Candy Sweet,Cavities,Sugar,Fruity
3 C Toothpaste Down Minty,Dentist
我有另一个带有说明的表,并想添加带有基因的列。例如,Table2将是:
Table2 <- data.frame(
"Col1" = c(1, 2, 3, 4, 5, 6),
"Desc" = c("Sweet", "Sugar", "Dentist", "Red", "Fruit", "Fruity")
)
给予:
Col1 Desc
1 1 Sweet
2 2 Sugar
3 3 Dentist
4 4 Red
5 5 Fruit
6 6 Fruity
我想在Table2中添加另一列名为“ Genes”的列,该列与两个表中的“ Desc”匹配,并从Table1中添加Genes以获得:
Col1 Desc Gene
1 1 Sweet Candy
2 2 Sugar Candy
3 3 Dentist Toothpaste
4 4 Red Apple
5 5 Fruit Apple
6 6 Fruity Candy
您可以尝试cSplit
从中splitstackshape
拆分“ Table1”中的“ Desc”列,并将数据集从“宽”格式转换为“长”格式。输出将是data.table
。我们可以使用以下data.table
方法将键列设置为“ Desc”(setkey
),与“ Table2”联接,最后通过选择列或将:=
不需要的列分配为空值()来删除输出中不需要的列
library(splitstackshape)
setkey(cSplit(Table1, 'Desc', ',', 'long'),Desc)[Table2[2:1]][
,c(5,4,2), with=FALSE]
# Col1 Desc Genes
#1: 1 Sweet Candy
#2: 2 Sugar Candy
#3: 3 Dentist Toothpaste
#4: 4 Red Apple
#5: 5 Fruit Apple
#6: 6 Fruity Candy
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句