user3253470
user3253470

Reputation: 201

Comparing elements of data.frames in R

I have 2 data frames

> a1
v1  v2  v3
ABCA1   --> GIF
ACTA1   --| CSNK2A1
ACTN4   --| HDAC7
ACTN4   --> RARA

> a2
v1  v2  v3
ACTA1   --| CSNK2A1
ABCD2   --| HDAC7
ABCA1   --| GIF
ACTN4   --> XYZ1      

I want output where a1$element1 == a2$element1 & a1$element3 == a2$element3 & a1$element2 != a2$element2. So, the outcome will be:

> a3
ABCA1   --> GIF

As all elements in this row fulfills our all 3 conditions. It is row 1 in a1 and row 3 in a2. As you can see (row 1 of a1 and row 3 of a2) ABCA1 == ABCA1 & --> != --| & GIF == GIF

Upvotes: 1

Views: 76

Answers (2)

Pierre L
Pierre L

Reputation: 28441

You can try merge:

m2 <- merge(a1, a2, by=c("v1", "v3"))
m2[m2[,3] != m2[,4],][c(1,3,2)]
#     v1 v2.x  v3
#1 ABCA1  --> GIF

We first merge on columns 1 and 3. That will connect the cases of the first two conditions. For the last condition, test for the cases where the column symbols are different.

Also stringsAsFactors should be set to FALSE when creating the data frames.


With data.table, you can merge using its shallow copying efficiency.

setDT(a1)[a2, on = c("v1", "v3")][v2 != i.v2]
      v1  v2  v3 i.v2
1: ABCA1 --> GIF  --|

Upvotes: 3

LyzandeR
LyzandeR

Reputation: 37879

This seems to work for me:

a1[ intersect(which((paste0(a1$v1, a1$v3) %in% paste0(a2$v1, a2$v3))),
              which(a1$v2 != a2$v2)) ,]

#     v1  v2  v3
#1 ABCA1 --> GIF

First, find which rows in a1 and a2 have common columns 1 and 3. Then find which rows do not have the second column in common. Then get the intersect of the two and use this to subset a1.

Data:

a1 <- read.table(header=T,text='v1  v2  v3
ABCA1   --> GIF
ACTA1   --| CSNK2A1
ACTN4   --| HDAC7
ACTN4   --> RARA', stringsAsFactors=F)

a2 <- read.table(header=T,text='v1  v2  v3
ACTA1   --| CSNK2A1
ABCD2   --| HDAC7
ABCA1   --| GIF
ACTN4   --> XYZ1', stringsAsFactors=F)

Upvotes: 2

Related Questions