Error in summarise_impl(.data, dots): Evaluation error: invalid 'type' (character) of argument

rstudio

#1

Hello,
I have the following error.

library(tidyverse) 
library(scales) 
#> 
#> Attaching package: 'scales'
#> The following object is masked from 'package:purrr':
#> 
#>     discard
#> The following object is masked from 'package:readr':
#> 
#>     col_factor
library(forcats) 
library(dplyr)
library(ggplot2)
library(data.table)
#> 
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last
#> The following object is masked from 'package:purrr':
#> 
#>     transpose
library(reprex)
library(cowplot)
#> 
#> Attaching package: 'cowplot'
#> The following object is masked from 'package:ggplot2':
#> 
#>     ggsave
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:data.table':
#> 
#>     hour, isoweek, mday, minute, month, quarter, second, wday,
#>     week, yday, year
#> The following object is masked from 'package:base':
#> 
#>     date
library(RColorBrewer)
dput(datt_merge[1:20,])
#> Error in dput(datt_merge[1:20, ]): object 'datt_merge' not found

data = structure(list(CUSTOMER_NUMBER = c(0L, 209020998L, 209100011L, 
                                          209100058L, 209100072L, 209100080L, 209100404L, 209100520L, 209100821L, 
                                          209101604L, 209103004L, 209104201L, 209104202L, 209106542L, 209107926L, 
                                          209108266L, 209108384L, 209108744L, 209109816L, 209109929L), 
                      Cluster = structure(c(6L, 2L, 5L, 2L, 1L, 2L, 2L, 6L, 2L, 
                                            1L, 6L, 4L, 1L, 2L, 2L, 3L, 4L, 6L, 1L, 4L), .Label = c("1", 
                                                                                                    "2", "3", "4", "5", "6"), class = "factor"), PROGRAM_LEVEL_DESCR = structure(c(11L, 
                                                                                                                                                                                   11L, 11L, 7L, 14L, 11L, 11L, 11L, 12L, 11L, 11L, 14L, 11L, 
                                                                                                                                                                                   14L, 11L, 11L, 12L, 11L, 11L, 9L), .Label = c("Branch Refusal", 
                                                                                                                                                                                                                                 "Club", "Corporate Refusal", "Credit Hold", "Customer Refusal", 
                                                                                                                                                                                                                                 "Diamond", "Enrollment", "Failed 2X in Calendar Year", "Gold", 
                                                                                                                                                                                                                                 "Institutional", "No Program", "Platinum", "RSVP", "Silver"
                                                                                                                                                                                   ), class = "factor"), Handpieces.x = c(1, 0, 0, 0, 0, 0, 
                                                                                                                                                                                                                          0, 0, 10, 0, 0, 2, 0, 3, 10, 0, 2, 2, 0, 4), `PRIVATE LABEL.x` = c(2, 
                                                                                                                                                                                                                                                                                             85, 0, 0, 553, 0, 3, 0, 1288, 2, 0, 454, 1, 702, 184, 0, 
                                                                                                                                                                                                                                                                                             347, 24, 54, 897), `SUND DISC.x` = c(0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                                                                                                                                                                                                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), SUNDRY.x = c(133, 
                                                                                                                                                                                                                                                                                                                                                                                       167, 66, 127, 490, 1, 20, 12, 1974, 2, 12, 560, 2, 656, 337, 
                                                                                                                                                                                                                                                                                                                                                                                       67, 1440, 8, 65, 817), Total_Orders = c(136, 252, 66, 127, 
                                                                                                                                                                                                                                                                                                                                                                                                                               1043, 1, 23, 12, 3272, 4, 12, 1016, 3, 1361, 531, 67, 1789, 
                                                                                                                                                                                                                                                                                                                                                                                                                               34, 119, 1718), PL_Order_Percentage = c(1.47, 33.73, 0, 0, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                       53.02, 0, 13.04, 0, 39.36, 50, 0, 44.69, 33.33, 51.58, 34.65, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                       0, 19.4, 70.59, 45.38, 52.21), Handpieces.y = c(23, 0, 0, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       0, 0, 0, 0, 0, 12073.4, 0, 0, 289.25, 0, 1485.95, 6633.44, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       0, 1752.05, 396.4, 0, 3390.7), `PRIVATE LABEL.y` = c(96.74, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            2448.25, 0, 0, 6593.85, 0, 80.45, 0, 19704.78, 25, 0, 8249.15, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            25.15, 10954.25, 1819.83, 0, 12489.05, 502.25, 553.93, 17949.9
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ), `SUND DISC.y` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            0, 0, 0, 0, 0, 0, 0, 0), SUNDRY.y = c(6177.78, 11495.25, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  11715.85, 4193.6, 28834.85, 36.75, 1355.4, 1006.2, 106976.7, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  70.9, 2416.8, 52468.77, 66.25, 27370.09, 13765.64, 718.1, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  77445.12, 1304.6, 1778.05, 45650.3), Total_Sales = c(6297.52, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       13943.5, 11715.85, 4193.6, 35428.7, 36.75, 1435.85, 1006.2, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       138754.88, 95.9, 2416.8, 61007.17, 91.4, 39810.29, 22218.91, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       718.1, 91686.22, 2203.25, 2331.98, 66990.9)), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   20L), class = "data.frame")
new_datt_merge = 
  data %>%
  mutate(category = 
           cut(PL_Order_Percentage,breaks = c(0,1,11,21,31,41,51,61,71,81,91,100,Inf), 
               labels = c('0% - < 1%','1%-10%','11%-20%',
                          '21%-30%','31%-40%','41%-50%',
                          '51%-60%','61%-70%','71%-80%',
                          '81%-90%','91% - < 100%','100%'),include.lowest = T,right = F)) %>%
  select(CUSTOMER_NUMBER, Cluster,PL_Order_Percentage,
         "PRIVATE LABEL.y","SUNDRY.y","Handpieces.y", Total_Sales, category) %>%
  gather(bucket,value,-category) %>%
  filter(bucket %in% c("PRIVATE LABEL.y","SUNDRY.y","Handpieces.y")) %>%
  group_by(category, bucket) %>%
  summarise(count = n(), 
            total_Sales = sum(value)) %>%
  spread(bucket, total_Sales, fill = 0) %>% 
  na.omit() %>%
  mutate(NonPL_Sales = `SUNDRY.y` + `Handpieces.y`,
         PL_Sales = `PRIVATE LABEL.y`,
         percent = count/sum(count)*100)
#> Warning: attributes are not identical across measure variables;
#> they will be dropped
#> Error in summarise_impl(.data, dots): Evaluation error: invalid 'type' (character) of argument.

Created on 2018-07-21 by the reprex
package
(v0.2.0).


#2

I think you need to convert Cluster from a factor to numeric before you do the gather step...

select(CUSTOMER_NUMBER, Cluster,PL_Order_Percentage,
         "PRIVATE LABEL.y","SUNDRY.y","Handpieces.y", Total_Sales, category) %>%
  ### At this point, Cluster is a factor while the other fields are numeric.
  ### The gather function doesn't know how to combine them and turns them into characters,
  ### which can't be summed later.
  ### Let's avoid that by making Cluster numeric...
  mutate(Cluster = as.numeric(Cluster)) %>%

  gather(bucket,value,-category) %>%