Subset Data Frame based on Unique Subject IDs

Hi Community -

I want to subset a data frame using a the subject ID column. Each subject ID is unique but i only want a subset and i want to keep all the other columns of data associated with that subset . Is there an elegant wat to do this? For example:

DF1 had IDs 3,4,5,6,7,8,9,10...100, I want DF2 that has IDs 5....60, 65, 43, etc.

Thank you

I suggest using the filter() function from the dplyr package.

DF <- data.frame(ID = 1:100, Value = 201:300)
ID_list <- c(6, 23, 65, 78)
library(dplyr, warn.conflicts = FALSE)
DF2 <- filter(DF, ID %in% ID_list)
#>   ID Value
#> 1  6   206
#> 2 23   223
#> 3 65   265
#> 4 78   278

Created on 2020-09-28 by the reprex package (v0.3.0)

Ok thank you i will try that now!

Ok i forgot add that the patient IDs have both letters and numbers - i'm getting this error.
Error: unexpected symbol in "Patient_ID_list <- c(045071P"

ha! i figured it out... when you have numbers and letters you have to use ' ####XXXX### ' - thats perfect!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.