[R] conversion of string

Milan Bouchet-Valat nalimilan at club.fr
Mon Jan 23 11:22:53 CET 2012


Le lundi 23 janvier 2012 à 00:35 -0800, uday a écrit :
> I new in R programming language. 
> 
> I have some time data  
> 
> time
>  [1] "2005-01-03 05:11:39 UTC" "2005-01-03 06:36:02 UTC" "2005-01-03
> 06:36:55 UTC"
>  [4] "2005-01-03 06:37:00 UTC" "2005-01-03 06:38:04 UTC" "2005-01-03
> 06:38:04 UTC"
>  [7] "2005-01-03 06:38:04 UTC" "2005-01-03 06:38:04 UTC" "2005-01-03
> 06:38:05 UTC"
> [10] "2005-01-03 06:38:05 UTC" "2005-01-03 06:38:05 UTC" "2005-01-03
> 06:38:05 UTC"
> 
> from this data set I would like to separate Year Month Day Hour Minute
> Second 
> 
> could you please tell which command can separate this data to the format
> which I needed?
You should convert these to Date objects (if not done already), and then
extract the relevant part using format.Date(). For example:
strings <- c("2005-01-03 05:11:39 UTC", "2005-01-03 06:36:02 UTC",
"2005-01-03 06:36:55 UTC")
dates <- as.Date(strings)
format.Date(dates, "%Y")

to get the year.


Cheers



More information about the R-help mailing list