Home > Blockchain >  How to find a match within a string based on character from other column?
How to find a match within a string based on character from other column?

Time:12-09

I have these data:

df1 <- data.frame(matrix(, nrow=2, ncol=2))
colnames(df1) <- c("ca", "ea")
df1$ca <- c("A=C,T=G", "T=C,G=G")
df1$ea <- c("G", "T")

And I want to make a new column called "match" which gives me the letter in column "ca" that is equal to the letter in column "ea". So my output would look like this:

df1 <- data.frame(matrix(, nrow=2, ncol=2))
colnames(df1) <- c("ca", "ea")
df1$ca <- c("A=C,T=G", "T=C,G=G")
df1$ea <- c("G", "T")
df1$match <- c("T", "C")

It is tricky because in the first instance the letter I want to match on is after the "=", but in the second instance it precedes it.

CodePudding user response:

Here's another tidyverse solution that might a little bit simpler using regular expressions. You need to have R > 4.0 to use the |> pipe operator, if that's not the case just substitute it by %>%.

library(tidyverse)

df1 |>
  # add a named match column as an extracted string by the following 
  # two possible patterns
  mutate(match = str_extract(ca, 
                             # Search for the letter preceded by ea=
                             paste0(paste0("(?<=",ea,"\\=)","[A-Z]"),
                                    # or
                                    "|",
                                    # search for the letter followed by =ea
                                    paste0("[A-Z]","(?=\\=",ea,")"))))

#        ca ea match
# 1 A=C,T=G  G     T
# 2 T=C,G=G  T     C

CodePudding user response:

I believe this should work for you :

df1 <- data.frame(matrix(, nrow=2, ncol=2))
colnames(df1) <- c("ca", "ea")
df1$ca <- c("A=C,T=G", "T=C,G=G")
df1$ea <- c("G", "T")

my_f <- function(x) {
  my_pattern <- paste("[ACGT]=", df1[x, "ea"], "|", df1[x, "ea"], "=[ACGT]", sep 
= "")
  my_a <- str_extract_all(string = df1[x, "ca"], pattern = my_pattern, simplify = TRUE)
  my_pattern <- paste(df1[x, "ea"], "|=", sep = "")
  my_a <- gsub(pattern = my_pattern, replacement = "", x = my_a)
  return (my_a)
}
df1$match <- lapply(1:nrow(df1), my_f)

CodePudding user response:

Good opportunity for a simple branch reset group

df1 <- data.frame(matrix(, nrow=2, ncol=2))
colnames(df1) <- c("ca", "ea")
df1$ca <- c("A=C,T=G", "T=C,G=G")
df1$ea <- c("G", "T")
df1$match <- c("T", "C")

mapply(
  function(p, x)
  gsub(sprintf('(?|%s=(.)|(.)=%s)|.', p, p), '\\1', x, perl = TRUE),
  df1$ea, df1$ca, USE.NAMES = FALSE
)
# [1] "T" "C"

CodePudding user response:

library(tidyverse)

df1 <- data.frame(matrix(, nrow = 2, ncol = 2))
colnames(df1) <- c("ca", "ea")
df1$ca <- c("A=C,T=G", "T=C,G=G")
df1$ea <- c("G", "T")
df1
#>        ca ea
#> 1 A=C,T=G  G
#> 2 T=C,G=G  T

df1 %>%
  mutate(
    match = ea %>% map2_chr(ca, function(ea, ca) {
      ca %>%
        str_split(",") %>%
        simplify() %>%
        keep(~ str_detect(.x, ea)) %>%
        str_remove_all(str_glue("[=|{ea}]"))
    })
  )
#>        ca ea match
#> 1 A=C,T=G  G     T
#> 2 T=C,G=G  T     C

Created on 2021-12-08 by the reprex package (v2.0.1)

CodePudding user response:

Using tidyr::separate and dplyr::case_when.

df1 %>% separate(ca, into = c("ca1","ca2","ca3","ca4")) %>% 
  mutate(match = case_when(ea == ca1 ~ ca2,
                           ea == ca2 ~ ca1,
                           ea == ca3 ~ ca4,
                           ea == ca4 ~ ca3))

  ca1 ca2 ca3 ca4 ea match
1   A   C   T   G  G     T
2   T   C   G   G  T     C
  • Related