If you could post your tables as dput() output for copying, I could be more sure, but here's a possible solution that uses the %>% pipe (are you familiar with it?):
library(tidyverse)
df1 <-
tribble(
~capitulo, ~codigo_inicio, ~codigo_termino, ~titulo,
'I', 'A00', 'B99', 'Ciertas enfermedades infecciosas y parasitarias',
'II', 'C00', 'D48', 'Neoplasias',
'III', 'D50', 'D89', 'Enfermedades de la sangre y de los órganos hematopoyéticos y otros trastornos que afectan el mecanismo de la inmunidad',
'IV', 'E00', 'E90', 'Enfermedades endocrinas, nutricionales y metabólicas',
'V', 'F00', 'F99', 'Trastornos mentales y del comportamiento',
'VI', 'G00', 'G99', 'Enfermedades del sistema nervioso'
)
df2 <-
tribble(
~CODIGO_ESTANDAR, ~id,
'Z02.7', 1,
'N39.0', 2,
'S42.3', 3,
'B86', 4,
'N23', 5,
'R50.9', 6
)
btw <- function(x, left, right) {
x >= left & x <= right
}
df1 %>% mutate(dummy = 1) %>%
inner_join(df2 %>% mutate(dummy = 1)) %>%
filter(CODIGO_ESTANDAR %>% btw(codigo_inicio, codigo_termino)) %>%
select(codigo_inicio, CODIGO_ESTANDAR, codigo_termino, titulo )
#> Joining, by = "dummy"
#> # A tibble: 1 x 4
#> codigo_inicio CODIGO_ESTANDAR codigo_termino titulo
#> <chr> <chr> <chr> <chr>
#> 1 A00 B86 B99 Ciertas enfermedades infecc…
df1 %>% mutate(dummy = 1) %>%
inner_join(df2 %>% mutate(dummy = 1)) %>%
filter(CODIGO_ESTANDAR %>% btw(codigo_inicio, codigo_termino)) %>%
select(CODIGO_ESTANDAR, id, titulo)
#> Joining, by = "dummy"
#> # A tibble: 1 x 3
#> CODIGO_ESTANDAR id titulo
#> <chr> <dbl> <chr>
#> 1 B86 4 Ciertas enfermedades infecciosas y parasitarias
Created on 2020-03-06 by the reprex package (v0.3.0)