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

标签 r dataframe

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

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/

相关文章:

r - 如何在 R 中匹配彼此在 +/- 5 以内的观察值?

r - 从 party 包中的回归树中获取节点的统计信息

python - 通过绘制县等值区域图添加更多关于悬停的数据

python - 使用 pandas python 中其他数据帧的值覆盖数据帧中的值

R - 有条件地汇总所有可能的列对中的数据

python - 如何在 matplotlib 中创建同一条线有两种不同颜色的折线图?

r - 粘贴命名向量 R

r - 成对二进制比较 - 优化 R 中的代码

python - Pandas 转换 float 删除指数

arrays - 数组切片上的矢量化和