0

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))
alexb523
  • 718
  • 2
  • 9
  • 26
  • 3
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jul 11 '18 at 21:26
  • Sorry! i think you called me out last time @MrFlick! I will work on my question representation. I am working on adding example. thanks. – alexb523 Jul 11 '18 at 22:33
  • i have updated with example. – alexb523 Jul 12 '18 at 20:40

0 Answers0