Reputation: 10538
I have the following data:
x <- c("aaa", "bbb", "ccc", "ddd", "eee")
y <- c(1, 0, 0, 1, 1)
df1 <- data.frame(x,y)
x <- c("aaa", "bbb", "ccc", "ddd", "eee")
y <- c(1, 1, 1, 1, 0)
df2 <- data.frame(x,y)
Structure:
> df1
x y
1 aaa 1
2 bbb 0
3 ccc 0
4 ddd 1
5 eee 1
> df2
x y
1 aaa 1
2 bbb 1
3 ccc 1
4 ddd 1
5 eee 0
I'd like to track changes between df1
and df2
.
If I anti_join(df2,df1)
I can keep the things that have changed:
x y
eee 0
ccc 1
bbb 1
But I want to know WHAT and HOW things changed from df1
to df2
. For example:
x y.from y.to
eee 1 0
ccc 0 1
bbb 0 1
Thanks in advance.
Upvotes: 3
Views: 123
Reputation: 10538
Piggy backing off @zx8754 here is a hacky dplyr
version using filter()
:
library(dplyr)
df3 <- merge(df1,df2,by="x") %>%
filter(y.x != x.x)
# x y.x y.y
# 2 bbb 0 1
# 3 ccc 0 1
# 5 eee 1 0
Upvotes: 0
Reputation: 92292
Here's a similar data.table
approach
library(data.table)
setkey(setDT(df1), x)
df1[df2][y != i.y]
# x y i.y
# 1: bbb 0 1
# 2: ccc 0 1
# 3: eee 1 0
Upvotes: 3
Reputation: 56179
Try this:
#merge on column x
df3 <- merge(df1,df2,by="x")
#show the change
df3[ df3[,2] != df3[,3],]
# x y.x y.y
# 2 bbb 0 1
# 3 ccc 0 1
# 5 eee 1 0
Upvotes: 4