r - Using functions with dplyr to parse dates -
i have code in r manipulating timestamps dyplr code below in essence, send out email @ time , response - if any. wanted extract number of attributes both sent , response items.
there 3 countries involved , have time stamps set differently example uk has timestamp like: 2015-03-11 09:32:51, other 2 countries have time stamps 02/03/2015 08:02
i have created code below based on studying of dplyr , wish create function out of im getting errors
udf_parsedates <- function(strformat) { email <- email %>% mutate(email.dtm = parse_date_time(sms.date,strformat), email.hour = as.numeric(format(sms.dtm,"%h")), email.dow = wday(sms.dtm, label=true, abbr=false), rep.dtm = parse_date_time(reply.date,strformat), rep.hour = as.numeric(format(rep.dtm,"%h")), rep.dow = wday(rep.dtm, label=true, abbr=false), responsetime.hours = as.numeric(difftime(rep.dtm, sms.dtm, units="hours"))) } # find hour , dow of email if(email$dest_ctry == 'gb') {udf_parsedates("%y-%m-%d %h:%m:%s") } else if (sms$dest_ctry == 'it') {udf_parsedates("%y/%m/%d %h:%m") } else if (sms$dest_ctry == 'es') {udf_parsedates("%y/%m/%d %h:%m")}
the mutate works fine outside of function im trying learn integrate , re-use code use functions this. appreciated
if me, i'd try create column shows date format, , use , ifelse
apply correct transformation posixct
object
this line in mutate
call generate date format
date_format = ifelse(grepl("\\d{4}-\\d{2}-\\d{2} ", sms.date), "ymd", "mdy")
Comments
Post a Comment