seema aswani
seema aswani

Reputation: 177

Filtering the dataframe by matching values of two columns

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

Upvotes: 4

Views: 7988

Answers (3)

Lorenzo Rossi
Lorenzo Rossi

Reputation: 1481

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)

Upvotes: 5

bramtayl
bramtayl

Reputation: 4024

Or,

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

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

Upvotes: 2

giac
giac

Reputation: 4299

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.

Upvotes: 4

Related Questions