I would like to extract values that occurs between two numbers. However, I have multiple ranges of numbers and between() seems not to work in this respect.
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) )
structure(list(id = 1:26, day = c(105L, 106L, 107L, 108L, 109L,
110L, 111L, 112L, 113L, 114L, 115L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 28L, 29L, 30L), letter = c("A", "B", "C",
"D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"), s = c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L,
6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L)), row.names = c(NA, 26L), class = "data.frame")
Specifically, I want to extract values based on id
. For example, I am interested in the data that falls within 1:3, 7:9, 12:15, 17:19, etc.
The following ways work but on a manual basis, which is time-consuming as I have a big dataset.
1- filter(ex, between(id, 1,3))
2- vec <- c(1:3)
ex%>% filter(id%in% vec)
The values I'm interested in are already stored in two vectors, so working from vectors would be preferable, but necessary. What is stored in the vectors are the two ranges - something like (not sure if this practical):
v1 <- c(1,7,12,17)
v2 <- c(3,9,15,19)
The output I'm looking to is similar to this.
id day letter s
1 1 105 A 1
2 2 106 B 1
3 3 107 C 1
4 7 111 G 3
5 8 112 H 3
6 9 113 I 3
7 12 1 L 4
8 13 2 M 5
9 14 3 N 5
10 15 4 O 5
11 17 6 Q 6
12 18 7 R 6
13 19 8 S 7
Thank you in advance!