Remove rows when cells are equal [duplicate]

2019-03-07 13:23发布

问题:

This question already has an answer here:

  • Find rows in a data frame where two columns are equal 1 answer

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.

回答1:

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)


回答2:

For large dataset you can use data.table:

library(data.table)
setDT(df)[Gen1!=Gen2,]