Here is a more elegant method that uses fuzzyjoin.
ex <- data.frame('id'= seq(1:26),
'day'= c(105:115, 1:12,28:30),
'letter' = LETTERS[1:26],
s = rep(1:26, each = 3, len = 26) )
v1 <- c(107,112,10)
v2 <- c(109,115,28)
word <- c("pen","desk","light")
Ranges <- data.frame(v1,v2,word)
library(dplyr)
library(fuzzyjoin)
library(tidyr)
ex2 <- ex |> fuzzy_left_join(Ranges, by = c(day = "v1", day = "v2"),
match_fun = list(`>=`, `<=`)) |>
mutate(word = replace_na(word, "undetermined")) |>
select(-v1, -v2)
ex2
id day letter s word
1 1 105 A 1 undetermined
2 2 106 B 1 undetermined
3 3 107 C 1 pen
4 4 108 D 2 pen
5 5 109 E 2 pen
6 6 110 F 2 undetermined
7 7 111 G 3 undetermined
8 8 112 H 3 desk
9 9 113 I 3 desk
10 10 114 J 4 desk
11 11 115 K 4 desk
12 12 1 L 4 undetermined
13 13 2 M 5 undetermined
14 14 3 N 5 undetermined
15 15 4 O 5 undetermined
16 16 5 P 6 undetermined
17 17 6 Q 6 undetermined
18 18 7 R 6 undetermined
19 19 8 S 7 undetermined
20 20 9 T 7 undetermined
21 21 10 U 7 light
22 22 11 V 8 light
23 23 12 W 8 light
24 24 28 X 8 light
25 25 29 Y 9 undetermined
26 26 30 Z 9 undetermined