0
votes

I have a data frame of apple stock prices YTD as below file name appl.csv

Date Close.Last   Volume   Open   High    Low
1 12/20/2019     279.44 68972090 282.23 282.65 278.56
2 12/19/2019     280.02 24626950 279.50 281.18 278.95
3 12/18/2019     279.74 29024690 279.80 281.90 279.12
4 12/17/2019     280.41 28575800 279.57 281.77 278.80
5 12/16/2019     279.86 32081110 277.00 280.79 276.98
6 12/13/2019     275.15 33432810 271.46 275.30 270.93

I would like to transform the above data into a zoo time series object so that I can use the power of time series analysis. I have tried writing the code as below, but not getting the results. Can anyone help me understand the mistake that I am committing in the code below.

mydata1 <- read.zoo("appl.csv",sep="," ,FUN,header =TRUE,format ="%m-%d-%y")

r gives me the following results

mydata1 <-read.zoo("appl.csv", format = "%m-%d-%y", tz = "", FUN = NULL,
+          regular = FALSE, index.column = 1, drop = TRUE, FUN2 = NULL,
+          split = NULL, aggregate = FALSE, read = read.table,sep=",",header =TRUE)

Error in read.zoo("appl.csv", format = "%m-%d-%y", tz = "", FUN = NULL, : index has 145 bad entries at data rows: 1 2 3 4 5 6 16 17 18 19 20 21 22 23 24 25 26 27 36 37 38 39 40 41 42 43 44 45 46 47 48 49 59 60 61 62 63 64 65 66 67 68 69 70 79 80 81 82 83 84 85 86 87 88 89 90 91 92 101 102 103 104 105 106 107 108 109 110 111 112 113 123 124 125 126 127 128 129 130 131 132 133 134 143 144 145 146 147 148 149 150 151 152 153 154 155 156 165 166 167 ...

Kindly let me know how to use read.zoo function

1
your file's Date field is in %m/%d/%Y format.user2474226

1 Answers

0
votes

The question did not show the input file format, only what it looks like after it was read in in a manner also not shown, so we can't be sure what you have but here are some comments based on the assumption that you have a csv file:

  1. the dates shown in the question have slashes but the format specifies dashes in the code in the question.

  2. The dates shown in the question have 4 digit years but the format used in the question specifies 2 digit years -- lower case y means 2 digits, upper case Y means 4 digits

  3. read.csv.zoo is provided in the zoo package and should be used for csv files to save having to specify the header= and sep= arguments.

  4. The filename shown in the question is appl.csv but the ticker of Apple stock is aapl, not appl. We have used appl.csv below assuming that that really is your file name but you may want to change both the filename and the code.

  5. FUN is written without a value. In fact it is not needed at all here. The default will do but at any rate you can't simply write an argument name without an equal sign and a value after it.

Keeping the above in mind we can write

read.csv.zoo("appl.csv", format ="%m/%d/%Y")

Also note that if you have already read it in as a data frame DF so that it looks like that shown in the question we can do this:

read.table(DF, format = "%m/%d/%Y")