1
votes

When trying to read a local csv file im getting the error

Error in xts(dat, order.by = as.Date(rownames(dat), "%m/%d/%Y")) : 'order.by' cannot contain 'NA', 'NaN', or 'Inf'

im trying out the example from https://rpubs.com/mohammadshadan/288218 which is the following:

tmp_file <- "test.csv"

# Create dat by reading tmp_file
dat <- read.csv(tmp_file,header=FALSE)

# Convert dat into xts
xts(dat, order.by = as.Date(rownames(dat), "%m/%d/%Y"))

# Read tmp_file using read.zoo
dat_zoo <- read.zoo(tmp_file, index.column = 0, sep = ",", format = "%m/%d/%Y")

# Convert dat_zoo to xts
dat_xts <- as.xts(dat_zoo)

the thing is when i try to read the file like in the example which is reading the file from the server this works somehow but not when i try with a csv file locally even if its the same info as the file in the web.

i have tried creating the csv file with Notepad,Notepad++ and Excel with no luck. Any idea what im missing?, i have also tried using read.table instead of csv with the same results...

File can be found at: https://ufile.io/zfqje

if header=TRUE i get the following error:

Warning messages: 1: In read.table(file = file, header = header, sep = sep, quote = quote, : incomplete final line found by readTableHeader on 'test.csv' 2: In read(file, ...) : incomplete final line found by readTableHeader on 'test.csv'

1
Can you provide a reproducible example so we can troubleshoot?jesstme
uploaded it to: ufile.io/zfqjeMakenshi
Can you use dput() on it instead and share a few lines of it here, in your question?jesstme
No error for me. The result comes as: dat_xts a b 2015-01-02 1 3 2015-02-03 2 MKR

1 Answers

1
votes

The problem is the header=FALSE argument in read.csv.

read.csv will choose the first column as the row names if there is a header and the first row contains one fewer field than the number of columns. When header = FALSE, it doesn't create the row names.

Here is an example of the problem:

dat <- read.csv(text = "a,b
1/02/2015,1,3
2/03/2015,2,4", header = F)

as.Date(rownames(dat), "%m/%d/%Y")
#> [1] NA NA NA

By removing header = F, the problem is fixed:

dat <- read.csv(text = "a,b
1/02/2015,1,3
2/03/2015,2,4")

as.Date(rownames(dat), "%m/%d/%Y")
#> [1] "2015-01-02" "2015-02-03"