0

Having a dataframe like this:

df <- data.frame(id = c(1,2), date1 = c("Nov 2016 <U+2192> Current", "Nov 2016 <U+2192> Current"), date2 = c("Nov 2016 <U+2192> Current", "Nov 2016 <U+2192> Current"))

Is there any command to replace this character in the whole dataframe?

Example:

df <- gsub(' <U+2192> ', '-', df)
3
  • df$date1 <- gsub(' <U+2192> ', '-', df$date1)? Commented Oct 22, 2022 at 12:34
  • @PanagiotisTogias for the whole dataframe not for one column Commented Oct 22, 2022 at 12:36
  • 1
    with dplyr : df %>% mutate(across(everything(), ~ gsub(' <U+2192> ', '-', ., fixed = TRUE))) Commented Oct 22, 2022 at 12:41

2 Answers 2

1

Placing it here also as an answer:

library(dplyr)

df %>% mutate(across(everything(), ~ gsub(' <U+2192> ', '-', ., fixed = TRUE)))

Output

  id            date1            date2
1  1 Nov 2016-Current Nov 2016-Current
2  2 Nov 2016-Current Nov 2016-Current
Sign up to request clarification or add additional context in comments.

Comments

0

A base R solution with sapply:

df <- sapply(df, function(x) gsub(" <U\\+2192>", " -", x))

Note that + must be escaped unless you use the fixed = TRUE argument

df
     id  date1                date2               
[1,] "1" "Nov 2016 - Current" "Nov 2016 - Current"
[2,] "2" "Nov 2016 - Current" "Nov 2016 - Current"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.