Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selecting rows of which the value of the variable is equal to certain vector

Tags:

r

row

subset

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?

like image 767
song0089 Avatar asked Mar 20 '23 22:03

song0089


1 Answers

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
like image 100
Matthew Lundberg Avatar answered Mar 23 '23 12:03

Matthew Lundberg