0

I currently have a dataframe of stock KPIs and I would like to remove the "$" character from the data. However, I can only use one line of code in addition to the mandatory usage of the stringi package. Looking at the documentation, the "stri_replace_all_fixed" was the function that stood out to me, but upon running that function, my data frame lost its formatting. I tried combining both the lapply and the stri_replace_all_fixed functions to no avail. Pointers on how to address this problem would be much appreciated.

Jaap
  • 81,064
  • 34
  • 182
  • 193

3 Answers3

0

With library(stringi)

yourdataframe[] <- lapply(yourdataframe, stri_replace_all_regex,"\\$", "")
CER
  • 854
  • 10
  • 22
0
# Dummy data
dta <- data.frame(group = rep(LETTERS[1:5], 10)) %>%
    mutate(value = sample(1:10, 50, replace = TRUE) %>% paste("$"))

# scrub a dub (using dplyr)
dta %>% mutate_all(funs(stri_replace_all_fixed(., "$", "")))

and it is one of the faster approaches, too: replacement drag races

leerssej
  • 14,260
  • 6
  • 48
  • 57
0

An update (as November 2022) on @leerssej ansewer as funs() was deprecated:

# Dummy data
dta <- data.frame(group = rep(LETTERS[1:5], 10)) %>%
    mutate(value = sample(1:10, 50, replace = TRUE) %>% paste("$"))

# scrub a dub (using dplyr)
dta %>% mutate_all(~stringi::stri_replace_all_fixed(., "$", ""))