so when v1 and v2 match in both df1 and df2 it mutates COE and otherwise 0, same for lead
I am not sure I follow. Would you mind following up with additional clarity as to exactly what you're trying to do with this and ifelse statement or something else?.
The values in v1
and v2
in correct
are integers, but appear at characters in df1
and df2
. So it's hard to use that as a guide for the operations required to go from your initial two data frames to correct
.
As nirgrahamuk mentioned in ifelse statement or something else?, set operations like those discussed in 13 Relational data | R for Data Science are a good way to approach this problem. For example, ``` r
library(dplyr)
df1<- structure(list(v1 = c("a", "b", "a"), v2 = c("b", "d", "e"),
rule = c("COE", "COE", "COE")), class = "data.frame", row.names = c(NA,
-3L))
df2 <- structure(list(v1 = c("a", "c", "g"), v2 = c("b", "e", "d")), class = "data.frame", row.names = c(NA,-3L))
#those in both
intersect(df1 %>% select(v1, v2),
df2 %>% select(v1, v2))
#> v1 v2
#> 1 a b
# in df1 but not in df2
setdiff(df1 %>% select(v1, v2),
df2 %>% select(v1, v2))
#> v1 v2
#> 1 b d
#> 2 a e
# in df2 but not in df1
setdiff(df2 %>% select(v1, v2),
df1 %>% select(v1, v2))
#> v1 v2
#> 1 c e
#> 2 g d
Created on 2021-12-20 by the reprex package (v2.0.0)