I have 2 data frames
> a1
v1 v2 v3
ABCA1 --> GIF
ACTA1 --| CSNK2A1
ACTN4 --| HDAC7
ACTN4 --> RARA
> a2
v1 v2 v3
ABCA1 --| GIF
ACTA1 --| CSNK2A1
ABCD2 --| HDAC7
ACTN4 --> XYZ1
I want output where a1$v1 == a2$v1
&& a1$v3 == a2$v3
&& a1$v2 != a2$v2
. So, the outcome will be:
> a3
ABCA1 --> GIF
Because Row 1 fulfill all those conditions. In row 2 condition 3
is not fulfilled. In row 3 condition 1
is not fulfilled and in row 4 condition 2
is not fulfilled.
If we are comparing the 'v1' column in 'a1' and 'a2' datasets, and use &
instead of &&
, we get the expected output
a1[(a1$v1==a2$v1) & (a1$v3==a2$v3) & (a1$v2 != a2$v2), , drop=FALSE]
# v1 v2 v3
#1 ABCA1 --> GIF
According to the description from ?"&&"
'&' and '&&' indicate logical AND and '|' and '||' indicate logical OR. The shorter form performs elementwise comparisons in much the same way as arithmetic operators. The longer form evaluates left to right examining only the first element of each vector.
If we need to compare one row in 'a1' against all the rows, we can paste
the rows in each datasets using do.call(paste,..
, and loop through lapply
on the paste
elements of 'a1' and compare against the paste
d 'a2' or this can be done using outer
.
lapply(do.call(paste, a1), '==', do.call(paste, a2))
Or
outer(do.call(paste, a1), do.call(paste, a2), '==')
a1 <- structure(list(v1 = c("ABCA1", "ACTA1", "ACTN4", "ACTN4"),
v2 = c("-->",
"--|", "--|", "-->"), v3 = c("GIF", "CSNK2A1", "HDAC7", "RARA"
)), .Names = c("v1", "v2", "v3"), class = "data.frame",
row.names = c(NA, -4L))
a2 <- structure(list(v1 = c("ABCA1", "ACTA1", "ABCD2", "ACTN4"),
v2 = c("--|",
"--|", "--|", "-->"), v3 = c("GIF", "CSNK2A1", "HDAC7", "XYZ1"
)), .Names = c("v1", "v2", "v3"), class = "data.frame",
row.names = c(NA, -4L))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.