在R中,如何通过另一个data.frame的值对数据框架进行子集化? [英] In R, how do I subset a data.frame by values from another data.frame?

查看:533
本文介绍了在R中,如何通过另一个data.frame的值对数据框架进行子集化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据帧。第一个 df.1 包含两列配对的数字标识符,其中每列包括〜100,000行。第二个数据框 df.2 包含数字标识符的一列( df.2 $ C )。这个数据框有大约200行。



如何找到 df.1 的数据配对子集仅包含在 df.2 $ C 中找到的标识符值的行?



最后一个子集将包含与<$ c $中找到的标识符相对应的 df.1 的配对数据c> df.2 $ C 与 df.1 $ A df.1 $ B 或这两者。

解决方案

您可以使用?% (类似于?匹配):

  df1<  -  data.frame(A = sample(1:10,10),B = sample(1:10,10))
df2 < - data.frame(C = 1:5)

selectedRows< - (df1 $ A%in%df2 $ C | df1 $ B%in%df2 $ C)

dfReduced< - df1 [selectedRows,]


I have two data frames. The first, df.1, contains two columns of paired numerical identifiers, where each column includes ~100,000 rows. The second data frame, df.2, includes one column (df.2$C) of numerical identifiers. This data frame has around 200 rows.

How can I find the paired subset of data of df.1 that includes only the rows with values of the identifiers found in df.2$C?

The final subset would include the paired data of df.1 which corresponds to identifiers found in df.2$C that match the identifiers found in df.1$A, df.1$B or both.

解决方案

You could use ?"%in%" (similar to ?match):

df1 <- data.frame(A=sample(1:10, 10), B=sample(1:10, 10))
df2 <- data.frame(C=1:5)

selectedRows <- (df1$A %in% df2$C | df1$B %in% df2$C)

dfReduced <- df1[selectedRows,]

这篇关于在R中,如何通过另一个data.frame的值对数据框架进行子集化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆