Convert time don't work on a column's dataframe

Viewed 42

I have some data from event producer. In a "created_at column I have mixed type of datetime value.

Some NA, some ISO8601 like, some POSIX with and without millisec.

I build a func that should take care of everything meanning let's NA and ISO8601 info as it is, and convert POSIX date to ISO8601.

library(anytime)
convert_time <- function(x) {
  nb_char = nchar(x)
  if (is.na(x)) return(x)
  else if (nb_char == 10 | nb_char == 13) {
    num_x = as.numeric(x)
    if (nb_char == 13) {
      num_x = round(num_x / 1000, 0)
    }
    return(anytime(num_x))
  }
  return(x)
}

If I passe one problematic value convert_time("1613488656")

"2021-02-16 15:17:36 UTC"

Works well !

Now

df_offer2$created_at = df_offer2$created_at %>% sapply(convert_time)

I still have the problematic values.

Any tips here ?

2 Answers

Two things that have worked for me:

col1<-seq(from=1,to=10)
col2<-rep("1613488656",10)
df <- data.frame(cbind(col1,col2))
colnames(df)<-c("index","created_at")


df <- df%>%
  mutate(converted = convert_time(df$created_at))`

alternatively

col1<-seq(from=1,to=10)
col2<-rep("1613488656",10)
df <- data.frame(cbind(col1,col2))
colnames(df)<-c("index","created_at")


df$created_at <- convert_time(df$created_at)

Both spit out warnings but appear to make the correction properly

I would suggest the following small changes...

convert_time <- function(x) {
  nb_char = nchar(x)
  if (is.na(x)) return(x)
  else if (nb_char == 10 | nb_char == 13) {
    num_x = as.numeric(x)
    if (nb_char == 13) {
      num_x = round(num_x / 1000, 0)
    }
    return(num_x)                        #remove anytime from here
  }
  return(x)
}

df_offer2$created_at = df_offer2$created_at %>% 
    sapply(convert_time) %>% anytime()   #put it back in at this point
Related