I merge
two data frames and then want to compare all of .x
columns to all the .y
columns to ensure they are same. I left a reproducible code sample below with an input example and what the desired out put would be. You can see I am using dplyr
to select
, mutate
, and filter
for each column comparison and then rbind
together to get one df
.
I would like to be able to use some sort of grep
to call all the columns with .x
and .y
and then sapply
to loop over all of them. Is there any way to do this more elegantly?
library(dplyr)
df <- data.frame(FoundOn = letters[1:10], ItemId = 1:10,
Brand = rep(c("Nike", "UnderArmor"), 5),
Product = rep(c("shoes", "shorts", "hat", "shirt", "pants"),2),
color.x = c(rep(c("red", "green", "blue"), 3), "red"),
color.y = c(rep(c("blue", "green", "red", "red"), 2), "blue", "green"),
price.x = rep(c(100,50, 25, 60, 80), 2),
price.y = rep(c(125,50, 30, 60, 80), 2))
ColorCheck <- df %>%
select(FoundOn, ItemId, Brand, Product, color.x, color.y) %>%
mutate(Check = color.x == color.y, CheckFrom = "ColorCheck") %>%
filter(Check == F)
PriceCheck <- df %>%
select(FoundOn, ItemId, Brand, Product, price.x, price.y) %>%
mutate(Check = price.x == price.y, CheckFrom = "PriceCheck") %>%
filter(Check == F)
#desired output:
Checks <- rbind(ColorCheck %>% rename(From.x = color.x, From.y = color.y),
PriceCheck %>% rename(From.x = price.x, From.y = price.y))