【问题标题】:Matching columns from a data frame (csv file with the columns of another dataframe csv file and add new colums匹配来自数据框的列(csv 文件与另一个数据框 csv 文件的列并添加新列
【发布时间】:2015-02-19 10:49:39
【问题描述】:

我有两个大数据帧(csv格式),一个(df1)有这种结构

chromName fragStart fragEnd fragLength leftFragEndLength rightFragEndLength
   Chr1     176         377            202          202                202
   Chr1     472         746            275          275                275
   Chr1     1276        1382            107         107                107
   Chr1     1581        1761            181         173                  4
   Chr1     1890        2080            191          93                 71

另一个 (df2) 包括 5'target_id_start 5'target_id_end 和 3'target_id_start,3'target_id_end together 的结果,看起来像这样

    Chr target_id_start target_id_end tot_counts uniq_counts est_counts
1  Chr1        10000016      10000066          0           0          0
2  Chr1        10000062      10000112          0           0          0
3  Chr1        10000171      10000221          0           0          0
4  Chr1        10000347      10000397          0           0          0
5  Chr1         1000041       1000091          0           0          0

我要做的是检查列 target_id_start 和 target_id_end 是否介于或等于与列 fragStart 和 fragEnd。如果这是真的,那么我想在第一个文件 df1.txt 中写入列 tot_counts uniq_counts est_counts。这将适用于 5'target_id_start 5'target_id_end 和 3'target_id_start,3'target_id_end 并且结果是这样的

chromName fragStart fragEnd fragLength leftFragEndLength rightFragEndLength tot_counts5' uniq_counts5' est_counts5' tot_counts3' uniq_counts3' est_counts3'
    Chr1     176         377            202          202                202            0           0          0            0           0          0 
    Chr1     472         746            275          275                275            0           0          0            0           0          0
    Chr1     1276        1382            107         107                107            0           0          0            0           0          0
    Chr1     1581        1761            181         173                  4            0           0          0            0           0          0
    Chr1     1890        2080            191          93                 71            0           0          0            0           0          0

你知道在 R 中有什么好的方法吗?非常感谢。

【问题讨论】:

  • 您可能需要检查findOverlaps 来自library(IRanges)foverlaps 来自library(data.table) 可能是这个链接提供一些想法stackoverflow.com/questions/27619381/…stackoverflow.com/questions/19748535/…
  • 两个 DF 大小相等,一个中的每一行对应另一个中的行?
  • 不,DF 的大小不相等,每一行都不对应其他行!
  • 也许你应该简化你的例子。是否要搜索 df1 中两个数字的某个区间是否与 df2 中两个数字的区间匹配?然后在每场比赛中将值从 df2 写入 df1?长列名称的混乱有点使它失去了问题的本质。
  • 就是这样。例如,如果 fragStart

标签: r dataframe multiple-columns bioinformatics matching


【解决方案1】:

尽管我真的很讨厌循环,但我能提供的最好的方法是:

a <- data.frame(x = c(1,10,100), y = c(2, 20, 200))
b <- data.frame(x = c(1.5, 30, 90, 150), y = c(1.6, 50, 101, 170), z = c("a","b","c", "d"))

a$z <= NA

for(i in 1:length(a$x)){
  temp <- which((b$x >= a$x[i] & b$x <= a$y[i]) | (b$y >= a$x[i] & b$y <= a$y[i]))
  a$z[i] <- ifelse(length(temp) > 0, temp, NA) 
}

作为示例 - 循环写入数据帧 b 的行索引,其中 a 中的间隔对应于 b 中的间隔。进一步,您可以编写一个循环,在其中获取这些行索引并将相应的值写入其他列。

这可能会给你一些想法。但这在大型数据集上效率不高。希望它能激发您找到正确的解决方案。不是像我这样的解决方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    相关资源
    最近更新 更多