I'm struggling to get the following code working. The data I have is a data.frame of a physical test. Athletes who did the test are classified based on a 'Sport Specific' paramater.
wingate_benchmarks <- wingate_data %>%
select(`Sport Specific`,`Minimum power output`,`Average power output`,
`Relative Average Power`,`Peak Power`,`Time to peak`,`Max. RPM`,`Time to Max. RPM`,`Total Work`) %>%
group_by(`Sport Specific`) %>%
dplyr::summarize_at(vars(`Minimum power output`,`Average power output`,
`Relative Average Power`,`Peak Power`,`Time to peak`,`Max. RPM`,`Time to Max. RPM`,`Total Work`),
list(mean = mean, sd = sqrt((n()-1)/n())*sd))
If I use only sd, it calculates the Standard Deviation as if the data is a sample, but it should be considered as the full popluation. Hence the sqrt((n()-1)/n()) addition.
But R keeps returning: Error: n() must only be used inside dplyr verbs.
Is there anyway to solve this? Thanks!
Here's an attempt, not certain if it will work with your data.
We can see it in action using
mtcars:As @Limey said in their comment, the
summarize_*functions have been superseded byacross, which generally takes two arguments: the variables (intidyselectfashion), and some form of function(s).The functions can be provided in several ways:
summarize(across(vs:carb, mean));summarize(across(vs:carb, function(z) mean(z)/2));rlang-style tilde funcs,summarize(across(vs:carb, ~ mean(.))), where the.is replaced with the column (vector); ormtcarsworking answer above.