0

I have a dataset like this

data <- data.frame(Time=as.Date(c("2007-01-31", "2007-02-28", "2007-03-31", 
        "2007-04-30", "2007-05-31"), format="%Y-%m-%d"), 
        a=c(104.8587, 104.5483, 104.0210, 105.7185, 104.9054), 
        b=c(95.4, 95.9, 95.6, 95.5, 95.8))


> data
    Time        a    b
1 2007-01-31 104.8587 95.4
2 2007-02-28 104.5483 95.9
3 2007-03-31 104.0210 95.6
4 2007-04-30 105.7185 95.5
5 2007-05-31 104.9054 95.8

Is used

ddata <- data.frame(diff(as.matrix(data[,-1])))
ddata$Time <- data[-1,1]
ddata <- ddata[c(3, 1:2)]

to take the differences.

> ddata
    Time       a    b
1 2007-02-28 -0.3104  0.5
2 2007-03-31 -0.5273 -0.3
3 2007-04-30  1.6975 -0.1
4 2007-05-31 -0.8131  0.3

Then I used write.csv(ddata, file="ddata.csv", row.names=FALSE) to save it into a csv file. However, if I reload it, the values change.

> ddata <- read.table(file="ddata.csv", dec=",", header=T, sep=",")
> ddata
    Time                  a                   b
1 2007-02-28 -0.310400000000001                 0.5
2 2007-03-31 -0.527299999999997  -0.300000000000011
3 2007-04-30   1.69750000000001 -0.0999999999999943
4 2007-05-31 -0.813100000000006   0.299999999999997

The rounded values are still the same, but the write.csv command added some decimals. What happenend?

nelakell
  • 189
  • 2
  • 13
  • 1
    You specify `","` to be both the separator and the decimal point – talat Jul 20 '15 at 12:03
  • Why not use `read.csv()`? – zx8754 Jul 20 '15 at 12:05
  • The values didn't change. By default, `print` outputs 7 digits of a numeric value. In addition, trailing zeros are cut. Thus, you see only `-0.3104` from `-0.310400000000001...`. You can change the number of digits printed to 22 by calling `options("digits" = 22)`. – lukeA Jul 20 '15 at 12:14
  • Thank you @docendodiscimus this solved the problem, although I still don´t get the extra digits in the csv file. – nelakell Jul 20 '15 at 13:03

1 Answers1

0

Your write.csv action, exports the data as is, though slightly rounded of.

If you set the digits to 20 you will see that what you think is -0.3 is in fact -0.300000000000011369.

options(digits = 20)
ddata
    Time                    a                     b
1 2007-02-28 -0.31040000000000134  0.500000000000000000
2 2007-03-31 -0.52729999999999677 -0.300000000000011369
3 2007-04-30  1.69750000000000512 -0.099999999999994316
4 2007-05-31 -0.81310000000000571  0.299999999999997158
phiver
  • 23,048
  • 14
  • 44
  • 56