Christofer Bogaso
2023-Aug-03 14:53 UTC
[R] Could not read time series data using read.zoo()
Hi, I have a CSV which contains data like below (only first few rows), Date Adj Close lret 02-01-1997 737.01 03-01-1997 748.03 1.48416235 06-01-1997 747.65 -0.050813009 07-01-1997 753.23 0.743567202 08-01-1997 748.41 -0.64196699 09-01-1997 754.85 0.856809786 10-01-1997 759.5 0.614126802 However when I try to read this data using below code I get error, read.zoo("1.csv", sep = ',', format = '%d-%m-%Y') Error reads as, index has 4500 bad entries at data rows: 1 2 3 4 5 6 7 8 9..... Could you please help to understand why I am getting this error?> sessionInfo()R version 4.2.2 (2022-10-31) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: macOS Big Sur ... 10.16 Matrix products: default BLAS: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib locale: [1] C/UTF-8/C/C/C/C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] zoo_1.8-12 loaded via a namespace (and not attached): [1] compiler_4.2.2 tools_4.2.2 grid_4.2.2 lattice_0.20-45
no commas? On August 3, 2023 7:53:07 AM PDT, Christofer Bogaso <bogaso.christofer at gmail.com> wrote:>Hi, > >I have a CSV which contains data like below (only first few rows), > >Date Adj Close lret >02-01-1997 737.01 >03-01-1997 748.03 1.48416235 >06-01-1997 747.65 -0.050813009 >07-01-1997 753.23 0.743567202 >08-01-1997 748.41 -0.64196699 >09-01-1997 754.85 0.856809786 >10-01-1997 759.5 0.614126802 > >However when I try to read this data using below code I get error, > >read.zoo("1.csv", sep = ',', format = '%d-%m-%Y') > >Error reads as, > >index has 4500 bad entries at data rows: 1 2 3 4 5 6 7 8 9..... > >Could you please help to understand why I am getting this error? > >> sessionInfo() > >R version 4.2.2 (2022-10-31) > >Platform: x86_64-apple-darwin17.0 (64-bit) > >Running under: macOS Big Sur ... 10.16 > > >Matrix products: default > >BLAS: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib > >LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib > > >locale: > >[1] C/UTF-8/C/C/C/C > > >attached base packages: > >[1] stats graphics grDevices utils datasets methods base > > >other attached packages: > >[1] zoo_1.8-12 > > >loaded via a namespace (and not attached): > >[1] compiler_4.2.2 tools_4.2.2 grid_4.2.2 lattice_0.20-45 > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.-- Sent from my phone. Please excuse my brevity.
One reason seems to be you are saying sep = "," and there is no "," in the file. Also you only have 3 columns of data but 4 variable names. On Thu, 3 Aug 2023 at 10:53, Christofer Bogaso <bogaso.christofer at gmail.com> wrote:> Hi, > > I have a CSV which contains data like below (only first few rows), > > Date Adj Close lret > 02-01-1997 737.01 > 03-01-1997 748.03 1.48416235 > 06-01-1997 747.65 -0.050813009 > 07-01-1997 753.23 0.743567202 > 08-01-1997 748.41 -0.64196699 > 09-01-1997 754.85 0.856809786 > 10-01-1997 759.5 0.614126802 > > However when I try to read this data using below code I get error, > > read.zoo("1.csv", sep = ',', format = '%d-%m-%Y') > > Error reads as, > > index has 4500 bad entries at data rows: 1 2 3 4 5 6 7 8 9..... > > Could you please help to understand why I am getting this error? > > > sessionInfo() > > R version 4.2.2 (2022-10-31) > > Platform: x86_64-apple-darwin17.0 (64-bit) > > Running under: macOS Big Sur ... 10.16 > > > Matrix products: default > > BLAS: > /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib > > LAPACK: > /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib > > > locale: > > [1] C/UTF-8/C/C/C/C > > > attached base packages: > > [1] stats graphics grDevices utils datasets methods base > > > other attached packages: > > [1] zoo_1.8-12 > > > loaded via a namespace (and not attached): > > [1] compiler_4.2.2 tools_4.2.2 grid_4.2.2 lattice_0.20-45 > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >-- John Kane Kingston ON Canada [[alternative HTML version deleted]]
Gabor Grothendieck
2023-Aug-03 18:46 UTC
[R] Could not read time series data using read.zoo()
The header has white space in it so skip over it, use header = FALSE and specify the column headers yourself. Also use fill=TRUE since the first row does not have 3 entries. # generate test file cat("Date Adj Close lret 02-01-1997 737.01 03-01-1997 748.03 1.48416235 06-01-1997 747.65 -0.050813009 07-01-1997 753.23 0.743567202 08-01-1997 748.41 -0.64196699 09-01-1997 754.85 0.856809786 10-01-1997 759.5 0.614126802", file = "1.csv") # test library(zoo) read.zoo("1.csv", skip = 1, header = FALSE, format = "%m-%d-%Y", fill = TRUE, col.names = c(NA, "Adj_Close", "Close")) On Thu, Aug 3, 2023 at 10:53?AM Christofer Bogaso <bogaso.christofer at gmail.com> wrote:> > Hi, > > I have a CSV which contains data like below (only first few rows), > > Date Adj Close lret > 02-01-1997 737.01 > 03-01-1997 748.03 1.48416235 > 06-01-1997 747.65 -0.050813009 > 07-01-1997 753.23 0.743567202 > 08-01-1997 748.41 -0.64196699 > 09-01-1997 754.85 0.856809786 > 10-01-1997 759.5 0.614126802 > > However when I try to read this data using below code I get error, > > read.zoo("1.csv", sep = ',', format = '%d-%m-%Y') > > Error reads as, > > index has 4500 bad entries at data rows: 1 2 3 4 5 6 7 8 9..... > > Could you please help to understand why I am getting this error? > > > sessionInfo() > > R version 4.2.2 (2022-10-31) > > Platform: x86_64-apple-darwin17.0 (64-bit) > > Running under: macOS Big Sur ... 10.16 > > > Matrix products: default > > BLAS: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib > > LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib > > > locale: > > [1] C/UTF-8/C/C/C/C > > > attached base packages: > > [1] stats graphics grDevices utils datasets methods base > > > other attached packages: > > [1] zoo_1.8-12 > > > loaded via a namespace (and not attached): > > [1] compiler_4.2.2 tools_4.2.2 grid_4.2.2 lattice_0.20-45 > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.-- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com