r - create function to format a column in a dataframe -


i have date time i'm trying strip , clean. i'm not sure if there out of box this. i've done searching couldn't find anything. date format is..

2017-01-01t01:36:17.000z 

reproducible example:

startdate <- c("2017-01-01t01:36:17.000z", "2017-01-01t01:36:17.000z") num <- c(1,2) dataframe <- data_frame(num, startdate) 

i've fixed doing following

require(dplyr) require(tidyr) require(lubridate)  dataframe <- dataframe %>%    separate(startdate , = c("newdate", "tail"), sep = "t") %>%    mutate(newdate= ymd(newdate)) %>%    select(-tail) 

what turn function can pipe when need to.

i came following, haven't been able work. i've tried variations of separate_, , mutate_. still no luck.

ztime <- function(df, datecol, newcol) {   library(lubridate)   library(tidyr)   library(dplyr)     df <- df %>%     separate_(datecol, = c(newcol, "extra"), sep = "t") #%>%     mutate_(newcol = ymd(newcol)) %>%     select(-extra)  } 

hoping insight! thank in advance

why don't call as_date lubridate ?

dater <- function(x){   lubridate::as_date(x) } dater("2017-01-01t01:36:17.000z") [1] "2017-01-01" 

best

colin


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -