I am given two very large data sets and I've been trying to build a function that would find certain coordinates from one set that respect an if clause regarding the other data set. My problem is that the function I wrote is very slow and although I've been reading answers to questions similar in some way, I haven't managed to make it work.
So if I am given:
>head(CTSS)
V1 V2 V3
1 chr1 564563 564598
2 chr1 564620 564649
3 chr1 565369 565404
4 chr1 565463 565541
5 chr1 565653 565697
6 chr1 565861 565922
and
> head(href)
chr region start end strand nu gene_id transcript_id
1 chr1 start_codon 67000042 67000044 + . NM_032291 NM_032291
2 chr1 CDS 67000042 67000051 + 0 NM_032291 NM_032291
3 chr1 exon 66999825 67000051 + . NM_032291 NM_032291
4 chr1 CDS 67091530 67091593 + 2 NM_032291 NM_032291
5 chr1 exon 67091530 67091593 + . NM_032291 NM_032291
6 chr1 CDS 67098753 67098777 + 1 NM_032291 NM_032291
For each value in the start column from the href data set I want to find the first two values in the 3rd column of the CTSS data set smaller or equal than it and keep it in a new dataframe.
The loop I wrote:
y <- CTSS[order(-CTSS$V3), ]
find_CTSS <- function(x, y) {
n <- length(x$start)
foo <- data.frame(matrix(0, n, 6))
for (i in 1:n)
{
a <- which(y$V3 <= x$start[i])
foo[i, ] = c(x$start[i], x$stop[i], y$V2[a[1]], y$V3[a[1]] , y$V2[a[2]], y$V3[a[2]])
}
print(foo)
}