Hi,

I am calling in some data into R via the read.table function. The 'tail' of
this data reads

> tail(data)

        Date                   Open   High    Low  Close  Volume
3728 Fri 14-Sep-07     114.19 114.46 113.93 114.08 1117992
3729 Mon 17-Sep-07   113.99 114.37 113.91 114.27  899671
3730 Tue 18-Sep-07    114.30 114.32 113.47 113.85 1346282
3731 Wed 19-Sep-07   113.71 113.71 113.00 113.16 1499423
3732 Thu 20-Sep-07    112.98 113.24 112.28 112.30 1376946
3733 Fri 21-Sep-07      112.41 112.64 112.24 112.47 1052431

The data is saved in a CSV file, which according to Notepad looks like

Fri 14-Sep-07,114.19,114.46,113.93,114.08,1117992
Mon 17-Sep-07,113.99,114.37,113.91,114.27,899671
Tue 18-Sep-07,114.3,114.32,113.47,113.85,1346282
Wed 19-Sep-07,113.71,113.71,113,113.16,1499423
Thu 20-Sep-07,112.98,113.24,112.28,112.3,1376946
Fri 21-Sep-07,112.41,112.64,112.24,112.47,1052431

i.e. all the data is saved to 2 decimal places. To calculate the net change
of the 'Close' variable in the last row I would use

> data[3733,5]-data[3732,5]
[1] 0.17

which is correct. However, when I use the following code to calculate the
net change over all observations i get the following result

> n <- length(data[,1])
> temp <- data[2:n,5]-data[1:(n-1),5]
> tail(temp)

0.039999999999992
0.189999999999998
-0.420000000000002
-0.689999999999998
-0.86
0.170000000000002

When subtracting,  all numbers should be to 2 decimal places. Why is R
calculating it to 15 decimal places -- the output is essentially wrong
112.47-112.30=0.17 NOT 0.170000000000002. I suspect I am encoding this
incorrectly?

Any help would be gratefully appreciated.

Kind regards,

Sam

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to