I got 2 files which I'd like to combine using R.
head(bed)
chr8 41513235 41513282 ANK1.Exon1
chr8 41518973 41519092 ANK1.Exon2
The first one is giving intervals and their names. (Chromosome, from, to, name)
head(coverage)
chr1 41513235 20
chr1 41513236 19
chr1 41513237 19
The second one is giving coverages for single Bases. (Chromosome, position, coverage)
I now want to get the name of each Exon written next to each Position. This will result in some positions with no "Exon" which I want to delete afterwards.
I figured out a ways how to do what I want. However it needs 3 for loops and about 15 hours computing time. Since for loops are not best practice in R I'd 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 of the three positions lie in the intervall "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