-2

Hello i want to import a . csv file in r, i have the following code :

fbmonthly<-read.zoo("E:\\R\\Stockforecast\\Data\\AAPLmonthly.csv",sep=",",header= TRUE, format = '%m/%Y', FUN=as.Date)

Although i have this error :

Error in read.zoo("E:\R\Stockforecast\Data\AAPLmonthly.csv", sep = ",", : index has bad entries at data rows: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

my csv file looks like 01-03-13 0 0 0 0 0 0 01-04-13 63.128571 63.607143 55.014286 63.254284 47.519821 2740872400 01-05-13 63.494286 66.535713 59.842857 64.247147 48.265705 2361882600 01-06-13 64.389999 64.918571 55.552856 56.647144 44.609531 1754634000 01-07-13 57.527142 65.334282 57.317142 64.647141 50.909512 1634528700 01-08-13 65.10714 73.391426 64.751427 69.602859 54.812115 2014584600 01-09-13 70.442856 72.559998 63.888573 68.10714 56.215424 2157735300 01-10-13 68.349998 77.035713 68.325714 74.671425 61.633572 1959433000 01-11-13 74.860001 79.761429 73.197144 79.438568 65.568352 1306288900 01-12-13 79.714287 82.162857 76.971428 80.145714 68.953758 1764349300 01-01-14 79.382858 80.028572 70.507141 71.514282 61.527653 2191488600 01-02-14 71.80143 78.741432 71.328575 75.177139 64.679031 1470091700 01-03-14 74.774284 78.428574 74.687141 76.677139 68.836685 1250424700 01-04-14 76.822861 85.632858 73.047142 84.298569 75.678795 1608765200 01-05-14 84.571426 92.024284 82.904289 90.428574 81.181992 1433917100 01-06-14 90.565712 95.050003 88.928574 92.93 86.802559 1206934800 01-07-14 93.519997 99.440002 92.57 95.599998 89.296494 1035086000 01-08-14 94.900002 102.900002 93.279999 102.5 95.741524 937077000

can you please help me? Thanks

  • Umm.... where are the commas? – Martin James Mar 24 '18 at 19:27
  • That is an obvious question but read.zoo inherits its default separator from read.file rather than from read.csv, so that's not the cause of the error. It also inherits its default for header and that would be an error, as well as the more glaring error with the format spec – IRTFM Mar 24 '18 at 20:00
  • Correction: inherits from `read.table` – IRTFM Mar 24 '18 at 20:07
  • I'm only nominating the question for reopening from a sense of "justice". I thought the question had enough information to support a valid answer. The lack of commas in the example is not sufficient for closure. I suppose if the questioner refrains from accepting one of the answers we can imagine that none of them are correct. – IRTFM Mar 27 '18 at 04:03

3 Answers3

2

It's not a "CSV" file. It's delimiter appears to be whitespace, which is what read.zoo would use by default. No header, also the default for read.zoo. Need to correct the date format:

read.zoo(text="01-03-13    0   0   0   0   0   0
 01-04-13    63.128571   63.607143   55.014286   63.254284   47.519821   2740872400", 
 format = '%m-%d-%y')
                 V2       V3       V4       V5       V6         V7
2013-01-03  0.00000  0.00000  0.00000  0.00000  0.00000          0
2013-01-04 63.12857 63.60714 55.01429 63.25428 47.51982 2740872400
IRTFM
  • 258,963
  • 21
  • 364
  • 487
-1
   fbmonthly<-read.zoo("E:\\R\\Stockforecast\\Data\\AAPLmonthly.csv",sep=",",header= TRUE, format = '%d-%m-%Y', FUN=as.Date)

If you have header and comma separated file it seems you have the date format wrong.

johnzarifis
  • 362
  • 1
  • 4
  • Wrong date format. Wrong sep. No header. – IRTFM Mar 24 '18 at 19:57
  • I think the Yahoo API went out of service almost 1 year ago. I think you should use Quantmod for this kind of task. https://github.com/joshuaulrich/quantmod/blob/master/demo/chartSeries.R https://www.r-bloggers.com/a-guide-on-r-quantmod-package-how-to-get-started/ – ASH Mar 27 '18 at 01:55
-1

so you say to use fbmonthly<-read.zoo("E:\\R\\Stockforecast\\Data\\AAPLmonthly.csv",sep=".",header= TRUE, format = '%m-%Y', FUN=as.yearmon)

instead?