rfor-loopfilter

Filtering data in R, in a for loop


I have a dataframe (d_activity) with 15 variables, each variable spans over multiple rows. What I want to do is filter the data if they have a specific Id. These Id's are stored in the a different dataframe called active_people. So I could do this:

activity_in_use <-filter(d_activity, Id == as.character(active_people[1]))

However active_people has 21 Id numbers so I would have to add or statments I think:

activity_in_use <-filter(d_activity, Id == as.character(active_people[1])|Id == as.character(active_people[2])|Id == as.character(active_people[3])|Id == as.character(active_people[4])|Id == as.character(active_people[5])|Id == as.character(active_people[6])|Id == as.character(active_people[7])|Id == as.character(active_people[8])|Id == as.character(active_people[9])|Id == as.character(active_people[10])|Id == as.character(active_people[11])|Id == as.character(active_people[12])|Id == as.character(active_people[13])|Id == as.character(active_people[14])|Id == as.character(active_people[15])|Id == as.character(active_people[16])|Id == as.character(active_people[17])|Id == as.character(active_people[18])|Id == as.character(active_people[19])|Id == as.character(active_people[20])|Id == as.character(active_people[21]))

My question is, is it possible to, instead of typing 21 different or statements, put this in a loop? Or is there a different way? Something like this:

for (i in 1:length(active_people))
{
activity_in_use <-filter(d_activity, Id == as.character(active_people[i]))
}

However, this keeps overwriting itself, so I end up only with the data of the last ID from the active_people list.

Thank you very much! Douwe


Solution

  • Use %in% instead of ==, thus we can remove the | along with multiple statements

    dplyr::filter(d_activity, Id %in% active_people)