R 基于第二数据帧的数据集重复子集划分及算法
我有一个数据帧“dfA”(65000行),其形式如下:R 基于第二数据帧的数据集重复子集划分及算法,r,R,我有一个数据帧“dfA”(65000行),其形式如下: Chr Pos NCP NCP_Ratio 1 72 1.06 0.599 1 371 4.26 1.331 1 633 2.10 2.442 1 859 1.62 1.276 1 1032 7.62 4.563 1 1199 6.12 4.896 1 1340 13.22 23.607 Chr Pos
Chr Pos NCP NCP_Ratio
1 72 1.06 0.599
1 371 4.26 1.331
1 633 2.10 2.442
1 859 1.62 1.276
1 1032 7.62 4.563
1 1199 6.12 4.896
1 1340 13.22 23.607
Chr Pos Watson Crick
1 1 5 0
1 2 5 0
1 4 1 0
1 6 1 0
1 7 1 0
1 8 2 0
1 9 2 0
1 12 1 0
1 14 1 0
1 15 2 0
1 22 1 0
我希望在dfA
的每一行中使用Chr
和Pos
的值,以顺序地子集形式的第二个数据帧dfB
:
Chr Pos NCP NCP_Ratio
1 72 1.06 0.599
1 371 4.26 1.331
1 633 2.10 2.442
1 859 1.62 1.276
1 1032 7.62 4.563
1 1199 6.12 4.896
1 1340 13.22 23.607
Chr Pos Watson Crick
1 1 5 0
1 2 5 0
1 4 1 0
1 6 1 0
1 7 1 0
1 8 2 0
1 9 2 0
1 12 1 0
1 14 1 0
1 15 2 0
1 22 1 0
dfB
大约有400万行
每次我子集dfB
,我都希望根据Pos
中的范围检索感兴趣区域的值(即+/-1000dfA
中Pos
的值),并将它们添加到第三个data.framedfC
中,该第三个data.frame最初以零作为前缀
我通过循环遍历dfA
的每一行来实现这一点。但由于65000行,这需要几个小时。因此,我的问题是:
temp=NULL
width=300 # Region upstream and downstream of centrepoint #
padding=50 # Add some padding area to table #
width1=width+padding
dfC=data.frame(NULL)
dfC[1:((width1*2)+1),"Pos"]=(1:((width1*2)+1)) # Create Pos column #
# Prefill dfC table with zeros #
dfC[1:((width1*2)+1),"Watson"]=0
dfC[1:((width1*2)+1),"Crick"]=0
for (chrom in 1:16) { # LOOP1. Specify which chromosomes to process #
dfB.1=subset(dfB,Chr==chrom) # Make temp copy of the dataframes for each chromosome #
dfA.1=subset(dfA, Chr==chrom)
for (i in 1:nrow(dfA.1)) { # LOOP2: For each row in dfA:
temp=subset(dfB.1, Pos>=(dfA.1[i,"Pos"]-width1) & Pos<=(dfA.1[i,"Pos"]+width1)) # Create temp matrix with hits in this region
temp$Pos=temp$Pos-dfA.1[i,"Pos"]+width1+1
dfC[temp$Pos,"Watson"]=dfC[temp$Pos,"Watson"]+temp[,"Watson"]
dfC[temp$Pos,"Crick"]=dfC[temp$Pos,"Crick"]+temp[,"Crick"]
} # End of LOOP2 #
} # End of LOOP1 #
我只是清理了你的代码,所以不要期望有很大的改进,但是我 我认为这个版本可能会运行得稍微快一些
width <- 300
padding <- 50
width1 <- width + padding
dfC <- data.frame(Pos=1:((width1*2)+1), Watson=0, Crick=0)
for (chrom in 1:16) {
dfB1 <- subset(dfB, Chr == chrom)
for (pos in dfA$Pos[dfA$Chr == chrom]) {
dfB2 <- dfB1[(dfB1$Pos >= pos - width1) & (dfB1$Pos <= pos + width1), ]
rows <- dfB2$Pos - pos + width1 + 1
dfC$Watson[rows] <- dfC$Watson[rows] + dfB2$Watson
dfC$Crick[rows] <- dfC$Crick[rows] + dfB2$Crick
}
}
width我只清理了你的代码,所以不要期望有很大的改进,但是我
我认为这个版本可能会运行得稍微快一些
width <- 300
padding <- 50
width1 <- width + padding
dfC <- data.frame(Pos=1:((width1*2)+1), Watson=0, Crick=0)
for (chrom in 1:16) {
dfB1 <- subset(dfB, Chr == chrom)
for (pos in dfA$Pos[dfA$Chr == chrom]) {
dfB2 <- dfB1[(dfB1$Pos >= pos - width1) & (dfB1$Pos <= pos + width1), ]
rows <- dfB2$Pos - pos + width1 + 1
dfC$Watson[rows] <- dfC$Watson[rows] + dfB2$Watson
dfC$Crick[rows] <- dfC$Crick[rows] + dfB2$Crick
}
}
<>代码>宽度,如果包含示例代码的预期输出,这将是有益的。此外,考虑以“代码> dPUT/CODE”的形式发布数据。感谢编辑/格式化的帮助。我添加示例输出。我不熟悉DPT,但我现在正在阅读帮助。如果包含SA的预期输出,这将是有帮助的。同样,考虑以“代码> dPUT/CODE”的形式发布数据。感谢编辑/格式化的帮助。我添加了示例输出。我不熟悉DPT,但我现在正在阅读帮助。