Matt W.
Matt W.

Reputation: 3722

create function to format a column in a dataframe

I have a date time that I'm trying to strip and clean. I'm not sure if there is something out of the box for this. I've done some searching but couldn't find anything. The 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 it by doing the following

require(dplyr)
require(tidyr)
require(lubridate)

DataFrame <- DataFrame %>%
   separate(StartDate , into = c("NewDate", "tail"), sep = "T") %>%
   mutate(NewDate= ymd(NewDate)) %>%
   select(-tail)

What I would like to do is to turn it into a function so that I can just pipe it when I need to.

I came up with the following, but haven't been able to get it to work. I've also tried variations of separate_, and mutate_. But still no luck.

ztime <- function(df, datecol, newcol) {
  library(lubridate)
  library(tidyr)
  library(dplyr)


  df <- df %>%
    separate_(datecol, into = c(newcol, "extra"), sep = "T") #%>%
    mutate_(newcol = ymd(newcol)) %>%
    select(-extra)

}

Hoping for some insight! Thank you in advance

Upvotes: 2

Views: 94

Answers (3)

Dave Gruenewald
Dave Gruenewald

Reputation: 5689

There's a game changing package for that! anytime

library(anytime)

NewDataFrame <- DataFrame %>% 
  mutate(NewDate = anytime(StartDate))

I'm always amazed at how much it cleans up and interprets

Upvotes: 1

Colin FAY
Colin FAY

Reputation: 5109

Why don't you call as_date from lubridate ?

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

Best

Colin

Upvotes: 3

F. Priv&#233;
F. Priv&#233;

Reputation: 11728

You can simply use

ztime <- function(df, datecol, newcol) {
  df[[newcol]] <- lubridate::ymd_hms(df[[datecol]])
}

Upvotes: 0

Related Questions