我在 R 中的一个对象中存储了几千行这样的数据。我想通过配对下面 df 中的两列 ($exonStarts 和 $exonEnds) 中的元素来创建一个新的 df2
df <- structure(list(bin = c(0L, 0L, 0L, 0L, 0L, 0L), name = c("XM_011541469.2",
"XM_017001276.2", "XM_011541467.2", "NM_001276352.2", "NM_001276351.2",
"XM_011541465.3"), chr = c("chr1", "chr1", "chr1", "chr1", "chr1",
"chr1"), strand = c("-", "-", "-", "-", "-", "-"), txStart = c(67092164L,
67092164L, 67092164L, 67092164L, 67092164L, 67092164L), txEnd = c(67109072L,
67131227L, 67131227L, 67134970L, 67134970L, 67134970L), cdsStart = c(67093004L,
67093004L, 67093004L, 67093579L, 67093004L, 67093004L), cdsEnd = c(67103382L,
67127240L, 67127240L, 67127240L, 67127240L, 67127240L), exonCount = c(5L,
9L, 9L, 9L, 8L, 9L), exonStarts = c("67092164,67095234,67096251,67103237,67109028,",
"67092164,67095234,67096251,67103237,67111576,67115351,67125751,67127165,67131141,",
"67092164,67095234,67096251,67103237,67111576,67115351,67125751,67127165,67131141,",
"67092164,67096251,67103237,67111576,67115351,67125751,67127165,67131141,67134929,",
"67092164,67095234,67096251,67115351,67125751,67127165,67131141,67134929,",
"67092164,67095234,67096251,67103237,67111576,67115351,67125751,67127165,67134929,"
), exonEnds = c("67093604,67095421,67096321,67103382,67109072,",
"67093604,67095421,67096321,67103382,67111644,67115464,67125909,67127257,67131227,",
"67093604,67095421,67096321,67103343,67111644,67115464,67125909,67127257,67131227,",
"67093604,67096321,67103382,67111644,67115464,67125909,67127257,67131227,67134970,",
"67093604,67095421,67096321,67115464,67125909,67127257,67131227,67134970,",
"67093604,67095421,67096321,67103382,67111644,67115464,67125909,67127257,67134970,"
), score = c(0L, 0L, 0L, 0L, 0L, 0L), gene = c("C1orf141", "C1orf141",
"C1orf141", "C1orf141", "C1orf141", "C1orf141"), cdsStartStat = c("cmpl",
"cmpl", "cmpl", "cmpl", "cmpl", "cmpl"), cdsEndStat = c("cmpl",
"cmpl", "cmpl", "cmpl", "cmpl", "cmpl"), exonFrames = c("0,2,1,0,-1,",
"0,2,1,0,1,2,0,0,-1,", "0,2,1,0,1,2,0,0,-1,", "2,1,0,1,2,0,0,-1,-1,",
"0,2,1,2,0,0,-1,-1,", "0,2,1,0,1,2,0,0,-1,")), row.names = c(NA,
6L), class = "data.frame")
产生如下所示的输出。输入 df 的第一行应通过将 $exonStart 的每个元素与 $exonEnd 的每个元素进行配对(元素计数将相同并存在于 $exonCount 列中),以下面的格式产生 5 行 df2。
$chr $exonStart $exonEnd
chr1 67092164 67093604
chr1 67095234 67095421
chr1 67096251 67096321
chr1 67103237 67103382
chr1 67109028 67109072
这需要迭代输入 df 的所有行并将所有结果合并到 df2 中。因此,df2 中总共会有sum(object$exonCount)
行。
我知道这里有某种strsplit
函数可以拆分 $exonStarts 和 $exonEnds,然后将它们合并。也许有一个apply
函数可以对所有行执行此操作?我首先尝试了类似这样的方法
map2(unlist(strsplit(df$exonStarts[1], ",")), unlist(strsplit(dft$exonEnds[1], ",")), c)
生成配对元素的列表
[[1]]
[1] "67092164" "67093604"
[[2]]
[1] "67095234" "67095421"
[[3]]
[1] "67096251" "67096321"
[[4]]
[1] "67103237" "67103382"
[[5]]
[1] "67109028" "67109072"
但我不知道接下来该怎么做。任何帮助我都会很感激。
您可以
separate_longer_delim
使用tidyr
结果如下(仅显示前 10 行):
空行是由于原始列中的尾随逗号造成的,可以使用 轻松删除
filter
。如果这样做,您将看到剩余 49 行,与 相同sum(df$exonCount)
。正如@TarJae 提到的,每列中的项目数必须相等,否则会导致错误。想必您确信这种情况不可能发生。
使用您的方法时您还会遇到错误
strsplit
。在 R 库中,我们可以这样做
(对于其他应用程序,我们需要修改最后一行
f()
。)应用:
注意。这种方法的缺点是,如果长度不是最大,则分别在-split行或的
NA
末尾填充 s 。换句话说,它强烈地假设缺失数据(如果存在)位于每行的末尾。只要您不携带包含此类信息的数据,这种方法可能就很合适。","
exonStarts
exonEnds
袖珍的: