[R] transform(_data,...) using strptime gives an error
Gabor Grothendieck
ggrothendieck at gmail.com
Sun Jul 19 12:42:57 CEST 2009
strptime produces a POSIXlt result and you likely intended
POSIXct instead:
as.POSIXct(x, format = "%Y/%m/%d %H:%M")
for storing in a data frame. Also to avoid time zone
problems down the line you might want to use chron:
library(chron)
as.chron(x, format = "%Y/%m/%d %H:%M")
See R News 4/1.
On Sun, Jul 19, 2009 at 6:26 AM, <amvds at xs4all.nl> wrote:
> I have timstamped data like this:
>
>> sd[1:10,]
> Tstamp Density Mesh50 Mesh70 Mesh100 Mesh150 Mesh200
> 2 2009/02/27 07:00 30.5 0.7 10.7 21.4 32.8 41.6
> 3 2009/02/27 08:00 32.2 1.6 12.4 23.3 34.5 43.0
> 4 2009/02/27 09:00 32.7 4.8 13.0 24.0 35.1 43.5
> 5 2009/02/27 10:00 26.7 0.3 6.5 17.6 28.1 36.9
> 6 2009/02/27 11:00 26.6 0.9 6.6 17.0 28.6 37.9
> 7 2009/02/27 12:00 23.3 6.3 3.4 14.0 25.5 34.6
> 8 2009/02/27 13:00 25.2 1.1 5.1 15.4 27.3 36.8
> 9 2009/02/27 14:00 28.6 0.2 8.7 19.4 30.9 40.0
> 10 2009/02/27 15:00 28.0 0.6 8.0 18.6 30.2 39.3
> 11 2009/02/27 16:00 28.3 0.9 8.3 18.9 30.5 39.5
>
> The timstamps are character vectors:
>
>> str(sd)
> 'data.frame': 591 obs. of 7 variables:
> $ Tstamp : chr "2009/02/27 07:00" "2009/02/27 08:00" "2009/02/27 09:00"
> "2009/02/27 10:00" ...
> $ Density: num 30.5 32.2 32.7 26.7 26.6 23.3 25.2 28.6 28 28.3 ...
> $ Mesh50 : num 0.7 1.6 4.8 0.3 0.9 6.3 1.1 0.2 0.6 0.9 ...
> $ Mesh70 : num 10.7 12.4 13 6.5 6.6 3.4 5.1 8.7 8 8.3 ...
> $ Mesh100: num 21.4 23.3 24 17.6 17 14 15.4 19.4 18.6 18.9 ...
> $ Mesh150: num 32.8 34.5 35.1 28.1 28.6 25.5 27.3 30.9 30.2 30.5 ...
> $ Mesh200: num 41.6 43 43.5 36.9 37.9 34.6 36.8 40 39.3 39.5 ...
> - attr(*, "na.action")=Class 'exclude' Named int [1:58] 1 88 89 90 250
> 318 319 320 321 322 ...
> .. ..- attr(*, "names")= chr [1:58] "1" "88" "89" "90" ...
>
> Trying to transform the timestamped character vector 'in place' using
> transform gives this error message:
>
>> sd<-transform(sd,Tstamp=strptime(Tstamp,format='%Y/%m/%d %H:%M'))
> Error in `[<-.data.frame`(`*tmp*`, inx[matched], value = list(Tstamp =
> list( :
> replacement element 1 has 9 rows, need 591
>
> Why? It beats me...
>
> I do have a backup of course:
>
> td<-strptime(sd$Tstamp,format='%Y/%m/%d %H:%M')
> sd<-data.frame(Tstamp=td, sd[2:7])
>
> this works fine but is one step more complicated. Something I miss about
> transform()?
>
> Thanks in advance,
> Alex van der Spek
>
> ______________________________________________
> 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