Error with dyplr::mutate function prevents shiny app from running

Hello,

I've been trying to run some code that had worked previously but now I'm receiving an error.

I'm importing a large data set that includes columns of dates along with NA values. R doesn't recognize the dates as such and imports the data as factors. As part of the initial data clean up, I'm converting the factors to dates and then creating additional columns to turn the presence of a date into a 1 and the absence a 0 with dplyr::mutate. Here's the code I run:

library(dplyr)
library(chron)
library(lubridate)

#Convert Inquiry, Prospect, Final Decision Date, Started, Submitted, Marked Complete, Moved to ERP, Admitted, Denied, Confirmed, Withdrawn, Enrolled columns from Factors to Dates
adm$Inquiry <- as.Date(adm$Inquiry, "%m/%d/%Y")
adm$Prospect <- as.Date(adm$Prospect, "%m/%d/%Y")
adm$Final.Decision.Date <- as.Date(adm$Final.Decision.Date, "%m/%d/%Y")
adm$Started <- as.Date(adm$Started, "%m/%d/%Y")
adm$Submitted <- as.Date(adm$Submitted, "%m/%d/%Y")
adm$Marked.Complete <- as.Date(adm$Marked.Complete, "%m/%d/%Y")
adm$Moved.to.ERP <- as.Date(adm$Moved.to.ERP, "%m/%d/%Y")
adm$Admitted <- as.Date(adm$Admitted, "%m/%d/%Y")
adm$Denied <- as.Date(adm$Denied, "%m/%d/%Y")
adm$Confirmed <- as.Date(adm$Confirmed, "%m/%d/%Y")
adm$Withdrawn <- as.Date(adm$Withdrawn, "%m/%d/%Y")
adm$Enrolled <- as.Date(adm$Enrolled, "%m/%d/%Y")

#Creating a new column that will convert the date in the Inquriy column into a 1 if the date value is on or before today's date.
adm$InquiryCount <- dplyr::mutate(adm$InquiryCount <- ifelse(adm$Inquiry <= today(tzone=""), 1, 0))
adm$ProspectCount <- dplyr::mutate(adm$ProspectCount <- ifelse(adm$Prospect <= today(tzone=""), 1, 0))
adm$FinalDecCount <- dplyr::mutate(adm$FinalDecCount <- ifelse(adm$Final.Decision.Date <= today(tzone=""), 1, 0))
adm$StartCount <- dplyr::mutate(adm$StartCount <- ifelse(adm$Started <= today(tzone=""), 1, 0))
adm$SubmitCount <- dplyr::mutate(adm$SubmitCount <- ifelse(adm$Submitted <= today(tzone=""), 1, 0))
adm$CompleteCount <- dplyr::mutate(adm$CompleteCount <- ifelse(adm$Marked.Complete <= today(tzone=""), 1, 0))
adm$MoveERPCount <- dplyr::mutate(adm$MoveERPCount <- ifelse(adm$Moved.to.ERP <= today(tzone=""), 1, 0))
adm$AdmitCount <- dplyr::mutate(adm$AdmitCount <- ifelse(adm$Admitted <= today(tzone=""), 1, 0))
adm$DenyCount <- dplyr::mutate(adm$DenyCount <- ifelse(adm$Denied <= today(tzone=""), 1, 0))
adm$ConfirmCount <- dplyr::mutate(adm$ConfirmCount <- ifelse(adm$Confirmed <= today(tzone=""), 1, 0))
adm$WithdrawCount <- dplyr::mutate(adm$WithdrawCount <- ifelse(adm$Withdrawn <= today(tzone=""), 1, 0))
adm$EnrollCount <- dplyr::mutate(adm$EnrollCount <- ifelse(adm$Enrolled <= today(tzone=""), 1, 0))

When I run the code I receive the following error:

Error in UseMethod("mutate_") : 
  no applicable method for 'mutate_' applied to an object of class "c('double', 'numeric')"

The new columns are created properly in the data frame and I can move forward, but when I attempt to run a shiny app locally or publish to shinapps.io, the error blocks the deployment of the app.

Any thoughts?

Hi there!

You want to rewrite your code to follow the mutate argument conventions of data <- mutate(data, new_name = value)

Check it:

iris$Species <- mutate(iris$Species <- ifelse(iris$Species == iris$Species[1], 1, 0))
Error in UseMethod("mutate_") : 
  no applicable method for 'mutate_' applied to an object of class "c('double', 'numeric')"

Rather:

iris <- mutate(iris, Species = ifelse(Species == Species[1], 1, 0))

head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2       1
2          4.9         3.0          1.4         0.2       1
3          4.7         3.2          1.3         0.2       1
4          4.6         3.1          1.5         0.2       1
5          5.0         3.6          1.4         0.2       1
6          5.4         3.9          1.7         0.4       1

Plus, you can capitalize on the benefits of dplyr by mutating a bunch of variables at the same time!

adm <-
  adm %>%
    mutate_all(list(count = ~ ifelse(. <= today(tzone=""), 1, 0)))
1 Like

Okay, moving in the right direction but experiencing a different issue now. Per you advice I've updated the code for a single column to:

adm <- mutate(adm, adm$InquiryCount <- ifelse(adm$Inquiry <= today(tzone=""), 1, 0))

But now the column name in the data frame is NULL.

Just a bit more detail. The column 'Inquiry' exists in the dataframe before the code above. I want to create the column 'InquiryCount' applying the formulate against the date values in the Inquiry column.

Also, thank you for the 'mutate_all' tip. I'm not mutating all but I'll look into 'mutate_if'

Try this instead:
adm <- mutate(adm, InquiryCount = ifelse(Inquiry <= today(tzone=""), 1, 0))

That did it! Thank you.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.