Filtering the dataframe by matching values of two

2020-07-22 09:30发布

I am having a dataframe in r. I want to delete those rows where the values of a string in two columns are equal. I used match function in r but was not able to get desired output. For example my dataframe is

ALDH1A1 ALDH1A1
ITGA7   CHRNA1
PPP1R9A ACTG1
SRGN    SRGN
GRB7    ERBB2
PAK1    ERBB2
DLG4     DLG4
PIK3R2   ERBB2
PTPN18   ERBB2
ERBB2    ERBB2
SMURF2   ARHGAP5
 NF2    ERBB2
 CD82    CD82
 ERRFI1 ERBB2
 CD44    CD44
 TOB1   TOB1

and my desired data frame after filtering out the rows with equal column values is

ITGA7    CHRNA1
PPP1R9A ACTG1
GRB7    ERBB2
PAK1    ERBB2
PIK3R2  ERBB2
PTPN18  ERBB2
SMURF2  ARHGAP5
 NF2    ERBB2
 ERRFI1 ERBB2

标签: r dataframe
3条回答
神经病院院长
2楼-- · 2020-07-22 09:55

Assuming that you have your data into an R object called df with columns V1 and V2, you can achieve this very simply with dplyr doing

library(dplyr)
df = filter(df, V1 != V2)
查看更多
叛逆
3楼-- · 2020-07-22 09:59

Or,

library(dplyr)
result = dta %>% filter(V1 != V2)

where V1 and V2 are the names of the columns, unquoted.

查看更多
放荡不羁爱自由
4楼-- · 2020-07-22 10:01

Let us imagine your dataset is called dta

then simply

dta[which(dta[,1] != dta[,2]), ]

Please provide the dput in order to reproduce your example.

查看更多
登录 后发表回答