如何根据间隔剪切两个数据框并合并它们?
read.table(textConnection(
" from to Lith
1 0 1.2 GRN
2 1.2 5.0 GDI
"), header=TRUE)
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
答案 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