'How to extract specific rows (in R) from a longitudinal data with repeated measurements for each participant?

I have a dataset with repeated measurements through 6 visits for each participant. I want to create new data frames 1. trial data for the first 4 visits, and 2. post trial data that was collected on visits 5 and 6.

nrow(data)= 7627 enter image description here

I have tried: data_trial<-data%>%filter(visit==c(1,2,3,4))

data_post_trial<-data%>%filter(visit==c(5,6))

results: -I get unreasonably small number of observations, nrow(data_trial)=1196 This is unrealistic, it seems some observations are left out. I also get an error; "Warning in visit == c("1", "2", "3", "4") : longer object length is not a multiple of shorter object length"

what could be the solution?

r


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source