I’m trying to get common characters from two separate vectors.
Example:
x <- c("abcde")
y <- c("efghi")
df <- data.frame(x, y)
Desired output
x y z
abcde efghi e
lmnop uvmxw m
I’ve tried something like this, but it is a bad result:
df |> mutate(m = unique(x, y))
If there are multiple matches, returning a list would work great.
>Solution :
str_intersect <- function(s1,s2) {
paste0(intersect(strsplit(s1,"")[[1]],strsplit(s2,"")[[1]]),collapse = "")
}
x <- c("abcde","abc")
y <- c("efghi","b")
df <- data.frame(x, y)
library(dplyr)
df %>%
rowwise() %>%
mutate(m = str_intersect(x,y))