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?