lubridate - How to extract Month from date in R -
i using lubridate
package , applying month
function extract month date. ran str command on date field , got
factor w/ 9498 levels "01/01/1979","01/01/1980",..: 5305 1 1 1 1 1 1 1 1 1 ... > v1$date<-month(v1$date) error in as.posixlt.character(as.character(x), ...) : character string not in standard unambiguous format
here example of data frame
https://drive.google.com/file/d/0b6cqwmwsek20q2dhblhxzi14wk0/edit?usp=sharing
i don't know doing wrong.
?month
states:
date-time must posixct, posixlt, date, period, chron, yearmon, yearqtr, zoo, zooreg, timedate, xts, its, ti, jul, timeseries, , fts objects.
your object factor, not character vector (presumably because of stringsasfactors = true
). have convert vector datetime class, instance posixlt
:
library(lubridate) some_date <- c("01/02/1979", "03/04/1980") month(as.posixlt(some_date, format="%d/%m/%y")) [1] 2 4
there's convenience function dmy
, can same (tip proposed @henrik):
month(dmy(some_date)) [1] 2 4
going further, @ishouldbuyaboat gives hint dd/mm/yyyy character formats accepted without explicit casting:
month(some_date) [1] 2 4
for list of formats, see ?strptime
. you'll find "standard unambiguous format" stands for
the default formats follow rules of iso 8601 international standard expresses day "2001-02-28" , time "14:01:02" using leading zeroes here.
Comments
Post a Comment