you have to use "POSIXct" classes to include date-time objects into
data
frames. strptime() returns an object of class of "POSIXlt". when you
do the
cbind(), it automatically converts test2 into "POSIXct"
you probably want
bsamp$spltime<-as.POSIXct(strptime(test,format="%d-%B-%y %H:%M"))
(but please be aware of time-zone issues when using "POSIXct"
classes). these
documents may help:
http://cran.r-project.org/doc/Rnews/Rnews_2004-1.pdf
=== Also ==Changes in behavior of POSIXt classes since the aforementioned R News
publication:
http://tolstoy.newcastle.edu.au/R/e2/help/07/04/13626.html
http://tolstoy.newcastle.edu.au/R/e2/help/07/04/13632.html
--- Jean-Louis Abitbol <abitbol at sent.com> wrote:
> Dear All,
>
> I am trying to convert to POSIXct after pasting a date and a time in
> character format with strptime.
>
> It is probably obvious but I don't understand why I get an error
message
> after
>
> bsamp$spltime<-strptime(test,format="%d-%B-%y %H:%M")
>
> whereas I can get what I want if I do it in 2 steps and rbinding ?
>
> Thanks and best regards, Jean-Louis
>
> This is the R console output
>
> >
bsamp<-read.table("bsampl2.csv",header=T,sep=";")
> > names(bsamp)<-tolower(names(bsamp))
> > bsamp<-upData(bsamp,drop=c("study"))
> Input object size: 23896 bytes; 15 variables
> Dropped variable study
> New object size: 23016 bytes; 14 variables
> > bsamp$visitdat<-as.character(bsamp$visitdat)
> > bsamp$samtime<-as.character(bsamp$samtime)
> > bsamp$admtime<-as.character(bsamp$admtime)
> > bsamp$delay<-as.character(bsamp$delay)
> > test<-paste(bsamp$visitdat,bsamp$samtime)
> > test
> [1] "01-mars-06 11:40" "15-mars-06 11:30"
"15-mars-06 15:00"
> [4] "29-mars-06 11:40" "01-mars-06 11:45"
"15-mars-06 11:15"
> [7] "15-mars-06 14:45" "29-mars-06 12:50"
"01-mars-06 11:16"
> [10] "15-mars-06 11:10" "15-mars-06 14:30"
"29-mars-06 11:50"
> [13] "01-mars-06 11:50" "15-mars-06 11:25"
"15-mars-06 14:55"
> [16] "29-mars-06 11:30" "01-mars-06 11:55"
"15-mars-06 11:35"
> [19] "15-mars-06 " "29-mars-06 11:45"
"01-mars-06 11:09"
> .....
>
>
> > bsamp$spltime<-strptime(test,format="%d-%B-%y %H:%M")
> Erreur dans `$<-.data.frame`(`*tmp*`, "spltime", value =
list(sec > numeric(0), :
> le tableau de remplacement a 9 lignes, le tableau remplac? en a
> 140
>
>
> > test2<-strptime(test,format="%d-%B-%y %H:%M")
> > bsamp<-cbind(bsamp,test2)
> > bsamp$test2
> [1] "2006-03-01 11:40:00 Centre de l'Europe"
> [2] "2006-03-15 11:30:00 Centre de l'Europe"
> [3] "2006-03-15 15:00:00 Centre de l'Europe"
> [4] "2006-03-29 11:40:00 Centre de l'Europe (heure
d'?t?"
> [5] "2006-03-01 11:45:00 Centre de l'Europe"
> [6] "2006-03-15 11:15:00 Centre de l'Europe"
> [7] "2006-03-15 14:45:00 Centre de l'Europe"
> [8] "2006-03-29 12:50:00 Centre de l'Europe (heure
d'?t?"
> [9] "2006-03-01 11:16:00 Centre de l'Europe"
> [10] "2006-03-15 11:10:00 Centre de l'Europe"
> [11] "2006-03-15 14:30:00 Centre de l'Europe"
> [12] "2006-03-29 11:50:00 Centre de l'Europe (heure
d'?t?"
> [13] "2006-03-01 11:50:00 Centre de l'Europe"
> [14] "2006-03-15 11:25:00 Centre de l'Europe"
> [15] "2006-03-15 14:55:00 Centre de l'Europe"
> [16] "2006-03-29 11:30:00 Centre de l'Europe (heure
d'?t?"
> [17] "2006-03-01 11:55:00 Centre de l'Europe"
> [18] "2006-03-15 11:35:00 Centre de l'Europe"
> [19] NA
> ..........
> > sessionInfo()
> R version 2.4.1 (2006-12-18)
> i386-pc-mingw32
>
> locale:
>
LC_COLLATE=French_France.1252;LC_CTYPE=French_France.1252;LC_MONETARY=French_France.1252;LC_NUMERIC=C;LC_TIME=French_France.1252>
> attached base packages:
> [1] "stats" "graphics" "grDevices"
"utils" "datasets"
> [6] "methods" "base"
>
> other attached packages:
> car RColorBrewer gplots gdata gtools
> "1.2-1" "0.2-3" "2.3.2"
"2.3.1" "2.3.1"
> lattice Hmisc acepack RWinEdt
> "0.14-17" "3.3-1" "1.3-2.2"
"1.7-5"
> >
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>