通过与r

时间:2015-05-18 16:33:00

标签: r

我想找出从第4列到最后一列的每个字符串的唯一元素。每个字符串都有两个用" /"分隔的元素。添加一列" Alt"在" REF"中保留任何非字符值的独特元素列,但" - "字符不计算在内。添加另一列" Num"保持独特元素的数量。 数据帧:

CHROM   POS REF sample1 sample2 sample3 sample4 sample5
Chr20   84  C   C/C C/G C/A C/C C/C
Chr20   102 TAA TAA/TAA TAA/TAA TAA/TA  TAA/TAA TA/TA
Chr20   104 ACCCCC  ACCCCC/ACCCCCC  ACCCCCC/ACCCCCC ACCCCC/ACCCCC   ACCCCC/ACCCCC   ACCCCC/ACCCCC
Chr20   109 C   C/T C/T -/- C/T C/C
Chr20   118 AT  A/AT    A/A AT/AT   AT/ATT  AT/T

预期的结果:

CHROM   POS REF sample1 sample2 sample3 sample4 sample5 Alt Num
Chr20   84  C   C/C C/G C/A C/C -/- A,G 2
Chr20   102 TAA TAA/TAA TAA/TAA TAA/TA  TAA/TAA TA/TA   TA  1
Chr20   104 ACCCCC  ACCCCC/ACCCCCC  ACCCCCC/ACCCCCC ACCCCC/ACCCCC   ACCCCC/ACCCCC   ACCCCC/ACCCCC   ACCCCCC 1
Chr20   109 C   C/T C/T -/- C/T C/C T   1
Chr20   118 AT  A/AT    A/A AT/AT   AT/ATT  AT/T    A,ATT,T 3

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您可以使用applyMARGIN=1来循环遍历行。我们可以通过'/'(strsplit(...))拆分元素,将'REF'与'sample'进行比较,并提取不匹配的元素。

df1[c('Alt', 'Num')] <-  t(apply(df1[-(1:2)],1, FUN=function(x) {
                    x1 <- unlist(strsplit(x[2:length(x)], '/'))
                    x2 <- unlist(strsplit(x[1], '/'))
                    x3 <- unique(x1[!x1 %in% c(x2,'-')])
                    c(toString(x3), length(x3))}))
df1$Num <- as.numeric(df1$Num)
df1
#  CHROM POS    REF        sample1         sample2       sample3       sample4
#1 Chr20  84      C            C/C             C/G           C/A           C/C
#2 Chr20 102    TAA        TAA/TAA         TAA/TAA        TAA/TA       TAA/TAA
#3 Chr20 104 ACCCCC ACCCCC/ACCCCCC ACCCCCC/ACCCCCC ACCCCC/ACCCCC ACCCCC/ACCCCC
#4 Chr20 109      C            C/T             C/T           -/-           C/T
#5 Chr20 118     AT           A/AT             A/A         AT/AT        AT/ATT
#        sample5       Alt Num
#1           C/C      G, A   2
#2         TA/TA        TA   1
#3 ACCCCC/ACCCCC   ACCCCCC   1
#4           C/C         T   1
#5          AT/T A, ATT, T   3

数据

df1 <- structure(list(CHROM = c("Chr20", "Chr20", "Chr20", "Chr20", 
"Chr20"), POS = c(84L, 102L, 104L, 109L, 118L), REF = c("C", 
"TAA", "ACCCCC", "C", "AT"), sample1 = c("C/C", "TAA/TAA", "ACCCCC/ACCCCCC", 
"C/T", "A/AT"), sample2 = c("C/G", "TAA/TAA", "ACCCCCC/ACCCCCC", 
"C/T", "A/A"), sample3 = c("C/A", "TAA/TA", "ACCCCC/ACCCCC", 
"-/-", "AT/AT"), sample4 = c("C/C", "TAA/TAA", "ACCCCC/ACCCCC", 
"C/T", "AT/ATT"), sample5 = c("C/C", "TA/TA", "ACCCCC/ACCCCC", 
"C/C", "AT/T")), .Names = c("CHROM", "POS", "REF", "sample1", 
"sample2", "sample3", "sample4", "sample5"), class = "data.frame", 
row.names = c(NA, -5L))