r - Why does sapply fail to use lubridate's parse_date_time while lapply does not? -


given: simple 4x2 data frame filled data of type character

goal: same data frame values replaced result of applying following lubridate function call them: parse_date_time(df, orders = c ("ymd_hms", "mdy_hms"), tz = "etz")

it seems using lapply works fine. when using sapply, parse_date_time function returns strange large integers.

here data:

df <- as.data.frame(stringsasfactors = false, matrix(c("2014-01-13 12:08:02", "2014-01-13 12:19:46", "2014-01-14 09:59:09", "2014-01-14 10:05:09", "6-18-2016 17:43:42", "6-18-2016 18:06:59", "6-27-2016 12:16:47", "6-27-2016 12:29:05"), nrow = 4, ncol = 2, byrow = true))

colnames(df) <- c("starttime", "stoptime")

here sapply call:

df2 <- sapply(df, fun = function(column) { parse_date_time(column, orders = c("ymd_hms", "mdy_hms"), tz = "etz") })

and lapply call:

df2 <- lapply(df, fun = function(column) { parse_date_time(column, orders = c("ymd_hms", "mdy_hms"), tz = "etz") })

i understand sapply returns simplest data structure can , lapply returns list. had sapply worked, have been followed df2 <- data.frame(df2)so i'd have desired data frame stated in 'goal' (note did successful lapply returned list).

my question why parse_date_time function behave expected in lapply not in sapply? reference, here example outputs of lapply , sapply call respectively:

2016-06-27 12:29:05

1467030545

the reason sapply have default simplify = true , when length or dimension of list elements same, simplifies vector or matrix. internally, date time classes stored numeric,

typeof(parse_date_time(df$starttime, orders = c("ymd_hms", "mdy_hms"), tz = "etz")) #[1] "double" 

while class 'posixct`

class(parse_date_time(df$starttime, orders = c("ymd_hms", "mdy_hms"), tz = "etz")) #[1] "posixct" "posixt"   

so coerces while doing matrix conversion, while in list preserves class format.

if interested in data.frame, create copy of 'df' , use [] same structure 'df'

df2 <- df df2[] <-  lapply(df, fun = function(column) {      parse_date_time(column, orders = c("ymd_hms", "mdy_hms"), tz = "etz")    })  df2 #           starttime            stoptime #1 2014-01-13 12:08:02 2014-01-13 12:19:46 #2 2014-01-14 09:59:09 2014-01-14 10:05:09 #3 2016-06-18 17:43:42 2016-06-18 18:06:59 #4 2016-06-27 12:16:47 2016-06-27 12:29:05 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -