R根据间隔和合并切割两个data.frames

时间:2013-10-15 20:23:06

标签: r dataframe

如何根据间隔剪切两个数据框并合并它们?

数据框1

read.table(textConnection(
"   from to Lith  
1   0   1.2 GRN   
2   1.2 5.0 GDI   
"), header=TRUE)    

数据框2

read.table(textConnection(
"   from to Weath  
1   0  1.1  HW  
2   1.1 2.9 SW 
3   2.9 5.0 HW  
"), header=TRUE) 

产生的数据框

  from to Weath Lith 
1 0.0 1.1 HW  GRN
2 1.1 1.2 SW  GRN
3 1.2 2.9 SW  GDI
4 2.9 5.0 HW  GDI 

2 个答案:

答案 0 :(得分:6)

使用roll的{​​{1}}功能的好地方:

data.table

答案 1 :(得分:2)

如果要通过最低匹配from值或跨行完全匹配,您想要“切割”两个数据集并不完全清楚。

尝试以下方法:

library(data.table)
ft <- c("from", "to")
allVals <- unique(sort(unlist(c(df1[, ft], df2[, ft]))))
results <- data.table(from=head(allVals, -1), to=allVals[-1L])

results[, 
  c("Lith", "Weath") := 
     lapply(list(
       df1[from >= df1[["from"]] & to <= df1[["to"]], "Lith"], 
       df2[from >= df2[["from"]] & to <= df2[["to"]], "Weath"]
       # alternatively, someting like:
       #  df1[which.max(from >= df1[["from"]]), "Lith"],
       #  df2[which.max(from >= df2[["from"]]), "Weath"]
     ), as.character)
  , by=list(from, to)]

results

   from  to Lith Weath
1:  0.0 1.1  GRN    HW
2:  1.1 1.2  GRN    SW
3:  1.2 2.9  GDI    SW
4:  2.9 5.0  GDI    HW
相关问题