如何基于值向量对R中的数据帧中的行进行子集化? [英] How can I subset rows in a data frame in R based on a vector of values?

查看:146
本文介绍了如何基于值向量对R中的数据帧中的行进行子集化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据集,它们本来应该是相同的大小,但不一样.我需要修剪A中不在B中的值,反之亦然,以便从要进入报表的图形中消除噪声. (不用担心,该数据不会被永久删除!)

I have two data sets that are supposed to be the same size but aren't. I need to trim the values from A that are not in B and vice versa in order to eliminate noise from a graph that's going into a report. (Don't worry, this data isn't being permanently deleted!)

我已阅读以下内容:

  • Selecting columns in R data frame based on those *not* in a vector
  • http://www.ats.ucla.edu/stat/r/faq/subset_R.htm
  • How to combine multiple conditions to subset a data-frame using "OR"?

但是我仍然无法使它正常工作.这是我的代码:

But I'm still not able to get this to work right. Here's my code:

bg2011missingFromBeg <- setdiff(x=eg2011$ID, y=bg2011$ID)
#attempt 1
eg2011cleaned <- subset(eg2011, ID != bg2011missingFromBeg)
#attempt 2
eg2011cleaned <- eg2011[!eg2011$ID %in% bg2011missingFromBeg]

第一次尝试只是消除结果setdiff向量中的第一个值.第二次尝试会产生笨拙的错误:

The first try just eliminates the first value in the resulting setdiff vector. The second try yields and unwieldy error:

Error in `[.data.frame`(eg2012, !eg2012$ID %in% bg2012missingFromBeg) 
:  undefined columns selected

推荐答案

这将为您提供所需的内容:

This will give you what you want:

eg2011cleaned <- eg2011[!eg2011$ID %in% bg2011missingFromBeg, ]

第二次尝试中的错误是因为您忘记了,

The error in your second attempt is because you forgot the ,

通常,为了方便起见,规范object[index]将2d object的列作为子集.如果要对行进行子集并保留所有列,则必须使用规范 object[index_rows, index_columns],而index_cols可以保留为空白,默认情况下将使用所有列.

In general, for convenience, the specification object[index] subsets columns for a 2d object. If you want to subset rows and keep all columns you have to use the specification object[index_rows, index_columns], while index_cols can be left blank, which will use all columns by default.

但是,您仍然需要包含,来表示您想要获取行的子集而不是列的子集.

However, you still need to include the , to indicate that you want to get a subset of rows instead of a subset of columns.

这篇关于如何基于值向量对R中的数据帧中的行进行子集化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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