function to remove "-" in a string

Hello, new to programming and need some help. I have a data table with a column of numbers with "-" and I need to remove the dash.

Example of the number with the dash, 12-345-678

What I need it to look like 12345678

I have tried a few different functions but what I have tried has only removed one of the dashes.

Thanks

Posted Feb 11, 2020, 4:11 PM UTC

08
elgarteo95•
gsub("-", "", x)
2
unknown47OP•

So that worked but when I go view the data table the dashes are still there and if I print the column the dashes still show up. How can I make the change so the data table is changed as well.

1
elgarteo95•

Did you assign the output to the column?

2
unknown47OP•

No, I did not. Looking at the documentation for gsub and don't see how I can. How would I do that? Since the DT is a csv would I use the write.table function?

0
elgarteo95•

What I mean is you should assign the output of gsub to the original column of the data frame containing the numbers with dash:

df$x <- gsub("-", "", df$x)

Executing the function without assigning its output to any variable changes nothing.

5