Create New Variable from Existing Variable (error code)

I am attempting to create a new variable from an existing variable, but I keep getting the following error message: Error in UseMethod(“mutate”) :
no applicable method for ‘mutate’ applied to an object of class “function”

Here is the example of my R code, where am I going wrong?

# Load Packages -----------------------------------------------------------

# The pacman package will install each package if necessary,
# and load it for use in the current session

pacman::p_load(
rio,          # for importing data
here,         # for locating files
skimr,        # for reviewing the data
janitor,      # for data cleaning  
lubridate,    # for date cleaning  
epikit,       # creating age categories
gtsummary,    # for nice tables
ggExtra,      # graphics  
dplyr,        # to create a new variable from an existing one
apyramid,     # age sex pyramids
viridis,      # color blind friendly color scales
tsibble,      # epiweeks and other time series analysis
tidyverse     # for data management and visualization
)

names(NHSN_HavBed_Data)

skimr::skim(NHSN_HavBed_Data)

NHSN_HavBed_Data <- NHSN_HavBed_Data %>% 
janitor::clean_names()

# see the new column names
names(NHSN_HavBed_Data)

# tabulate masks
NHSN_HavBed_Data %>% 
tabyl(on_hand_supply_of_surgical_masks_in_days)

# tabulate gowns
NHSN_HavBed_Data %>% 
tabyl(on_hand_supply_of_single_use_surgical_gowns_in_days)

# tabulate n95 respirators
NHSN_HavBed_Data %>% 
tabyl(on_hand_supply_of_n95_respirators_in_days)

# tabulate eye protection
NHSN_HavBed_Data %>% 
tabyl(on_hand_supply_of_eye_protection_in_days)

# tabulate gloves
NHSN_HavBed_Data %>% 
tabyl(on_hand_supply_of_gloves_in_days)


NHSN_HavBed_Data <- NHSN_HavBed_Data %>% 
mutate(on_hand_supply_of_surgical_masks_in_days = recode(on_hand_supply_of_surgical_masks_in_days,
                         "0 DAYS" = "zero days",
                         "0 Days" = "zero days",
                         "0 days" = "zero days",
                         "1-3 DAYS" = "1-3 days",
                         "1-3 Days" = "1-3 days",
                         "1-3 days" = "1-3 days",
                         "15-30 DAYS" = "15-30 days",
                         "15-30 Days" = "15-30 days",
                         "15-30 days" = "15-30 days",
                         "4-6 DAYS" = "4-6 days",
                         "4-6 Days" = "4-6 days",
                         "4-6 days" = "4-6 days",
                         "7-14 DAYS" = "7-14 days",
                         "7-14 Days" = "7-14 days",
                         "7-14 days" = "7-14 days",
                         "> 30 DAYS" = ">30 days",
                         ">30 Days" = ">30 days",
                         " >30 days" = ">30 days",
                         "No" = "NA",
                         "Yes" = "NA",
                         "NA" = "NA",
                         "<NA>" ="NA",))
                       

NHSN_HavBed_Data %>% 
tabyl(on_hand_supply_of_surgical_masks_in_days)



NHSN_HavBed_Data <- NHSN_HavBed_Data %>% 
mutate(on_hand_supply_of_single_use_surgical_gowns_in_days = recode(on_hand_supply_of_single_use_surgical_gowns_in_days,
                                                         "0 DAYS" = "zero days",
                                                         "0 Days" = "zero days",
                                                         "0 days" = "zero days",
                                                         "1-3 DAYS" = "1-3 days",
                                                         "1-3 Days" = "1-3 days",
                                                         "1-3 days" = "1-3 days",
                                                         "15-30 DAYS" = "15-30 days",
                                                         "15-30 Days" = "15-30 days",
                                                         "15-30 days" = "15-30 days",
                                                         "4-6 DAYS" = "4-6 days",
                                                         "4-6 Days" = "4-6 days",
                                                         "4-6 days" = "4-6 days",
                                                         "7-14 DAYS" = "7-14 days",
                                                         "7-14 Days" = "7-14 days",
                                                         "7-14 days" = "7-14 days",
                                                         "> 30 DAYS" = ">30 days",
                                                         ">30 Days" = ">30 days",
                                                         " >30 days" = ">30 days",
                                                         "> 30 days" = ">30 days",
                                                         "Thank" = "NA",
                                                         "No" = "NA",
                                                         "Yes" = "NA",
                                                         "<NA>" ="NA",))


NHSN_HavBed_Data %>% 
tabyl(on_hand_supply_of_single_use_surgical_gowns_in_days)


NHSN_HavBed_Data <- NHSN_HavBed_Data %>% 
mutate(on_hand_supply_of_n95_respirators_in_days = recode(on_hand_supply_of_n95_respirators_in_days,
                                                                    "0 DAYS" = "zero days",
                                                                    "0 Days" = "zero days",
                                                                    "0 days" = "zero days",
                                                                    "1-3 DAYS" = "1-3 days",
                                                                    "1-3 Days" = "1-3 days",
                                                                    "1-3 days" = "1-3 days",
                                                                    "15 - 30 days" = "15-30 days",
                                                                    "15-30 DAYS" = "15-30 days",
                                                                    "15-30 Days" = "15-30 days",
                                                                    "15-30 days" = "15-30 days",
                                                                    "4-6 DAYS" = "4-6 days",
                                                                    "4-6 Days" = "4-6 days",
                                                                    "4-6 days" = "4-6 days",
                                                                    "7-14 DAYS" = "7-14 days",
                                                                    "7-14 Days" = "7-14 days",
                                                                    "7-14 days" = "7-14 days",
                                                                    "> 30 DAYS" = ">30 days",
                                                                    ">30 Days" = ">30 days",
                                                                    " >30 days" = ">30 days",
                                                                    ">30 days" = ">30 days",
                                                                    "> 30 days" = ">30 days",
                                                                    "No" = "NA",
                                                                    "Yes" = "NA",
                                                                    "<NA>" ="NA",))


NHSN_HavBed_Data %>% 
tabyl(on_hand_supply_of_n95_respirators_in_days)


NHSN_HavBed_Data <- NHSN_HavBed_Data %>% 
mutate(on_hand_supply_of_eye_protection_in_days = recode(on_hand_supply_of_eye_protection_in_days,
                                                          "0 DAYS" = "zero days",
                                                          "0 Days" = "zero days",
                                                          "0 days" = "zero days",
                                                          "1-3 DAYS" = "1-3 days",
                                                          "1-3 Days" = "1-3 days",
                                                          "1-3 days" = "1-3 days",
                                                          "15 - 30 days" = "15-30 days",
                                                          "15-30 DAYS" = "15-30 days",
                                                          "15-30 Days" = "15-30 days",
                                                          "15-30 days" = "15-30 days",
                                                          "4-6 DAYS" = "4-6 days",
                                                          "4-6 Days" = "4-6 days",
                                                          "4-6 days" = "4-6 days",
                                                          "7-14 DAYS" = "7-14 days",
                                                          "7-14 Days" = "7-14 days",
                                                          "7-14 days" = "7-14 days",
                                                          "> 30 DAYS" = ">30 days",
                                                          ">30 Days" = ">30 days",
                                                          " >30 days" = ">30 days",
                                                          ">30 days" = ">30 days",
                                                         "> 30 days" = ">30 days",
                                                          "No" = "NA",
                                                          "Yes" = "NA",
                                                          "yes" = "NA",
                                                         "NA" = "NA",
                                                          "<NA>" ="NA",))


NHSN_HavBed_Data %>% 
tabyl(on_hand_supply_of_eye_protection_in_days)


NHSN_HavBed_Data <- NHSN_HavBed_Data %>% 
mutate(on_hand_supply_of_gloves_in_days = recode(on_hand_supply_of_gloves_in_days,
                                                         "0 DAYS" = "zero days",
                                                         "0 Days" = "zero days",
                                                         "0 days" = "zero days",
                                                         "1-3 DAYS" = "1-3 days",
                                                         "1-3 Days" = "1-3 days",
                                                         "1-3 days" = "1-3 days",
                                                         "15 - 30 days" = "15-30 days",
                                                         "15-30 DAYS" = "15-30 days",
                                                         "15-30 Days" = "15-30 days",
                                                         "15-30 days" = "15-30 days",
                                                         "4-6 DAYS" = "4-6 days",
                                                         "4-6 Days" = "4-6 days",
                                                         "4-6 days" = "4-6 days",
                                                         "7-14 DAYS" = "7-14 days",
                                                         "7 - 14 days" = "7-14 days",
                                                         "7-14 Days" = "7-14 days",
                                                         "7-14 days" = "7-14 days",
                                                         "> 30 DAYS" = ">30 days",
                                                         ">30 Days" = ">30 days",
                                                         " >30 days" = ">30 days",
                                                         ">30 days" = ">30 days",
                                                        "> 30 days" = ">30 days",
                                                         "No" = "NA",
                                                         "Yes" = "NA",
                                                         "yes" = "NA",
                                                         "<NA>" ="NA",))


NHSN_HavBed_Data %>% 
tabyl(on_hand_supply_of_gloves_in_days) 


#create new variable gloves

library(dplyr)
df %>%
mutate(gloves = case_when(on_hand_supply_of_gloves_in_days == 'zero days' ~ '6',
                          on_hand_supply_of_gloves_in_days == '1-3 days' ~ '5',
                          on_hand_supply_of_gloves_in_days == '4-6 days' ~ '4',
                          on_hand_supply_of_gloves_in_days == '7-14 days' ~ '3',
                          on_hand_supply_of_gloves_in_days == '15-30 days' ~ '2',
                          on_hand_supply_of_gloves_in_days == '>30 days' ~ '1',
                           TRUE ~ 'NA'))
1 Like

Hello,

It would be helpful if you could post your question as a reprex that isolates where exactly in your code you are getting errors.

All the best,

Tim

1 Like

Good Morning,

The error seems to have occurred after this portion of the code

library(dplyr)
df %>%
  mutate(gloves = case_when(on_hand_supply_of_gloves_in_days == 'zero days' ~ '6',
                            on_hand_supply_of_gloves_in_days == '1-3 days' ~ '5',
                            on_hand_supply_of_gloves_in_days == '4-6 days' ~ '4',
                            on_hand_supply_of_gloves_in_days == '7-14 days' ~ '3',
                            on_hand_supply_of_gloves_in_days == '15-30 days' ~ '2',
                            on_hand_supply_of_gloves_in_days == '>30 days' ~ '1',
                             TRUE ~ 'NA'))

1 Like

@carmong I didn’t find the object called df in your code. Confirm if this is the name of the data frame object to which you want to apply the mutate function. This may be generating the error.

I imagine if you replace df with NHSN_HavBed_Data your code will work.

1 Like

Thank you for the suggestion. When I added that to my code I got the following message:

 library(dplyr)
NHSN_HavBed_Data %>%
  mutate(gloves = case_when(on_hand_supply_of_gloves_in_days == 'zero days' ~ '6',
                            on_hand_supply_of_gloves_in_days == '1-3 days' ~ '5',
                            on_hand_supply_of_gloves_in_days == '4-6 days' ~ '4',
                            on_hand_supply_of_gloves_in_days == '7-14 days' ~ '3',
                            on_hand_supply_of_gloves_in_days == '15-30 days' ~ '2',
                            on_hand_supply_of_gloves_in_days == '>30 days' ~ '1',
                             TRUE ~ 'NA'))


NHSN_HavBed_Data %>% 
  tabyl(gloves) 

> NHSN_HavBed_Data %>% 
+   tabyl(gloves) 
Error in `dplyr::select()`:
! Can't subset columns that don't exist.
✖ Column `gloves` doesn't exist.
Run `rlang::last_trace()` to see where the error occurred.

1 Like

Hello,

@lnielsen is correct, you called df but it was never defined. As well, in your most recent code you never saved NHSN_HavBed_Data with gloves added to it so when you called tabyl on it it wasn’t found.

Try the following code:

library(dplyr)

NHSN_HavBed_Data  <- NHSN_HavBed_Data %>%
  mutate(gloves = case_when(on_hand_supply_of_gloves_in_days == 'zero days' ~ '6',
                            on_hand_supply_of_gloves_in_days == '1-3 days' ~ '5',
                            on_hand_supply_of_gloves_in_days == '4-6 days' ~ '4',
                            on_hand_supply_of_gloves_in_days == '7-14 days' ~ '3',
                            on_hand_supply_of_gloves_in_days == '15-30 days' ~ '2',
                            on_hand_supply_of_gloves_in_days == '>30 days' ~ '1',
                             TRUE ~ 'NA'))


NHSN_HavBed_Data %>% 
  tabyl(gloves)

All the best,

Tim

2 Likes

The following code worked, thank you everyone for your help. This was my first attempt writing mutation code in R and it has been quite a challenge. Thank you again.

3 Likes

Thanks @carmong for posting - R will get easier with time and we are here to help!

If you have not yet reviewed it, I recommend this data cleaning chapter of our Epi R Handbook to learn more about mutate().

2 Likes