I have a longitudinal data called df for more than 1000 people that looks like the following:
id  year    name    status  
1   1984    James   4   
1   1985    James   1   
2   1983    John    2   
2   1984    John    1   
3   1980    Amy     2   
3   1981    Amy     2   
4   1930    Jane    4   
4   1931    Jane    5   
I'm trying to subset the data by certain id. For instance, I have a vector dd that consists of ids that I would like to subset:
dd<-c(1,3)
I've tried the following but it did not work, for instance:
subset<-subset(df, subset(df$id==dd))
or
subset<-subset(df, subset(unique(df$id))==dd))
or
subset<-df[which(unique(df$id)==dd),]
or I tried a for-loop
for (i in 1:2){
  subset<-subset(df, subset=(unique(df$id)==dd[i]))
}
Would there be a way to only select the rows with the ids that match the numbers in in the vector dd?
Use %in% and logical indexing:
df[df$id %in% dd,]
  id year  name status
1  1 1984 James      4
2  1 1985 James      1
5  3 1980   Amy      2
6  3 1981   Amy      2
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With