R当2列具有不同的值时,设置数据框架 [英] R Subsetting a data.frame when 2 columns have different values

查看:115
本文介绍了R当2列具有不同的值时,设置数据框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数据框架:

I have a data.frame like this:

          Type1 rep1    Type2 rep2    stat p.value
    17    DqSAD    1 rnzDqSAD    9  3.7946  0.0101
    18    DqSAD    1    DqSAD   10 -0.5278  0.6428
    19    DqSAD    1 rnzDqSAD   10  0.4111  0.2231
    20 rnzDqSAD    1    DqSAD    2 -0.3111  0.5085
    21 rnzDqSAD    1 rnzDqSAD    2 -0.8904  0.9080

当我们将列1和类型2具有不同的值。我的意思是以自动的方式,没有明确地检查这个特定的值,如Type1 ==DqSAD& Type2 ==rnzDqSAD我记得这可以用sql完成,但是我不知道如何在R中执行。

and I would like to subset it when the columns Type1 & Type 2 have different values. I mean in an automatic way, not explicitly checking for this particular values like Type1=="DqSAD" & Type2=="rnzDqSAD" I remember this could be done with sql, but I don't figure out how to do it in R.

谢谢!

推荐答案

您可以通过查找 Type1 Type2 不等于!= 逻辑运算符。如果 df 是数据,

You can do this by finding the rows where Type1 and Type2 are not equal with the != logical operator. If df is the data,

> df[with(df, Type1 != Type2), ]
#       Type1 rep1    Type2 rep2    stat p.value
# 17    DqSAD    1 rnzDqSAD    9  3.7946  0.0101
# 19    DqSAD    1 rnzDqSAD   10  0.4111  0.2231
# 20 rnzDqSAD    1    DqSAD    2 -0.3111  0.5085

这篇关于R当2列具有不同的值时,设置数据框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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