Extract data based on a list of time points from different individuals
I am trying to extract rows of a dataset based on a list of time points needed for different individuals. What is the way to do it in base R?
Here is the original dataset:
data.frame(id=rep(1:3, each=3), time=1:3, y=c(1:9))
Here is the list of time points for each id which I want to use to extract the data:
$`1` #this is id 1
[1] 1 2 #these are the time points I need for id 1
$`2`
[1] 1 3
$`3`
[1] 2 3
So the final data looks like this:
id time y
1 1 1
1 2 2
2 1 4
2 3 6
3 2 8
3 3 9
Here are couple of ways following the same logic.
In base R -
do.call(rbind, Map(function(p, q) subset(data, id == q & time %in% p),
lst, names(lst)))
Or using tidyverse
-
library(dplyr)
library(purrr)
purrr::imap_dfr(lst, ~data %>% filter(id == .y, time %in% .x))
# id time y
#1 1 1 1
#2 1 2 2
#3 2 1 4
#4 2 3 6
#5 3 2 8
#6 3 3 9
data
data <- data.frame(id=rep(1:3, each=3), time=1:3, y= 1:9)
lst <- list(`1` = c(1, 2), `2` = c(1, 3), `3` = c(2, 3))
One approach using a left join:
df <- data.frame(id=rep(1:3, each=3), time=1:3, y=c(1:9))
want <- data.frame(id = rep(1:3, each = 2),
time = c(1,2,1,3,2,3))
merge(want, df, all.x = TRUE)
Result
id time y
1 1 1 1
2 1 2 2
3 2 1 4
4 2 3 6
5 3 2 8
6 3 3 9