Error: `n()` must only be used inside dplyr verbs

chi_table <-
survey_data %>%
group_by(birthmonth) %>%
ungroup()
summarise(
observed = n(),
expected = totaln/12,
diff = observed-expected,
sq_diff = diff^2,
std_sq_diff = sq_diff / expected
)
chi_table

I am trying to get R to create a tibble for this, but it keeps having errors. First it said that summarise was an ungrouping output and to override with .groups, but that didn't work so I put ungroup before summary and now it is saying that n() can only be used inside dplyr verbs even though it is in one, summarise. Can someone please help me.

Looks like you are missing a pipe operator (%>%) before summarise.

I also don't think that you want to have ungroup() directly behind group_by() since it removes the grouping your are adding right before. The message you got only tells you that your output will not keep the grouping that you have specified before but the code still works. I suppose the message is just there to make clear what behaviour you should expect from your results.

Without knowing your data, I guess that this should work:

chi_table <-survey_data %>%
    group_by(birthmonth) %>%
    summarise(observed = n(),
              expected = totaln/12,
              diff = observed-expected,
              sq_diff = diff^2,
              std_sq_diff = sq_diff / expected)
1 Like

I got the same, but also note that expected won't work as it doesn't refer to a column, I've assumed you meant observed/12 as observed was n().

chi_table <- survey_data %>%
  group_by(birthmonth) %>%
  summarise(observed = n(),
            expected = observed/12,
            diff = observed-expected,
            sq_diff = diff^2,
            std_sq_diff = sq_diff / expected
  )

chi_table

It is some data set from my class.

chi_table <- survey_data %>%

  • group_by(birthmonth) %>%
    
  • summarise(observed = n(),
    
  •           expected = totaln/12
    
  •           diff = observed-expected
    

Error: unexpected symbol in:
" expected = totaln/12
diff"

          sq_diff = diff^2

Error in diff^2 : non-numeric argument to binary operator

          std_sq_diff = sq_diff/expected

Error: object 'sq_diff' not found

)

Error: unexpected ')' in " )"

I have no idea what this means.

This is now showing up. Do I need a certain package?

chi_table <- survey_data %>%

  • group_by(birthmonth) %>%
    
  • summarise(observed = n(),
    
  •           expected = totaln/12
    
  •           diff = observed-expected
    

Error: unexpected symbol in:
" expected = totaln/12
diff"

          sq_diff = diff^2

Error in diff^2 : non-numeric argument to binary operator

          std_sq_diff = sq_diff/expected

Error: object 'sq_diff' not found

)

Error: unexpected ')' in " )"

This looks like a copying issue as you no longer have commas at the end of each line:

chi_table <- survey_data %>%

group_by(birthmonth) %>%
summarise(observed = n(),
          expected = totaln/12 # missing comma
          diff = observed-expected # missing comma
  sq_diff = diff^2 # missing comma
std_sq_diff = sq_diff/expected
)

Without the commas the programme is reading it all as one line, like missing punctuation in a paragraph makes no sense. The error Error: unexpected ')' in " )" is suggesting you are missing an end bracket but that's not what is missing in this case but it's a common thing to miss.

chi_table <- survey_data %>%
group_by(birthmonth) %>%
summarise(observed = n(),
          expected = totaln/12,
          diff = observed-expected,
          sq_diff = diff^2,
          std_sq_diff = sq_diff/expected
)
1 Like

It worked! Thank you so much!

1 Like

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.