gpt4 book ai didi

r - 使用另一个表中的数据将列添加到表中

转载 作者:行者123 更新时间:2023-12-04 01:37:52 29 4
gpt4 key购买 nike

我有一张如下所示的表格:

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

我有另一个带有描述的表,想添加一个带有基因的列。例如表 2 将是:
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

我想向名为“Genes”的 Table2 添加另一列,该列与两个表中的“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”列并将数据集从“wide”格式转换为“long”格式。输出将是 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

关于r - 使用另一个表中的数据将列添加到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30083345/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com