Remove rows when cells are equal [duplicate]

2019-03-07 13:42发布

This question already has an answer here:

I have a table:

df <- read.table(text=" 
a    b    5
a    a    2
c    a    3   
d    d    2    
a    a    1    
b    d    2   ")
colnames(df) <- c("Gen1","Gen2", "N")

I would like to remove the rows when Gen1 = Gen2. For example I would get for this example:

result <- read.table(text=" 
a    b    5
c    a    3     
b    d    2   ")
colnames(df) <- c("Gen1","Gen2", "N")

I tried with duplicated but duplicate is working per rows, not columns.

2条回答
smile是对你的礼貌
2楼-- · 2019-03-07 14:10

We can use subset

subset(df, Gen1!=Gen2)

Or filter from tidyverse

library(tidyverse)
df %>% 
    filter(Gen1 != Gen2)

data

df[1:2] <- lapply(df[1:2], as.character)
查看更多
神经病院院长
3楼-- · 2019-03-07 14:20

For large dataset you can use data.table:

library(data.table)
setDT(df)[Gen1!=Gen2,]
查看更多
登录 后发表回答