Hello! I'm new to R, sorry if I can't explain well!
I am trying the following:
In table_base, I have the start and end columns. I am trying to check which table_target rows (toFind column) are between these start and end values. At the end I create a tibble where each row has the result, with the table_base and table_base rows matching.
My solution was from below, using a loop. However the loop takes too long. Is there another way?
table_target
A tibble: 732,854 x 2
start end
1 10001 10468
2 10469 11447
3 11504 11675
4 11678 11780
5 15265 15355
6 16713 16749
7 18907 19048
8 19948 20405
9 20531 20679
10 21949 22075
... with 732,844 more rows
table_base
#----------------------------
A tibble: 47,773 x 1
toFind
<dbl>
1 91194674
2 230560793
3 5937253
4 166958439
5 214170376
6 43831041
7 51034865
8 200011786
9 170490434
10 20960010
... with 47,763 more rows
table_base
My solution was:
#This is just to show a progress status
progress <- seq(1, nrow(table_target),1000)
#Loop start!
for (i in 1:nrow(table_target)) {
#The the respective start and end values
start_number <- as.numeric(table_target$start[i])
end_number <- as.numeric(table_target$end[i])
#Filter table_base$toFind with start and end values
searching <- table_base %>% filter(toFind>= start_number & toFind <= end_number)
#Test the results and, if it work, send to results tibble
if(nrow(searching) != 0) {
results <- results %>% bind_rows(searching)
}
#Just show the progrees
if(i %in% progress) {
cat("\014")
cat("\n", round(i/nrow(table_target)*100),"%")
}
}
Thanks!