0

Pretty embarrassed to post this because it's super basic but I can't for the life of me figure out what's wrong. I have a dataframe with dates and gender.

# A tibble: 119,186 x 2
   date       gender
   <date>     <fct> 
 1 2020-01-02 male  
 2 2020-01-02 male  
 3 2020-01-02 male  
 4 2020-01-02 female
 5 2020-01-02 male  
 6 2020-01-02 female
 7 2019-12-25 male  
 8 2019-12-25 male  
 9 2019-12-25 female
10 2019-12-25 female
# … with 119,176 more rows

I'm trying to create a dataframe that is sorted by date and gender with the count of each - here's a hypothetical

   date       gender   count 
 1 2020-01-02 male     4
 2 2020-01-02 female   2
 3 2019-12-25 male     2
 4 2019-12-25 female.  2
 ... etc 

I have tried a bunch of things to get this to work, like counting:

> df_gender %>% 
+   group_by(date) %>%
+   summarize(count = count(gender))
  count.x count.freq
1    male      69217
2  female      49969

And adding a column:

date       gender cases
   <date>     <fct>  <dbl>
 1 2020-01-02 male       1
 2 2020-01-02 male       1
 3 2020-01-02 male       1
 4 2020-01-02 female     1
 5 2020-01-02 male       1
 6 2020-01-02 female     1
 7 2019-12-25 male       1
 8 2019-12-25 male       1
 9 2019-12-25 female     1
10 2019-12-25 female     1
# … with 119,176 more rows

> df_gender %>% 
+   group_by(date) %>%
+   summarize(count = sum(cases))
   count
1 119186

I feel like this should work (and has worked in other contexts) - so I've been troubleshooting my date variable. I think it's properly formatted:

glimpse(df_gender$date)
 Date[1:119186], format: "2020-01-02" "2020-01-02" "2020-01-02" "2020-01-02" "2020-01-02" ...

Vaguely losing my mind here. Thanks in advance for any help and sorry for wasting time with this!

  • 2
    Just do `count(df_gender, date, gender)`? – arg0naut91 Feb 18 '20 at 16:41
  • No sadly not. I'm not sure what I'm doing wrong. Running that gives me: > count(df_gender, date, gender) Error in UseMethod("as.quoted") : no applicable method for 'as.quoted' applied to an object of class "function" – Michael Putman Feb 18 '20 at 21:46
  • The dplyr solution from the link above gives this: – Michael Putman Feb 18 '20 at 21:49
  • > df_gender %>% + group_by(date, gender) %>% + mutate(count = n()) Error: n() should only be called in a data context Run `rlang::last_error()` to see where the error occurred. > – Michael Putman Feb 18 '20 at 21:49
  • There are conflicts somewhere. Try restarting R, load only `dplyr` without any other package, especially not `plyr`, and redo. The function should work fine. – arg0naut91 Feb 18 '20 at 22:44
  • 1
    Ugh that absolutely fixed it. plyr was clearly the problem. THANK YOU for spending your time on such a worthless issue. Really appreciate it. – Michael Putman Feb 19 '20 at 00:09

0 Answers0