merge
.x
.y
列以确保它们相同。我在下面留下了一个可复制的代码示例,其中有一个输入示例和所需的输出。你可以看到我在用
dplyr
到
select
,
mutate
,和
filter
rbind
一起去弄一个
df
.
我想用一些
grep
x
年
然后
sapply
把它们都圈起来。有没有什么方法可以更优雅地做到这一点?
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))