0

Colleagues, I want to summarise a data set, grouped by factors. dplyr::summarise is happpy to give me sample sizes with n(), but gives an error, argument "x" is missing with no default when I ask for something else, such as mean(). Any suggestions on what I'm doing wrong?

library(dplyr)

#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

# create a dataset

data <- data.frame(
  name = c(rep("A", 500), rep("B", 500), rep("C", 20), rep("D", 100)),
  value = c(rnorm(500, 10, 5), rnorm(500, 13, 1), rnorm(20, 25, 4), rnorm(100, 12, 1))
)

# sample size

sample_size <- data |>
  group_by(name) |>
  summarize(num = n())

# sample means

sample_mean <- data |>
group_by(name) |>
summarise(mu = mean())

#> Error: Problem with `summarise()` column `mu`.
#> i `mu = mean()`.
#> x argument "x" is missing, with no default
#> i The error occurred in group 1: name = "A".

Created on 2021-06-17 by the reprex package (v2.0.0)

WinzarH
  • 65
  • 8
  • 3
    As you are loading the `dplyr`, can use `%>%` instead of `|>`. The summarise step would be `summarize(num = mean(value))` `n()` is a special function which doesn't need any argument, while `mean` and other functions need the column name as unquoted – akrun Jun 16 '21 at 23:44
  • 2
    First, use `%>%` instead of `|%`. Second, `summarise(mu = mean(value))`. – Zhiqiang Wang Jun 16 '21 at 23:50
  • Thank you _akrun_ and _zhiqiang-wang_ for your very quick replys. I knew it would be something embarrassingly simple. – WinzarH Jun 16 '21 at 23:57

0 Answers0