Introdução ao estudo de caso

Dados Categóricos no Tidyverse

Emily Robinson

Data Scientist

Um gráfico de barras intitulado "o inferno são os outros em um tubo de metal pressurizado" com a legenda "percentual de 874 passageiros de avião que disseram que a ação é muito ou um pouco rude." A maior barra é "levar crianças indisciplinadas de propósito" com 82% e a menor é "pedir para trocar de assento pela família" com 17%. Há 9 barras no total. Esta pesquisa é do FiveThirtyEight e foi feita no SurveyMonkey.

Dados Categóricos no Tidyverse

Conjunto de dados original

# A tibble: 1,040 x 27
  RespondentID travel_amount   do_recline   
         <dbl> <chr>           <chr>        
1  3436139758. Once a year or… NA           
2  3434278696. Once a year or… About half t…
3  3434275578. Once a year or… Usually      
4  3434268208. Once a year or… Always       
# ... with 24 more variables: height <chr>,
#   children_sub_18 <chr>,
#   middle_arm_rest_three <chr>,
#   middle_arm_rest_two <chr>,
#   window_shade_control <chr>,
#   rude_move_seats <chr>, rude_talk <chr>,
#   times_get_up <chr>,
#   recliner_obligation <chr>,
#   rude_recline <chr>,
#   eliminate_recline <chr>,
#   rude_switch_seats_friend <chr>,
Dados Categóricos no Tidyverse

Revisão de ferramentas

wide_data
# A tibble: 2 x 3
  favorite_fruit favorite_vegetable disliked_dessert
  <chr>          <chr>              <chr>           
1 apple          carrot             cookie          
2 orange         cauliflower        cake            
wide_data %>%
   mutate(across(where(is.character), as.factor))
# A tibble: 2 x 3
  favorite_fruit favorite_vegetable disliked_dessert
  <fct>          <fct>              <fct>           
1 apple          carrot             cookie          
2 orange         cauliflower        cake
Dados Categóricos no Tidyverse

tidyr pivot_longer()

wide_data %>%
   pivot_longer(everything(), names_to = "column", values_to = "value")
# A tibble: 6 x 2
  column             value      
  <chr>              <chr>      
1 favorite_fruit     apple      
2 favorite_fruit     orange     
3 favorite_vegetable carrot     
4 favorite_vegetable cauliflower
5 disliked_dessert   cookie     
6 disliked_dessert   cake       
Dados Categóricos no Tidyverse

Funções auxiliares do select

wide_data %>%
   select(contains("favorite"))
# A tibble: 2 x 2
  favorite_fruit favorite_vegetable
  <chr>          <chr>             
1 apple          carrot            
2 orange         cauliflower       
Dados Categóricos no Tidyverse

Vamos praticar!

Dados Categóricos no Tidyverse

Preparing Video For Download...