简体   繁体   English

基于另一个 df 匹配的 df 子集(R/R Studio)

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

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. id 列包括主题代码 + 一个“A”或“B”或“C”,具体取决于 session。 I have another df (subs) with only one variable which includes the id s I want to keep (it contains a selection of all the ids from the original df).我有另一个 df (subs),只有一个变量,其中包括我要保留的id (它包含从原始 df 中选择的所有 id)。 In this data frame there is only the subjects code but no session indicator.在此数据框中,只有主题代码,但没有 session 指标。

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?如何仅保留与 subs$V1 列匹配的原始数据框的行?

Using substr .使用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.我将首先在您的原始 data.frame 中创建另一列,该列仅提取 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 .然后您可以根据文件subs过滤行。

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

That should do it.那应该这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM