Search code examples
rstringdataframestr-replace

How do I remove the dollar sign with str_replace in a dataframe variable in R?


I have a dataframe in R with one variable called FARE that has a dollar sign. I need to remove the dollar sign in it. How do I remove it?

airline.df$FARE[1] --> returns "$84.23"

Solution

  • You can use the approach of replacing it with an empty char:

    str_replace(airline.df$FARE[1], "\\$", "");
    

    Or you can remove the first char ($) by creating the substring that starts with second character.

    fare = substring(airline.df$FARE[1], 2)