Home > Software engineering >  Mutate all variables that contain ′ to '? and other symbols to desired one?
Mutate all variables that contain ′ to '? and other symbols to desired one?

Time:07-02

How do I mutate all variables that contain ′ to ' using R?

df <- data.frame(
S=c("1′,@","2′2′abc","3:ae′","′4~@e′","55′","6:ae′"),
Q=c("AAA′E","BEAA′","CA′′B","D:DDF","EE@′A","FFSS")
)
        S     Q
1    1′,@ AAA′E
2 2′2′abc BEAA′
3   3:ae′ CA′′B
4  ′4~@e′ D:DDF
5     55′ EE@′A
6   6:ae′  FFSS

Desired output:

        S     Q
1    1',@ AAA'E
2 2'2'abc BEAA'
3   3:ae' CA''B
4  ′4~@e' D:DDF
5     55' EE@'A
6   6:ae'  FFSS

Here's what I have tried:

library(tidyverse)
library(dplyr)

df %>%
  mutate(
    dplyr::across(
      .cols = everything(), 
      .fns = ~ dplyr::if_else(stringr::str_detect(.x, "′"), "'", .x)
    )
  )

which returns

   S     Q
1 '     '
2 '     '
3 '     '
4 ' D:DDF
5 '     '
6 '  FFSS

Can someone advise how to change the syntax to make it work?

And it will be extremely helpful if solution is extended to also mutate

: to ^

@ in df$S to a

AA to aa

as it will then apply to most cases that I can think of.

Thanking in advance.

CodePudding user response:

Yo should use gsub or stringr::str_replace_all instead of ifelse.

library(dplyr)

df %>%
  mutate(across(everything(), ~ gsub("′", "'", .x)))

#         S     Q
# 1    1',@ AAA'E
# 2 2'2'abc BEAA'
# 3   3:ae' CA''B
# 4  '4~@e' D:DDF
# 5     55' EE@'A
# 6   6:ae'  FFSS

To perform multiple replacements in each element of string, pass a named vector (c(pattern1 = replacement1)) to str_replace_all.

df %>%
  mutate(
    S = str_replace_all(S, "@", "a"),
    across(everything(), ~ str_replace_all(.x, c("′" = "'", ":" = "\\^", "AA" = "aa")))
  )

#         S     Q
# 1    1',a aaA'E
# 2 2'2'abc BEaa'
# 3   3^ae' CA''B
# 4  '4~ae' D^DDF
# 5     55' EE@'A
# 6   6^ae'  FFSS
  • Related