Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert list/vector of strings with date format into posix date class with R

I have a list of strings that is in our date format and I want to convert it into a list of posix dates that I can manipulate with R, how can I do that?

This is what I have but I end up with a list of lists:

 a <- c("2009.01.01 00:00:00", "2009.01.01 00:00:00")

z <- lapply(a,function(x){strptime(x, "%Y.%m.%d %H:%M:%S")})

> z <- lapply(a,function(x){strptime(x, "%Y.%m.%d %H:%M:%S")})
> summary(z)
     Length Class   Mode
[1,] 1      POSIXlt list
[2,] 1      POSIXlt list
like image 747
Berlin Brown Avatar asked Jan 26 '12 17:01

Berlin Brown


1 Answers

strptime is vectorized:

a <- c("2009.01.01 12:20:10", "2009.01.01 04:12:14")
> out <- strptime(a, "%Y.%m.%d %H:%M:%S")
> str(out)
 POSIXlt[1:2], format: "2009-01-01 12:20:10" "2009-01-01 04:12:14"
like image 146
joran Avatar answered Oct 21 '22 11:10

joran