Counting and grouped by year

Hi everyone, I am new to R and I thank you ahead for any help!

I have created a MasterTable with a large hospital dataset that includes different demographic data for children who are admitted that includes year and if they had any complications during their admission. I want to find out a count of complications by year. In the sample dataset, I only made it span 5 years, but the actual dataset spans many more years. I tried the code below (shown underneath an example dataset) but it returned with this error:

"Error: Problem with summarise() input AnyComp.
x no applicable method for 'group_vars' applied to an object of class "c('double', 'numeric')"
i Input AnyComp is count(AllComplication).
i The error occurred in group 1: Discharge.Year = 2014."

Dataset:

MasterTable <- data.frame(
  Patient.ID = c("A", "B", "C", "C", "C", "D", "D", "E", "F", "G", "G"),
  Admit.ID = c("1Zz", "1Yy", "5Pp", "3Cc", "9Dd", "4Yy", "4Dd", "2Aa", "6Rr", "8Ee", "7Pp"),
  Gender = c("Female", "Male", "Male", "Male", "Male", "Female", "Female", "Male", "Female", "Male", "Male"),
  AnyComplication = c(0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0),
  Discharge.year = c("2017", "2014", "2015", "2016", "2015", "2017", "2018", "2014", "2018", "2015", "2016"),
  LOS.Days = c(0.59, 4.35, 1.89, 6.02, 2.76, 1.99, 0.96, 11.52, 1.57, 4.16, 3.74))

AnyComplications <-MasterTable %>% group_by(Discharge.Year) %>%
  summarize(AnyComp = count(AnyComplication))%>%
  ungroup()"

Created on 2021-05-04 by the reprex package (v0.3.0)

Is this what you wanted?

library(dplyr)

AnyComplications <- 
  MasterTable %>% 
  group_by(Discharge.year) %>%
  summarize(AnyComp = sum(AnyComplication)) %>%
  ungroup()

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.