Error code using mutate function, unable to find object

Hi, R community, I am attempting to clean my dataset but keep getting the below error code; any help will be much appreciated! Thanks in advance

# reprex1 script  

# install and load packages
pacman::p_load(rio, here, janitor, tidyverse, reprex, datapasta)

# import data
surv_raw <- data.frame(
  stringsAsFactors = FALSE,
  adm3_name_res = c(NA,"Mountain Rural",
                    "Mountain Rural","East II","West III"),
  sex = c("m", "f", "f", "f", "f")
)

# try to convert column to class "Date"
surv_clean <- surv_raw %>% 
  clean_names() %>% 
  mutate(onset_date = ymd(onset_date))
#> Error in `mutate()`:
#> ℹ In argument: `onset_date = ymd(onset_date)`.
#> Caused by error:
#> ! object 'onset_date' not found
1 Like

Hi @d.honeyman, The column onset_date does not exist in surv_raw dataframe. That’s why the last line of the error message says the column is not found.

I have added a onset_date column in your example. Now, your code works. Hope this helps.

library(tidyverse)
library(janitor)

surv_raw <- data.frame(
  stringsAsFactors = FALSE,
  adm3_name_res = c(NA,"Mountain Rural",
                    "Mountain Rural","East II","West III"),
  sex = c("m", "f", "f", "f", "f"),
  onset_date = c("2020-01-01", "2020-01-10", "2020-01-15", "2020-01-18", "2020-01-11")
)

surv_clean <- surv_raw %>% 
  clean_names() %>% 
  mutate(onset_date = ymd(onset_date))

surv_clean

# 
#    adm3_name_res sex onset_date
# 1           <NA>   m 2020-01-01
# 2 Mountain Rural   f 2020-01-10
# 3 Mountain Rural   f 2020-01-15
# 4        East II   f 2020-01-18
# 5       West III   f 2020-01-11
1 Like