library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
ucast12 <- data.frame(
OBEC = c(22,33,44,55, 66, 77, 88, 99)
)
transfer <- data.frame(
MOMC = c(20,33,44,50, 66, 77, 80, 99),
CISOB = c(22,30,40,50, 60, 70, 80, 90)
)
ucast12 %>%
if_else(OBEC=transfer$MOMC, OBEC=transfer$CISOB,OBEC=OBEC)
#> Error in if_else(., OBEC = transfer$MOMC, OBEC = transfer$CISOB, OBEC = OBEC): unused arguments (OBEC = transfer$MOMC, OBEC = transfer$CISOB, OBEC = OBEC)
I'd like to replace a column 'OBEC' in 'ucast12' by a column 'CISOB' in 'transfer' if OBEC equals MOMC otherwise keep 'OBEC' as it was.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
ucast12 <- data.frame(
OBEC = c(22,33,44,55, 66, 77, 88, 99)
)
transfer <- data.frame(
MOMC = c(20,33,44,50, 66, 77, 80, 99),
CISOB = c(22,30,40,50, 60, 70, 80, 90)
)
ucast12 %>%
if_else(OBEC=transfer$MOMC, OBEC=transfer$CISOB,OBEC=OBEC)
#> Error in if_else(., OBEC = transfer$MOMC, OBEC = transfer$CISOB, OBEC = OBEC): unused arguments (OBEC = transfer$MOMC, OBEC = transfer$CISOB, OBEC = OBEC)
See below. I adjusted your code slightly by merging the tables together (makes for cleaner syntax and you can just select the column you want again relatively easily). I did one mutate to show you it works. The other example show you that you can overwrite that column as well. Let me know if this solves it.