Subset of df based on matches of another df (R/ R Studio)

Viewed 47

I have my original data frame from which I only want to keep certain rows.

head(original_df)
  id    roi            mean  sd
1 1102A HarvardOxford 0.4675 0.1345
2 1102A HarvardOxford 0.4456 0.1345
3 1102A HarvardOxford 0.4567 0.788
4 1102A HarvardOxford 0.1231 0.8976
5 1102A Lh_func_3     0.1231 0.8678
6 1102A Lh_func_      0.2342 0.67856

The id-column includes the subjects code + an "A" or "B" or "C" depending on the session. I have another df (subs) with only one variable which includes the ids I want to keep (it contains a selection of all the ids from the original df). In this data frame there is only the subjects code but no session indicator.

head(subs)
         V1
1 1102
2 1103
3 1104
4 1107
5 1110
6 1111

How can I keep just the rows of my original data frame that match the subs$V1 column?

2 Answers

Using substr.

subset(df1, substr(id, 1, 4) %in% df2$V1)
#      id          x
# 2 1103A  0.2051387
# 4 1103B -0.8920853
# 6 1103C  0.8064977

Data:

df1 <- structure(list(id = c("1102A", "1103A", "1102B", "1103B", "1102C", 
"1103C"), x = c(-0.946458205218808, 0.205138719393085, -0.734810811183742, 
-0.892085335997171, 0.327500189913222, 0.806497715247655)), class = "data.frame", row.names = c(NA, 
-6L))

df2 <- structure(list(V1 = 1103:1105), class = "data.frame", row.names = c(NA, 
-3L))

I would start off by making another column in your original data.frame that extract only the number in the ID.

library(tidyverse)
original.df2 <- mutate(original.df, V1 = substr(id,1,4) ## select only first 4 digits

Then you can filter the row based on the the file subs.

filter(original.df2, V1 %in% subs$V1)

That should do it.

Related