
Here we briefly address how its behavior changes when applied to grouped data.
#Dplyr summarize sum if how to#
See the dplyr section of the Descriptive tables page for a detailed description of how to produce summary tables with summarise(). # bmi, days_onset_hosp, and abbreviated variable names ¹date_infection, ²date_hospitalisation, ³date_outcome, ⁴age_unit, ⁵age_years, ⁶age_cat5, # … with 5,878 more rows, 10 more variables: ct_blood, fever, chills, cough, aches, vomit, temp, time_admission , # case_id generation date_infec…¹ date_onset date_hos…² date_out…³ outcome gender age age_u…⁴ age_y…⁵ age_cat age_c…⁶ hospi…⁷ lon lat infec…⁸ source wt_kg ht_cm # print to see which groups are active ll_by_outcome # A tibble: 5,888 × 30

Sadly, dplyr is commonly taught without explaining this extremely important distinction.īecause ncases is not the name of an object in the calling environment or one of its parents, you get the error "that it didn't find the ncases object". Most functions in R do not work that way.

dplyr's "verb" functions like filter(), summarize(), select(), etc) use a special kind of non-standard evaluation where expressions can also be evaluated to the name of a variable on the first argument. ncases is the name of a variable, or "column", in the ame, but sum() does not "know" this, because it uses standard R evaluation and looks for objects that have that symbol. (the ame returned by the prior operation) and ncases (doesn't exist). You have supplied as arguments the expressions. Should missing values (including NaN) be removed?Įach argument to sum() must be a numeric, complex or logical vector.

Sum of Vector Elements Description sum returns the sum of all the values present in its arguments. You can determine this by looking at the documentation for the function: ?sum The base::sum() function cannot be used on a ame() object directly. esoph %>% filter(alcgp="120+") returns a ame object. The pipe operator supplies the last evaluated value as the first argument to the subsequent function.
