R Subsetting a data.frame when 2 columns have different values

蹲街弑〆低调 提交于 2019-12-20 03:52:41

问题


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

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.

Thanks!


回答1:


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


来源:https://stackoverflow.com/questions/25826743/r-subsetting-a-data-frame-when-2-columns-have-different-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!