As @Roland correctly suggested, here's a possible data.table::foverlaps
solution
library(data.table)
setDT(data1) ; setDT(data2) # Convert data sets to data.table objects
setnames(data2, c("loc.start", "loc.end"), c("start", "end")) # Rename columns so they will match in both sets
setkey(data2, start, end) # key the smaller data so foverlaps will work
foverlaps(data1, data2, nomatch = 0L)[, 1:5 := NULL][] # run foverlaps and remove the unnecessary columns
# seg.mean Rl pValue chr i.start i.end CNA
# 1: 0.0039 2 2.594433 6 129740000 129780000 gain
# 2: -1.7738 1 1.992114 10 80900000 81100000 gain
# 3: 0.0110 1 7.175750 16 44780000 44920000 gain
Or
indx <- foverlaps(data1, data2, nomatch = 0L, which = TRUE) # run foverlaps in order to find indexes using `which`
data1[indx$xid][, seg.mean := data2[indx$yid]$seg.mean][] # update matches
# Rl pValue chr start end CNA seg.mean
# 1: 2 2.594433 6 129740000 129780000 gain 0.0039
# 2: 1 1.992114 10 80900000 81100000 gain -1.7738
# 3: 1 7.175750 16 44780000 44920000 gain 0.0110
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…