r - 如何在R中将两组数据融合成两列?

标签 r data.table melt

我有以下数据。我想融化一切No_of.reads一列和所有列_contamination_另一列中的列。所以最终的数据框将有 diluted_sample , No_of_reads_contamination_列。我尝试分两步进行,但这会让我重复观察。正确的做法是什么?

代码:

test.dput.melted <- melt(test.dput, id = 1:3, measure = 4:7)
test.dput.melted <- melt(test.dput.melted, id = c(1,4,5), measure = 2:3)

数据:

 test.dput<- structure(list(diluted_sample = c("100%", "95%", "90%", "85%", 
"80%", "75%"), No_of_reads_from_NA12878 = c("15,000,000", "14,250,000", 
"13,500,000", "12,750,000", "12,000,000", "11,250,000"), No_of_reads_from_NA12877 = c("0", 
"750,000", "1,500,000", "2,250,000", "3,000,000", "3,750,000"
), tEst_contamination_of_NA12878 = c("99.60%", "99.10%", "96.80%", 
"92.60%", "88%", "82.60%"), pair_contamination_of_NA12878 = c("100.00%", 
"94.15%", "88.72%", "83.36%", "78.20%", "73.08%"), tEst_contamination_of_NA12877 = c("0.10%", 
"7%", "13.60%", "20.10%", "26.20%", "32.10%"), pair_contamination_of_NA12877 = c("0.10%", 
"5.21%", "10.50%", "15.85%", "20.92%", "26.04%")), .Names = c("diluted_sample", 
"No_of_reads_from_NA12878", "No_of_reads_from_NA12877", "tEst_contamination_of_NA12878", 
"pair_contamination_of_NA12878", "tEst_contamination_of_NA12877", 
"pair_contamination_of_NA12877"), row.names = c(NA, 6L), class = "data.frame")

最佳答案

自从您标记了 data.tablemelt

library(magrittr)
library(data.table)
setDT(test.dput)


n.reads <- 
  test.dput[, grep('diluted|reads', names(test.dput)), with = F] %>% 
    melt(1, variable.name = 'Which_No_of_reads',
            value.name    = 'No_of_reads') %>% 
    .[, Which_No_of_reads := gsub('No_of_reads_from_', '', Which_No_of_reads)]

contam <- 
  test.dput[, grep('diluted|contamination', names(test.dput)), with = F] %>% 
    melt(1, variable.name = 'Which_contamination',
            value.name    = '_contamination_') %>% 
    .[, Which_contamination := gsub('contamination_of_', '', Which_contamination)]

cbind(n.reads, contam) %>% 
  .[, unique(names(.)), with = F]

#    diluted_sample Which_No_of_reads No_of_reads Which_contamination _contamination_
#  1:           100%           NA12878  15,000,000        tEst_NA12878          99.60%
#  2:            95%           NA12878  14,250,000        tEst_NA12878          99.10%
#  3:            90%           NA12878  13,500,000        tEst_NA12878          96.80%
#  4:            85%           NA12878  12,750,000        tEst_NA12878          92.60%
#  5:            80%           NA12878  12,000,000        tEst_NA12878             88%
#  6:            75%           NA12878  11,250,000        tEst_NA12878          82.60%
#  7:           100%           NA12877           0        pair_NA12878         100.00%
#  8:            95%           NA12877     750,000        pair_NA12878          94.15%
#  9:            90%           NA12877   1,500,000        pair_NA12878          88.72%
# 10:            85%           NA12877   2,250,000        pair_NA12878          83.36%
# 11:            80%           NA12877   3,000,000        pair_NA12878          78.20%
# 12:            75%           NA12877   3,750,000        pair_NA12878          73.08%
# 13:           100%           NA12878  15,000,000        tEst_NA12877           0.10%
# 14:            95%           NA12878  14,250,000        tEst_NA12877              7%
# 15:            90%           NA12878  13,500,000        tEst_NA12877          13.60%
# 16:            85%           NA12878  12,750,000        tEst_NA12877          20.10%
# 17:            80%           NA12878  12,000,000        tEst_NA12877          26.20%
# 18:            75%           NA12878  11,250,000        tEst_NA12877          32.10%
# 19:           100%           NA12877           0        pair_NA12877           0.10%
# 20:            95%           NA12877     750,000        pair_NA12877           5.21%
# 21:            90%           NA12877   1,500,000        pair_NA12877          10.50%
# 22:            85%           NA12877   2,250,000        pair_NA12877          15.85%
# 23:            80%           NA12877   3,000,000        pair_NA12877          20.92%
# 24:            75%           NA12877   3,750,000        pair_NA12877          26.04%

关于r - 如何在R中将两组数据融合成两列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52908289/

相关文章:

r - 如何计算一个元素在 data.table 中连续出现的次数?

r - 交易日期前6个月的总金额

卷起一个 data.table

r - 使用 reshape2 对数据框的第一列重新排序

RODBC 函数和错误/警告

r - 使用逻辑从现有多列数据帧生成新的多列数据帧

r - 使用quadProg库进行约束二次优化

r - 如何使用 dplyr 熔化和类型转换数据框?

R - 熔化两个地址列并类型转换为新列

r - 使用来自其他数据集的两列中的字符串对大数据集进行子集