从字符矩阵计算一个值

时间:2018-03-07 23:01:35

标签: r

我有一个7358444行和110列的矩阵。矩阵由caracter矢量组成,如下所示:

GSL

我需要计算每个样本(具有模式eQTL的列)来自allele1的剂量。这可以使用每个列中第二个install.packages("ggstatsplot")之后的GP值来计算。我需要应用的公式是 FORMAT eQTL188 eQTL193 eQTL178 eQTL179 eQTL238 [1,] "GT:DS:GP" "0/1:0.79:0.221,0.767,0.011" "0/0:0.031:0.97,0.03,0" "0/0:0.033:0.967,0.033,0" "0/0:0.079:0.922,0.077,0.001" "0/0:0.344:0.664,0.329,0.007" [2,] "GT:DS:GP" "0/0:0.047:0.953,0.047,0" "0/0:0.007:0.993,0.007,0" "0/0:0.006:0.994,0.006,0" "0/0:0.008:0.992,0.008,0" "0/1:0.525:0.477,0.52,0.002" [3,] "GT:DS:GP" "0/0:0.047:0.953,0.047,0" "0/0:0.007:0.993,0.007,0" "0/0:0.006:0.994,0.006,0" "0/0:0.008:0.992,0.008,0" "0/1:0.527:0.476,0.521,0.003" [4,] "GT:DS:GP" "0/0:0.048:0.952,0.048,0" "0/0:0.007:0.993,0.007,0" "0/0:0.006:0.994,0.006,0" "0/0:0.008:0.992,0.008,0" "0/1:0.518:0.485,0.512,0.003" ,其中P1是第二个:之后的第一个元素,A2是第二个元素。

我正在寻找的结果(数字矩阵)看起来像这样

P(A1) = 2*P(A1/A1) + P(A1/A2)

由于矩阵非常庞大,速度可能是一个问题

1 个答案:

答案 0 :(得分:1)

一种方法是首先在第二个:之后检索数字,然后在逗号上检索strsplit。该公式可以与lapply一起使用,例如,此

df <- matrix(c("GT:DS:GP" ,"0/1:0.79:0.221,0.767,0.011" ,"0/0:0.031:0.97,0.03,0" ,  "0/0:0.033:0.967,0.033,0", "0/0:0.079:0.922,0.077,0.001", "0/0:0.344:0.664,0.329,0.007",
            "GT:DS:GP" ,"0/0:0.047:0.953,0.047,0" ,   "0/0:0.007:0.993,0.007,0", "0/0:0.006:0.994,0.006,0" ,"0/0:0.008:0.992,0.008,0"  ,   "0/1:0.525:0.477,0.52,0.002",
            "GT:DS:GP", "0/0:0.047:0.953,0.047,0"  ,  "0/0:0.007:0.993,0.007,0", "0/0:0.006:0.994,0.006,0" ,"0/0:0.008:0.992,0.008,0"  ,   "0/1:0.527:0.476,0.521,0.003",
            "GT:DS:GP" ,"0/0:0.048:0.952,0.048,0" ,   "0/0:0.007:0.993,0.007,0", "0/0:0.006:0.994,0.006,0" ,"0/0:0.008:0.992,0.008,0"   ,  "0/1:0.518:0.485,0.512,0.003"), 
            ncol=6, byrow=TRUE)


df <- df[, -1]
df <- gsub(".+:.+:(.*)", "\\1", df)
out <- lapply(strsplit(df, ","), function(x) {
  x <- as.numeric(x)
  return(2 * x[1] / x[1] + x[1] / x[2])
})

out <- do.call(rbind, out)
dim(out) <- dim(df)

          [,1]      [,2]      [,3]      [,4]     [,5]
[1,]  2.288136  34.33333  31.30303  13.97403 4.018237
[2,] 22.276596 143.85714 167.66667 126.00000 2.917308
[3,] 22.276596 143.85714 167.66667 126.00000 2.913628
[4,] 21.833333 143.85714 167.66667 126.00000 2.947266

显然,必须调整公式,因为它似乎是你问题中的拼写错误