0
votes

My data concerns a company and includes Total Sales and the amount of sales in three counties CA , TX and WI.

Data :

> dput(head(WalData))
structure(list(CA = c(11047, 9925, 11322, 12251, 16610, 14696
), TX = c(7381, 5912, 9006, 6226, 9440, 9376), WI = c(6984, 3309, 
8883, 9533, 11882, 8664), Total = c(25412, 19146, 29211, 28010, 
37932, 32736), date = structure(c(1296518400, 1296604800, 1296691200, 
1296777600, 1296864000, 1296950400), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), event_type = c("NA", "NA", "NA", "NA", "NA", "Sporting"
), snap_CA = c(1, 1, 1, 1, 1, 1), snap_TX = c(1, 0, 1, 0, 1, 
1), snap_WI = c(0, 1, 1, 0, 1, 1)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

With the following code i am trying to calculate the average sales share of the three states on the company's total sales.

In addition, i need the same average percentages for each year, month of the year and day of the week.

install.packages("dplyr")
install.packages("lubridate")
library(dplyr)
library(lubridate)

df1 <- df %>% 
  dplyr::mutate(YEAR = lubridate::year(date),
                MONTH = lubridate::month(date),
                WEEKDAY = lubridate::wday(date),
                P_CA = CA / Total,
                P_TX = TX / Total,
                P_WI = WI / Total) 

# Average per Year
df1 %>% 
  dplyr::group_by(YEAR) %>% 
  dplyr::summarise(AV_CA = mean(P_CA, na.rm = TRUE),
                   AV_TX = mean(P_TX, na.rm = TRUE),
                   AV_WI = mean(P_WI, na.rm = TRUE))

# Average per Month
df1 %>% 
  dplyr::group_by(MONTH) %>% 
  dplyr::summarise(AV_CA = mean(P_CA, na.rm = TRUE),
                   AV_TX = mean(P_TX, na.rm = TRUE),
                   AV_WI = mean(P_WI, na.rm = TRUE))

# Average per Weekday
df1 %>% 
  dplyr::group_by(WEEKDAY) %>% 
  dplyr::summarise(AV_CA = mean(P_CA, na.rm = TRUE),
                   AV_TX = mean(P_TX, na.rm = TRUE),
                   AV_WI = mean(P_WI, na.rm = TRUE))

Output :

> df1 <- df %>% 
+   dplyr::mutate(YEAR = lubridate::year(date),
+                 MONTH = lubridate::month(date),
+                 WEEKDAY = lubridate::wday(date),
+                 P_CA = CA / Total,
+                 P_TX = TX / Total,
+                 P_WI = WI / Total) 
Error in UseMethod("mutate_") : 
  no applicable method for 'mutate_' applied to an object of class "function"
> # Average per Year
> df1 %>% 
+   dplyr::group_by(YEAR) %>% 
+   dplyr::summarise(AV_CA = mean(P_CA, na.rm = TRUE),
+                    AV_TX = mean(P_TX, na.rm = TRUE),
+                    AV_WI = mean(P_WI, na.rm = TRUE))
Error in eval(lhs, parent, parent) : object 'df1' not found

It comes with an error : Error in UseMethod("mutate_") : no applicable method for 'mutate_' applied to an object of class "function"

I cant figure out whats wrong , i double checked the code and the correctness of the data . Please give a solution .

1
Not able to reproduce the erorr. Is the df the same as 'WalData` - akrun
I posted a soluiton below which shows exactly why it is not working. - akrun
I had to realize that I had made this serious omission. I had not assigned my dataframe to df. Μy big mistake ! Thank you very much for your clear thinking and intervention. @akrun - ilaias zark HD
Appreciate your help and time @akrun - ilaias zark HD

1 Answers

0
votes

The issue would be that df is not created as an object in the global env and there is a function with name df if we do ?df

df(x, df1, df2, ncp, log = FALSE)

Basically, the error is based on applying mutate on a function df rather than an object


Checking on a fresh R session with no objects created

df %>% 
   dplyr::mutate(YEAR = lubridate::year(date),
                 MONTH = lubridate::month(date),
                 WEEKDAY = lubridate::wday(date),
                 P_CA = CA / Total,
                 P_TX = TX / Total,
                 P_WI = WI / Total) 

Error in UseMethod("mutate_") : no applicable method for 'mutate_' applied to an object of class "function"

Now, we define 'df' as

df <- WalData
df %>% 
   dplyr::mutate(YEAR = lubridate::year(date),
                 MONTH = lubridate::month(date),
                 WEEKDAY = lubridate::wday(date),
                 P_CA = CA / Total,
                 P_TX = TX / Total,
                 P_WI = WI / Total)
# A tibble: 6 x 15
#     CA    TX    WI Total date                event_type snap_CA snap_TX snap_WI  YEAR MONTH WEEKDAY  P_CA  P_TX  P_WI
#  <dbl> <dbl> <dbl> <dbl> <dttm>              <chr>        <dbl>   <dbl>   <dbl> <dbl> <dbl>   <dbl> <dbl> <dbl> <dbl>
#1 11047  7381  6984 25412 2011-02-01 00:00:00 NA               1       1       0  2011     2       3 0.435 0.290 0.275
#2  9925  5912  3309 19146 2011-02-02 00:00:00 NA               1       0       1  2011     2       4 0.518 0.309 0.173
#3 11322  9006  8883 29211 2011-02-03 00:00:00 NA               1       1       1  2011     2       5 0.388 0.308 0.304
#4 12251  6226  9533 28010 2011-02-04 00:00:00 NA               1       0       0  2011     2       6 0.437 0.222 0.340
#5 16610  9440 11882 37932 2011-02-05 00:00:00 NA               1       1       1  2011     2       7 0.438 0.249 0.313
#6 14696  9376  8664 32736 2011-02-06 00:00:00 Sporting         1       1       1  2011     2       1 0.449 0.286 0.265