使用最接近的匹配移动值

时间:2016-06-07 22:15:09

标签: r

如何根据df1

的位置值中最接近的匹配来移动df2中的起始值
x <- "Sample    Chromosome  Start
       Samp.1        6  14150000
       Samp.1        7  70000
       Samp.1        13 19250000"    
df1 <-read.table(text=x, header=T, sep = "")

x2 <-  "Name     Chromosome Position
         7:50000    7   50000
         7:57670000 7   57670000
         6:14170000 6   14170000
         6:26950000 6   26950000
         6:36950000 6   36950000
        13:19270000 13  19270000
        13:83370000 13  83370000"           
df2 <- read.table(text=x2, header=T, sep="")

output <-  "Sample  Chromosome  Start   
             Samp.1    6     14170000 
             Samp.1    7      50000
             Samp.1   13     19270000"

1 个答案:

答案 0 :(得分:1)

我想从你的输出中,你正在寻找与你的开始相关的最接近的位置变化并替换它。以下是dplyr

的方法
library(dplyr)
merge(df1, df2[,c(2,3)], by = "Chromosome") %>% 
    group_by(Chromosome) %>% 
    filter(min(abs(Start - Position)) == abs(Start - Position)) %>% 
    select(Sample, Chromosome, Position) %>% unique


# Source: local data frame [3 x 3]
# Groups: Chromosome [3]
#
#   Sample Chromosome Position
#   (fctr)      (int)    (int)
# 1 Samp.1          6 14170000
# 2 Samp.1          7    50000
# 3 Samp.1         13 19270000