0

Thinking about a data.frame lets say:

df <- data_frame(id = c(1, 1, 2, 2), n = c("100+", "50+", "30+", "40+"))

Column n should be to converted to c("100","50","30","40") by using str_replace() within dplyr library.

I need something like this to work on:

library(dplyr)
library(stringr)

df %>%
  mutate(n = str_replace("+",""))

There must be a proper way to apply str_replace() function for a column with dplyr.

Sotos
  • 51,121
  • 6
  • 32
  • 66
ahmetg
  • 117
  • 1
  • 4
  • 13

1 Answers1

9

You need to pass the column you want to perform the replacement on, and then escape the plus symbol.

library(dplyr)
library(stringr)

df %>%
  mutate(n = str_replace(n, "\\+",""))
Sotos
  • 51,121
  • 6
  • 32
  • 66
Kara Woo
  • 3,595
  • 19
  • 31