str_replace_all 使用多个条件
str_replace_all using multiple conditions
我有两个数据框(“之前”、“之后”)。
before1 <- c("A", "B", "C")
before2 <- c("1", "2", "3")
before3 <- c("ab", "cd", "ef")
before <- data.frame(before1, before2, before3)
after1 <- c("A", "B")
after2 <- c("1", "2")
after3 <- c("de", "fg")
after <- data.frame(after1, after2, after3)
当 before1 和 before2 都匹配 after1 和 after3 时,我想用 after3 替换 before3。
下面是我要的结果
before1 <- c("A", "B" "C")
before2 <- c("1", "2", "3")
newafter <- c("de", "fg", "ef")
您可以加入两个数据帧并更新 coalesce
。
library(dplyr)
before %>%
left_join(after, by = c('before1' = 'after1', 'before2' = 'after2')) %>%
mutate(newafter = coalesce(after3, before3)) %>%
select(before1, before2, newafter)
# before1 before2 newafter
#1 A 1 de
#2 B 2 fg
#3 C 3 ef
我有两个数据框(“之前”、“之后”)。
before1 <- c("A", "B", "C")
before2 <- c("1", "2", "3")
before3 <- c("ab", "cd", "ef")
before <- data.frame(before1, before2, before3)
after1 <- c("A", "B")
after2 <- c("1", "2")
after3 <- c("de", "fg")
after <- data.frame(after1, after2, after3)
当 before1 和 before2 都匹配 after1 和 after3 时,我想用 after3 替换 before3。
下面是我要的结果
before1 <- c("A", "B" "C")
before2 <- c("1", "2", "3")
newafter <- c("de", "fg", "ef")
您可以加入两个数据帧并更新 coalesce
。
library(dplyr)
before %>%
left_join(after, by = c('before1' = 'after1', 'before2' = 'after2')) %>%
mutate(newafter = coalesce(after3, before3)) %>%
select(before1, before2, newafter)
# before1 before2 newafter
#1 A 1 de
#2 B 2 fg
#3 C 3 ef