Skip to content

Error using n() #169

@rdboyes

Description

@rdboyes

I came across this issue while writing some examples for TidierPlots. It may be that I'm just doing something wrong, but this code:

df_h = @chain penguins begin
    @group_by(species)
    @summarize(
        mean_bill = mean(bill_length_mm),
        se_bill = std(bill_length_mm) / sqrt(n())
    )
    @mutate(
        lower = mean_bill - 1.96 * se_bill,
        upper = mean_bill + 1.96 * se_bill
    )
end

Fails with error "No method matching nrow()". You can get this to run by separating the steps, like so:

df_h = @chain penguins begin
    @group_by(species)
    @summarize(
        mean_bill = mean(bill_length_mm),
        sd_bill = std(bill_length_mm),
        n = n()
    )
    @mutate(
        se_bill = sd_bill / sqrt(n)
    )
    @mutate(
        lower = mean_bill - 1.96 * se_bill,
        upper = mean_bill + 1.96 * se_bill
    )
end

But I don't think you would need to separate it out like this in dplyr

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions