I have 2 dataframes:
> access
V1 V2 V3
1 chr10 136122 136533
2 chr10 179432 179769
3 chr10 182988 183371
4 chr10 224234 224489
5 chr10 237693 237958
and
> peaks
V1 V2 V3
1 chr10 126122 126533
2 chr10 179450 179730
3 chr10 182788 183350
4 chr10 224244 224500
5 chr10 237695 237950
The coloumn V2 and V3 are start and end of regions (range) in both dataframes. I want to keep those rows in peaks
dataframe for which access$V1 == peaks$V1
AND which fall in the range (or regions) of access
dataframe. For example the new dataframe will be like: peaks
dataframe's
1st row region doesn't exist in
access
dataframe so it will be assigned category U.2nd row of
peaks
falls in the given range inaccess
dataframe (2nd row) and will be assigned category B.3rd row of
peaks
doesn't completely fall in that region but it somehow overlaps with region in 3rd row ofaccess
, for this I will assign category A.4th row of
peaks
also doesn't overlap completely at it ends 11 number after the end of region in row 4 of access, this will also be in category A.5th row falls in the region hence will be in category B.
Expected output:
> newdf
V1 V2 V3 V4
1 chr10 126122 126533 U
2 chr10 179450 179730 B
3 chr10 182788 183350 A
4 chr10 224244 224500 A
5 chr10 237695 237950 B
Here are the dput of input dataframes:
> dput(peaks)
structure(list(V1 = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "chr10", class = "factor"),
V2 = c(126122L, 179450L, 182788L, 224244L, 237695L), V3 = c(126533L,
179730L, 183350L, 224500L, 237950L)), .Names = c("V1", "V2",
"V3"), class = "data.frame", row.names = c(NA, -5L))
> dput(access)
structure(list(V1 = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "chr10", class = "factor"),
V2 = c(136122L, 179432L, 182988L, 224234L, 237693L), V3 = c(136533L,
179769L, 183371L, 224489L, 237958L)), .Names = c("V1", "V2",
"V3"), class = "data.frame", row.names = c(NA, -5L))
Edit:
My new access df looks like this and now I also want to append the last column in my final output df:
> access
V1 V2 V3 V4
1 chr10 136122 136533 found
2 chr10 179432 179769 notFound
3 chr10 182988 183371 found
4 chr10 224234 224489 found
5 chr10 237693 237958 notFound
So now there is one extra condition which is if row in access falls in peaks range then also append the value in V4 in a new column in final df, if some region is not found then by default will be notFound
. Therefore, final output will be:
> newdf
V1 V2 V3 V4 V5
1 chr10 126122 126533 U notFound
2 chr10 179450 179730 B notFound
3 chr10 182788 183350 A found
4 chr10 224244 224500 A found
5 chr10 237695 237950 B notFound
Here in row1$V5
the value is notFound because this region was not found and in remaining cases we got the values in V5 from modified access df.