0

I am doing some exercises to get a grasp on R's tidyverse instruments. I have the following code so far:

library(tidyverse)
starwars$gender[is.na(starwars$gender)] = 'none'

starwars %>% group_by(eye_color) %>% count(gender)

Which gives me:

  eye_color gender     n
   <chr>     <chr>  <int>
 1 black     female     2
 2 black     male       7
 3 black     none       1
 4 blue      female     6
 5 blue      male      13

 ...         ...       ...

But what I would like to get is a count of genders for each eye color in its own feature-column like so:

  eye_color  male   female   none
   <chr>     <int>  <int>    <int>
 1 black     7      2        1
 2 blue      13     6        0

 ...         ...    ...     ...

Are there some aggregation functions that could help or what is a good way in general to go about such tasks?

AegisX303
  • 103
  • 9

0 Answers0