Group_by / summary语句不适用于多个语句

时间:2019-11-22 20:30:47

标签: r dplyr

有人可以告诉我为什么这不起作用吗?

 me_totals_by_county <- me %>%
  group_by(redcap_data_access_group)%>%
  summarise(
            (alcohol_number_occurences = sum(grepl('alcohol', me_cause, ignore.case = TRUE) | grepl('ethanol', me_cause, ignore.case = TRUE))),
            (fentanyl_number_occurences = sum(grepl('alcohol', me_cause, ignore.case = TRUE)))
            )
me_totals_by_county

它没有给出任何错误,但是无法产生预期的输出:

       redcap_data_access_group `(...)`
   <chr>                      <int>
 1 c1                             0
 2 c2                             0
 3 c3                             1
 4 c4                             0

谢谢。

1 个答案:

答案 0 :(得分:1)

您似乎有几个额外的左括号。试试这个:

 me_totals_by_county <- me %>%
  group_by(redcap_data_access_group)%>%
  summarise(
            alcohol_number_occurences = sum(grepl('alcohol', me_cause, ignore.case = TRUE) | grepl('ethanol', me_cause, ignore.case = TRUE))),
            fentanyl_number_occurences = sum(grepl('alcohol', me_cause, ignore.case = TRUE)))
            )
me_totals_by_county