Pergunta

I am having difficulty using xts. Either only a header is output or all values are output when I use the following (or with alterations to the date).

datax1["14/04/2014"]

I am reading in a data in the following format using read.csv

Date     Time   ws  wd
1 10/04/14 00:00:00 1.88 118
2 10/04/14 00:04:30 1.91 118
3 10/04/14 00:09:00 1.90 114

Then, I use the following to convert Date and Time columns to date and time.

datax1 <- xts(data1[-c(1,2)],
as.POSIXlt(paste(as.Date(data1$Date, format="%d-%m-%y"),
data1$Time), tz=""))

After this stage, when I run the commmand to output data from a specific date, only a header or all data from all dates is output. Any help appreciated on how to get this to work.

Edited to add - the full range of data extends from 10/04/14 - 29/04/14.

Foi útil?

Solução

It seems that the error is the format string passed to as.Date. It specifies - characters, but you're using / characters in the actual input.

The easiest way to do the desired operation, is to use paste on the columns themselves, then pass to as.POSIXlt with an appropriate format string:

with(data1, as.POSIXlt(paste(Date, Time), format="%d/%m/%y %H:%M:%S"))
## [1] "2014-04-10 00:00:00" "2014-04-10 00:04:30" "2014-04-10 00:09:00"
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top