Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get matching characters?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading