r - Subset data based on vector with repeated observations -


i have following data 2 observations per subject:

subject <- c(8,8,10,10,11,11,15,15) position <- c("h","l","h","l","h","l","h","l") time <- c(90,90,30,30,30,30,90,90) response <- c(5.6,5.2,0,0,4.8,4.9,1.2,.9)  data <- data.frame(subject,position,time,response) 

i want rows of data have subject numbers in vector, v:

v <- c(8,10,10) 

how can obtain both observations data subject number in v , have observations repeated same number of times corresponding subject number appears in v?

desired result:

subject <- c(8,8,10,10,10,10) position <- c("h","l","h","l","h","l") time <- c(90,90,30,30,30,30) response <- c(5.6,5.2,0,0,0,0)  out <- data.frame(subject,position,time,response) 

i thought variation of %in% operator trick not account repeated subject numbers in v. though subject number listed twice in v, 1 copy of corresponding rows in data.

i create loop , append matching observations piece inside bootstrap sampler , option dramatically increase computation time.

merge friend:

merge(list(subject=v), data) #  subject position time response #1       8        h   90      5.6 #2       8        l   90      5.2 #3      10        h   30      0.0 #4      10        l   30      0.0 #5      10        h   30      0.0 #6      10        l   30      0.0 

as @frank implies, logic can translated data.table or dplyr or sql of else handle left-join.


Comments