Remove rows when cells are equal [duplicate]

做~自己de王妃 提交于 2019-12-03 00:47:38

问题


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,]


来源:https://stackoverflow.com/questions/42292597/remove-rows-when-cells-are-equal

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