这与问题有关:How to extract rows (using Loop) from a data frame and save it in another data frame
如果' POS' ddf位于任何' start'之间。并且'结束' refdf,它需要包含在outdf中,它与ddf具有相同的结构。我可以用'来管理它。循环,但可以不使用'来实现。循环?
ddf = structure(list(POS = c(23L, 48L, 5L), Freq1 = c(0.5, 0.7, 0.8
), Freq2 = c(0.45, 0.55, 0.65)), .Names = c("POS", "Freq1", "Freq2"
), class = "data.frame", row.names = c(NA, -3L))
refdf = structure(list(Start = c(1L, 25L, 60L), End = c(10L, 50L, 75L
)), .Names = c("Start", "End"), class = "data.frame", row.names = c(NA,
-3L))
ddf
# POS Freq1 Freq2
#1 23 0.5 0.45
#2 48 0.7 0.55
#3 5 0.8 0.65
refdf
# Start End
#1 1 10
#2 25 50
#3 60 75
outdf = data.frame(POS=numeric(), Freq1=numeric(), Freq2=numeric())
for(i in 1:nrow(ddf)) for(j in 1:nrow(refdf)){
if(ddf[i,1]>refdf[j,1] && ddf[i,1]<refdf[j,2])
{outdf[nrow(outdf)+1,] = ddf[i,]; next}
}
outdf
# POS Freq1 Freq2
#2 48 0.7 0.55
#3 5 0.8 0.65
我试过以下但不起作用:
apply(ddf,1,function(x){print(x);ifelse(x[1]>refdf$Start & x[1]<refdf$End, x,"")})
答案 0 :(得分:1)
对于大问题,这在空间上效率不高,但它不使用for
:
ddf[ddf$POS %in% unlist(apply(refdf, 1, function(x) seq(x[1],x[2]))),]
## POS Freq1 Freq2
## 2 48 0.7 0.55
## 3 5 0.8 0.65
POS
的所有允许值均由unlist(apply)
表达式计算。这当然假设POS
仅包含整数值。
答案 1 :(得分:1)
这是一种方式。它并不需要整数值,但它也不会特别有效:
pow <- cbind(expand.grid(ddf$POS, refdf$Start), Var3=expand.grid(ddf$POS, refdf$End)$Var2)
boom <- pow[which(pow$Var1 > pow$Var2 & pow$Var1 < pow$Var3), 'Var1']
ddf[ddf$POS %in% boom, ]
# POS Freq1 Freq2
#2 48 0.7 0.55
#3 5 0.8 0.65