Error in mutate

Hi, I was trying to clean my data. Something went wrong but I don’t know what. Could you please help me?

pacman::p_load(
  rio, 
  janitor, 
  here, 
  skimr, 
  scales,
  gtsummary,
  EpiStats,
  broom,
  reprex,
  datapasta,
  tidyverse)


# Import data -------------------------------------------------------------

linelist_raw <- data.frame(
  stringsAsFactors = FALSE,
  dayonset = c("12nov2006",
               NA,NA,NA,"12nov2006"),
  age = c(18L, 18L, 17L, 17L, 18L),
  sex = c("male",
          "female","female","male","female")
)



# Data cleaning -----------------------------------------------------------

linelist_clean <- linelist_raw %>%   # begin with the raw data
  clean_names() %>%                  # standardise column names 
  mutate(dayonset == dmy(dayonset))  # convert to date class
#> Error in `mutate()`:
#> ℹ In argument: `dayonset == dmy(dayonset)`.
#> Caused by error in `charToDate()`:
#> ! Zeichenkette ist nicht in einem eindeutigen Standardformat
#> Backtrace:
#>      ▆
#>   1. ├─linelist_raw %>% clean_names() %>% ...
#>   2. ├─dplyr::mutate(., dayonset == dmy(dayonset))
#>   3. ├─dplyr:::mutate.data.frame(., dayonset == dmy(dayonset))
#>   4. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
#>   5. │   ├─base::withCallingHandlers(...)
#>   6. │   └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
#>   7. │     └─mask$eval_all_mutate(quo)
#>   8. │       └─dplyr (local) eval()
#>   9. ├─lubridate (local) `==.Date`(dayonset, dmy(dayonset))
#>  10. │ ├─base::as.Date(e1)
#>  11. │ └─base::as.Date.character(e1)
#>  12. │   └─base (local) charToDate(x)
#>  13. │     └─base::stop("character string is not in a standard unambiguous format")
#>  14. └─base::.handleSimpleError(...)
#>  15.   └─dplyr (local) h(simpleError(msg, call))
#>  16.     └─rlang::abort(message, class = error_class, parent = parent, call = error_call)


ggplot(
  data = linelist_raw,
  mapping = aes(x = dayonset))+
  geom_histogram()
#> Error in `geom_histogram()`:
#> ! Problem while computing stat.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `setup_params()`:
#> ! `stat_bin()` requires a continuous x aesthetic
#> ✖ the x aesthetic is discrete.
#> ℹ Perhaps you want `stat="count"`?
#> Backtrace:
#>      ▆
#>   1. ├─base::tryCatch(...)
#>   2. │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>   3. │   ├─base (local) tryCatchOne(...)
#>   4. │   │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   5. │   └─base (local) tryCatchList(expr, names[-nh], parentenv, handlers[-nh])
#>   6. │     └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>   7. │       └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   8. ├─base::withCallingHandlers(...)
#>   9. ├─base::saveRDS(...)
#>  10. ├─base::do.call(...)
#>  11. ├─base (local) `<fn>`(...)
#>  12. └─global `<fn>`(input = base::quote("ash-crane_reprex.R"))
#>  13.   └─rmarkdown::render(input, quiet = TRUE, envir = globalenv(), encoding = "UTF-8")
#>  14.     └─knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
#>  15.       └─knitr:::process_file(text, output)
#>  16.         ├─base::withCallingHandlers(...)
#>  17.         ├─base::withCallingHandlers(...)
#>  18.         ├─knitr:::process_group(group)
#>  19.         └─knitr:::process_group.block(group)
#>  20.           └─knitr:::call_block(x)
#>  21.             └─knitr:::block_exec(params)
#>  22.               └─knitr:::eng_r(options)
#>  23.                 ├─knitr:::in_input_dir(...)
#>  24.                 │ └─knitr:::in_dir(input_dir(), expr)
#>  25.                 └─knitr (local) evaluate(...)
#>  26.                   └─evaluate::evaluate(...)
#>  27.                     └─evaluate:::evaluate_call(...)
#>  28.                       ├─evaluate (local) handle(...)
#>  29.                       │ └─base::try(f, silent = TRUE)
#>  30.                       │   └─base::tryCatch(...)
#>  31.                       │     └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  32.                       │       └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  33.                       │         └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  34.                       ├─base::withCallingHandlers(...)
#>  35.                       ├─base::withVisible(value_fun(ev$value, ev$visible))
#>  36.                       └─knitr (local) value_fun(ev$value, ev$visible)
#>  37.                         └─knitr (local) fun(x, options = options)
#>  38.                           ├─base::withVisible(knit_print(x, ...))
#>  39.                           ├─knitr::knit_print(x, ...)
#>  40.                           └─knitr:::knit_print.default(x, ...)
#>  41.                             └─evaluate (local) normal_print(x)
#>  42.                               ├─base::print(x)
#>  43.                               └─ggplot2:::print.ggplot(x)
#>  44.                                 ├─ggplot2::ggplot_build(x)
#>  45.                                 └─ggplot2:::ggplot_build.ggplot(x)
#>  46.                                   └─ggplot2:::by_layer(...)
#>  47.                                     ├─rlang::try_fetch(...)
#>  48.                                     │ ├─base::tryCatch(...)
#>  49.                                     │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  50.                                     │ │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  51.                                     │ │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  52.                                     │ └─base::withCallingHandlers(...)
#>  53.                                     └─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
#>  54.                                       └─l$compute_statistic(d, layout)
#>  55.                                         └─ggplot2 (local) compute_statistic(..., self = self)
#>  56.                                           └─self$stat$setup_params(data, self$stat_params)
#>  57.                                             └─ggplot2 (local) setup_params(..., self = self)
#>  58.                                               └─cli::cli_abort(...)
#>  59.                                                 └─rlang::abort(...)

Created on 2023-10-12 with reprex v2.0.2

1 Like

Hello @sarman, in R the double equal sign “==” is used to check conditions. For example when we want to filter only obsevrations in city A we write filter(city == "A").

Inside mutate, when our goal is to create a new variable, we need to use only one equal sign “=” , which has a similar effect as the assignment operator

So, in your code, just change the “==” to “=”. Like this:

linelist_clean <- linelist_raw %>%  
  clean_names() %>%                  
  mutate(dayonset = dmy(dayonset))
3 Likes

Hi @lnielsen , thank you so much for your help, much appreciated!
Navina

1 Like