1

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

数据框 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 
4

2 回答 2

6

使用以下roll功能的好地方data.table

library(data.table)

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

dt2 = data.table(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))

# set the key for the join
setkey(dt1, from)
setkey(dt2, from)

# get the unique id's
ids = sort(unique(c(dt1$from, dt2$from, dt1$to, dt2$to)))

# make a table of final from-to, keyed by 'final.from'
from.to = data.table(final.from = head(ids, -1),
                     final.to = tail(ids, -1),
                     key = 'final.from')

# join with a roll and combine together
result = dt1[from.to, roll = Inf][, Weath := dt2[from.to, roll = Inf]$Weath][,
             `:=`(to = final.to, final.to = NULL)]
#   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
于 2013-10-15T21:45:24.427 回答
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
于 2013-10-15T20:45:08.093 回答