Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于R中另一个数据帧的条件对data.frame中的列求和_R_Apply - Fatal编程技术网

基于R中另一个数据帧的条件对data.frame中的列求和

基于R中另一个数据帧的条件对data.frame中的列求和,r,apply,R,Apply,我有两个数据帧,a和b 对于b中的每一行,我希望找到a中位于b的start,end内的所有start,end,然后求a的这个特定子集的start,end的差分之和,并将其作为一个新列存储在b中。我正在使用for循环,但是有没有更有效的方法在R中使用apply 使用sqldf简单,使用base R烦人: R>require(sqldf) R>b$id <- 1:nrow(b) R>sqldf("select id, b.chr, sum(a.end - a.start) a

我有两个数据帧,a和b

对于b中的每一行,我希望找到a中位于b的start,end内的所有start,end,然后求a的这个特定子集的start,end的差分之和,并将其作为一个新列存储在b中。我正在使用for循环,但是有没有更有效的方法在R中使用apply


使用sqldf简单,使用base R烦人:

R>require(sqldf)
R>b$id <- 1:nrow(b)
R>sqldf("select id, b.chr, sum(a.end - a.start) as diff 
    from a, b where a.start >= b.start and b.end >= a.end group by id")
  id chr diff
1  1   1    5
2  2   1    4

使用基因组范围搜索帖子,这是一个包,旨在有效处理涉及重叠范围的生物信息学问题。
R>require(sqldf)
R>b$id <- 1:nrow(b)
R>sqldf("select id, b.chr, sum(a.end - a.start) as diff 
    from a, b where a.start >= b.start and b.end >= a.end group by id")
  id chr diff
1  1   1    5
2  2   1    4