user13267770
user13267770

Reputation:

error in function for converting integer to date format in r, error: "do not know how to convert '.' to class “Date”"

I am new to R and couldnt find a solution googling, so here we go:

I have a data frame with many columns. Two of them have dates in it. One column as character und the other one as integer64. The values in both columns look like this "201802171254". so they are in the format Y%m%d%H%M. I want to converting them into date formats using a function. Before that i tried the following two lines:

df$column_character <- as.Date(as.character(c(df$column_character)), format = '%Y%m%d%H%M')

df$column_integer64 <- as.Date(as.character(c(df$column_integer64)), format = '%Y%m%d%H%M')

I know, that this is not optimal, but they work. I get the date formats for both columns. so i have tried to put the code into a function:

date_format <- function(df, col){
  col_q = enquo(col)

df %>%

base::as.Date(as.character(c(!!col_q), na.rm = TRUE), format = '%Y%m%d%H%M')

}

date_format(df$column_character)

date_format(df$column_integer64)

This works, but only for the first one, the character column. For integer64 column I get the error message:

Error in as.Date.default(., as.character(c(!!col_q), na.rm = TRUE), format = "%Y%m%d%H%M") : 
  do not know how to convert '.' to class “Date” 

that is confusing me, because if at all i was expecting character column to struggle, since in the code i have one unnecessary step (converting it to character before converting it to date, but it already is character) in it.

where is my mistake? why does it work without the function, but i try to use it as a template, half of it is not working anymore?

thanks for your answers in advance

Upvotes: 0

Views: 146

Answers (1)

stefan
stefan

Reputation: 124083

Can't tell you exactly, why the function worked in the first place. However, your function is in my opinion not well specfied or messed up. If you want to use tidy eval then have a look at my function date_format_new. However, in my opinion there is a simpler way to set up the function, which I coded as date_format_simple.

library(dplyr)

df <- data.frame(
  column_character = "201802171254",
  column_integer64 = 201802171254,
  stringsAsFactors = FALSE
)

date_format <- function(df, col){
  col_q = enquo(col)

  df %>%
    base::as.Date(as.character(c(!!col_q), na.rm = TRUE), format = '%Y%m%d%H%M')
}

date_format(df$column_character)
#> [1] "2018-02-17"
date_format(df$column_integer64)
#> Error: Quosures can only be unquoted within a quasiquotation context.
#> 
#>   # Bad:
#>   list(!!myquosure)
#> 
#>   # Good:
#>   dplyr::mutate(data, !!myquosure)

# Works
date_format_new <- function(df, col){
  col_q = enquo(col)

  df %>%
    pull(!!col_q) %>% 
    as.character() %>% 
    base::as.Date(format = '%Y%m%d%H%M')

}

date_format_new(df, column_character)
#> [1] "2018-02-17"
date_format_new(df, column_integer64)
#> [1] "2018-02-17"

# Simpler
date_format_simple <- function(col){
  base::as.Date(as.character(col), format = '%Y%m%d%H%M')
}

date_format_simple(df$column_character)
#> [1] "2018-02-17"
date_format_simple(df$column_integer64)
#> [1] "2018-02-17"

Created on 2020-04-09 by the reprex package (v0.3.0)

Upvotes: 0

Related Questions