I have 2 files that I would like to merge using R.
head(bed) chr8 41513235 41513282 ANK1.Exon1 chr8 41518973 41519092 ANK1.Exon2
The first indicates the intervals and their names. (Chromosome, from, to, name)
head(coverage) chr1 41513235 20 chr1 41513236 19 chr1 41513237 19
The second - provides coverage for single bases. (Chromosome, position, coating)
Now I want to get the name of each Exon written next to each item. This will result in some non-Exon line items that I want to delete later.
I figured out how to do what I want. However, they require 3 cycles and about 15 hours of computing time. Since looping is not the best practice in R, I would like to know if anyone knows a better way than:
coverage <- cbind(coverage, "Exon") coverage[,4] <- NA for(i in 1:nrow(bed)){ for(n in bed[i,2]:bed[i,3]{ for(m in 1:nrow(coverage)){ if(coverage[m,2]==n){ file[m,4] <- bed[i,4] } } } } na.omit(coverage)
Since all three positions are in the interval "ANK1.Exon1", the output should look like this:
head(coverage) chr1 41513235 20 ANK1.Exon1 chr1 41513236 19 ANK1.Exon1 chr1 41513237 19 ANK1.Exon1
source share