Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of each factor grouping by another factor

I know the answer to this question will be simple but I have searched the forums extensively and I have been unable to find a solution.

I have a column called Data_source which is a factor that I want to group my variables by.

I have a series of symptom* variables where I want the counts according to Data_source.

For some reason, I am unable to figure out how to do this. The normal group_by functions do not seem to work appropriately.

Here is the dataframe in question

 df <- wrapr::build_frame(
   "Data_source"  , "Sex"   , "symptoms_decLOC", "symptoms_nausea_vomitting" |
     "1"          , "Female", NA_character_    , NA_character_               |
     "1"          , "Female", NA_character_    , NA_character_               |
     "1"          , "Female", "No"             , NA_character_               |
     "1"          , "Female", "Yes"            , "No"                        |
     "1"          , "Female", "Yes"            , "No"                        |
     "1"          , "Female", "Yes"            , "No"                        |
     "1"          , "Male"  , "Yes"            , "No"                        |
     "1"          , "Female", "Yes"            , "No"                        |
     "2"          , "Female", NA_character_    , NA_character_               |
     "2"          , "Male"  , NA_character_    , NA_character_               |
     "2"          , "Male"  , NA_character_    , NA_character_               |
     "2"          , "Female", "Yes"            , "No"                        |
     "2"          , "Female", "Yes"            , "No"                        |
     "2"          , "Male"  , NA_character_    , NA_character_               |
     "2"          , "Male"  , NA_character_    , NA_character_               |
     "2"          , "Male"  , NA_character_    , NA_character_               |
     "2"          , "Female", NA_character_    , NA_character_               |
     "2"          , "Female", NA_character_    , NA_character_               |
     "2"          , "Male"  , NA_character_    , NA_character_               |
     "2"          , "Female", NA_character_    , NA_character_               )

Notice that Sex and the symptoms variables are all factors which include NA's. I have attempted the following

df %>% na.omit() %>% group_by(Data_source) %>% count("symptoms_decLOC")

Which does not work and is less than optimal because I would have to repeat it for every column. The ideal would be to use something similar to lapply(df, count) but this does not give me description for each group.

EDIT

In response to question below, I have added the expected output. I have edited this in excel, color coding the group_by for clarity.

enter image description here

Notice how I am getting a break down for each possible answer. When I run this using dplyr here is the output.

> df %>% na.omit() %>% group_by(Data_source) %>% count("symptoms_decLOC")
# A tibble: 2 x 3
# Groups:   Data_source [2]
  Data_source `"symptoms_decLOC"`     n
  <chr>       <chr>               <int>
1 1           symptoms_decLOC         5
2 2           symptoms_decLOC         2
like image 629
Patrick Avatar asked Oct 15 '25 04:10

Patrick


1 Answers

This gets most of the way: haven't figured out how to include zero-count groups yet ... supposedly adding .drop=FALSE takes care of this, but it's not working for me (using dplyr v. 0.8.0.9001).

library(dplyr)
library(tidyr)
(df
    %>% tidyr::gather(var,val,-Data_source)
    %>% count(Data_source,var,val, .drop=FALSE)
    %>% na.omit()
)

Results:

  Data_source var                       val        n
  <chr>       <chr>                     <chr>  <int>
1 1           Sex                       Female     7
2 1           Sex                       Male       1
3 1           symptoms_decLOC           No         1
4 1           symptoms_decLOC           Yes        5
5 1           symptoms_nausea_vomitting No         5
6 2           Sex                       Female     6
7 2           Sex                       Male       6
8 2           symptoms_decLOC           Yes        2
9 2           symptoms_nausea_vomitting No         2
like image 142
Ben Bolker Avatar answered Oct 17 '25 19:10

Ben Bolker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!