Search code examples
rdatetimesapplyiso8601

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


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 ?


Solution

  • 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