Function not creating cumulative output.


I am trying to create a cumulative column with dplyr functions. The output results in a count not a cumsum.


head(crimeD, 10)

Description / District / Crime / DateOccur <S3: POSIXct>/ CodedMonth /Complaint / NHD_NAME
HOMICIDE 6 10000 2016-06-09 08:28:00 2018-06-28 16-027833 Penrose
HOMICIDE 4 10000 2017-09-01 13:44:00 2018-12-28 17-043306 Columbus Square
HOMICIDE 1 10000 2017-02-22 23:45:00 2018-02-28 18-008475 Holly Hills
HOMICIDE 6 10000 2018-06-10 00:01:00 2019-03-28 18-026258 Baden
HOMICIDE 6 10000 2018-05-28 00:01:00 2018-11-28 18-023972 Greater Ville
HOMICIDE 5 10000 2018-05-02 17:05:00 2018-10-28 18-019567 Fountain Park
HOMICIDE 5 10000 2018-05-02 17:05:00 2018-10-28 18-019567 Fountain Park
HOMICIDE 3 10000 2018-07-27 20:15:00 2018-11-28 18-034672 Gravois Park



# Neighborhood By Name   
# Group by Neighborhood and count
crimeD  %>%
  mutate_if(is.factor,
                      fct_explicit_na,
                      na_level = "to_impute") %>%
  group_by(NHD_NAME) %>%
  count(Crime, sort = TRUE) %>%
  arrange(desc(n))%>%
  mutate(cumulative = cumsum(n))

NHD_NAME / Crime n cumulative
Wells Goodfellow 10000 32 32
Baden 10000 27 27
Greater Ville 10000 24 24
Dutchtown 10000 22 22
Penrose 10000 22 22
Walnut Park East 10000 18 18
Hamilton Heights 10000 16 16
Jeff Vanderlou 10000 15 15
Walnut Park West 10000 13 13
Fountain Park 10000 12 12


Never mind, I just found another message that said to "ungroup". It worked!

crimeD  %>%
 mutate_if(is.factor,
                     fct_explicit_na,
                     na_level = "to_impute") %>%
 group_by(NHD_NAME) %>%
 count(Crime, sort = TRUE) %>%
 arrange(desc(n)) %>%
 ungroup()%>%
 mutate (cumulative = cumsum(n))

'```

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