0

I have two dataframes with multiple rows in both.

df1

position  frequency         
  7572868      14  
  7572868      0.00  
  7572868      7 
  7572869      0.00 
  7572869      7 
  7572870      6
  7577057      9
  7577019      10

df2

chr    start    end    ID
chr17 7572869 7572967 ID_1
chr17 7577057 7577121 ID_2
chr17 7572790 7572890 ID2_1

I want to check if position value from df1 falls into range of values from dataframe 2, assign ID to df1 for values present in range. So for values 7572869 and 7577019 should have ID assigned as (ID_1 and ID2_1) and values 7577057 and 7577019 have an ID of ID_2.

I used following code to perform this but got wrong result. How can I get the accurate results?

rawPostFreq$ampID <- ifelse(sapply(rawPostFreq$position, function(p) any(insertBed$start <= p & insertBed$end >= p)), insertBed$ampID, NA)

Result of code enter image description here

Callie
  • 333
  • 1
  • 9
  • I'd recommend [bedtools](https://bedtools.readthedocs.io/en/latest/content/tools/map.html?highlight=map) for tasks like that. – Andre Wildberg Nov 04 '22 at 21:16

0 Answers0