[R] Error in as.xts

Rui Barradas ruipbarradas at sapo.pt
Mon Jul 16 08:51:48 CEST 2012


Hello,

I can see several things that are not right or may go wrong. (Without an 
actual dataset, this is just a series of hints.)

1. The read.csv statement. Like read.table, it creates a data.frame, 
which defaults to reading strings as factors, coded internally as 
integers. Sometimes there are problems, when using them and you are 
converting the dates/times to POSIXct. You can try to use read.csv option

stringsAsFactors = FALSE

and then do the conversions. These conversions would include converting 
'species' and 'site' to factor, if you want them as factors.

2. Are there typos in your posted code example?
2.a) You read into data.frame 'd1' but then use d$date and d$TIME.
2.b) In 'd1' the column is named 'time', not 'TIME'.

3. Try to create the date/time and assign it to a variable, for instance,

dtvar <- as.POSIXct(...etc...)

Now you can check length(dtvar) and NROW(d1) to see if they are equal. 
You can also see if the conversion was allright. This is a general rule: 
if there's an error in a complicated instruction, break it into smaller 
ones.

Hope this helps,

Rui Barradas

Em 16-07-2012 03:10, Yolande Tra escreveu:
> Hi
> I got the following error using as.xts
> Error in xts(x, order.by = order.by, frequency = frequency, ...) :
>    NROW(x) must match length(order.by)
> Here is how the data looks like
>> d1 <- read.csv(file.path(dataDir,"AppendixA-FishCountsTable-2009.csv"),
> as.is=T)
>> d1[1:3,]
>    dive_id       date  time      species count size    site depth level
> TRANSECT VIS_M
> 1      62 10/12/2009 12:44 E. lateralis     2   15 Hopkins    15     B
>     1     4
> 2      62 10/12/2009 12:44 E. lateralis     1   22 Hopkins    15     B
>     1     4
> 3      62 10/12/2009 12:44 E. lateralis     1   25 Hopkins    15     B
>     1     4
>> diveData_2009 <- as.xts( d1,order.by=as.POSIXct(strptime(paste(d$date,
> d$TIME ), "%d/%m/%Y %H:%M") ))
> Error in xts(x, order.by = order.by, frequency = frequency, ...) :
>    NROW(x) must match length(order.by)
>
> I could not figure out how to correct it
> Thank you for your help
> Yolande
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list