0

I have a time series data which have currency data in one column. The data have '$' sign in this. How can I remove all the $ from the currency value.

My data :

        Date Dividend dat
1 1980-12-23   $0.001  26
2 1981-03-24   $0.001  27
3 1981-06-16   $0.001  28
4 1981-08-31   $0.001  29
5 1981-10-05   $0.003  30
6 1982-03-23   $0.001  31

I want data like this:

        Date Dividend dat
1 1980-12-23   0.001  26
2 1981-03-24   0.001  27
3 1981-06-16   0.001  28
4 1981-08-31   0.001  29
5 1981-10-05   0.003  30
6 1982-03-23   0.001  31
Gini
  • 1
  • 4

2 Answers2

1

sub with fixed=TRUE will substitute one literal string for another. You need fixed=TRUE as $ is a special character, with meaning in a regular expression (it indicates the end of the string).

transform makes the syntax a bit better for the command. transform does not alter the data frame, so you would need to use the return value, perhaps assigning it back to the original variable.

transform(d, Dividend = sub('$', '', Dividend, fixed=TRUE))
        Date Dividend dat
1 1980-12-23    0.001  26
2 1981-03-24    0.001  27
3 1981-06-16    0.001  28
4 1981-08-31    0.001  29
5 1981-10-05    0.003  30
6 1982-03-23    0.001  31
Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
0

Use gsub with fixed=TRUE as explained by @Mathew.

ginidata <- read.table(text = "Date Dividend dat
1 1980-12-23   $0.001  26
2 1981-03-24   $0.001  27
3 1981-06-16   $0.001  28
4 1981-08-31   $0.001  29
5 1981-10-05   $0.003  30
6 1982-03-23   $0.001  31")
ginidata$Dividend <- gsub("$","",ginidata$Dividend, fixed = TRUE)

ginidata
#         Date Dividend dat
# 1 1980-12-23    0.001  26
# 2 1981-03-24    0.001  27
# 3 1981-06-16    0.001  28
# 4 1981-08-31    0.001  29
# 5 1981-10-05    0.003  30
# 6 1982-03-23    0.001  31
Paul Rougieux
  • 10,289
  • 4
  • 68
  • 110