Introdução

Nesse espaço iremos trabalhar com variáveis qualitativas. Devemos fazer:

  1. Importar arquivos do excel e csv;
  2. Olhar e tratar dados;
  3. Construir tabelas;
  4. Fazer gráficos;
  5. Escrever um parecer.

Importação dos dados

Aqui vamos colocar os dados do excel, csv e RData.

# importar do excel
library(readxl)
Familias <- read_excel("C:/Users/Igor/Desktop/Base_de_dados-master/Familias.xls", 
                       sheet = "Dados da pesquisa")
# importar do csd
library(readr)
UF <- read_delim("C:/Users/Igor/Desktop/Base_de_dados-master/Estados.csv", 
                 delim = ";", escape_double = FALSE, trim_ws = TRUE)
## Rows: 27 Columns: 35
## -- Column specification --------------------------------------------------------
## Delimiter: ";"
## chr (10): S, Sigla, Estado, Regiao_Geografica, CodigoReg, Gini, IDH, IDH_Ren...
## dbl  (3): Codigo, PIB, Populacao
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
# carregar do Rdata
load ("C:/Users/Igor/Desktop/Base_de_dados-master/CARROS.RData")

Inspecionar os dados

Aqui vamos ver se o R classificou as variáveis corretamente.

str (Familias)
## tibble [120 x 6] (S3: tbl_df/tbl/data.frame)
##  $ familia: num [1:120] 1 2 3 4 5 6 7 8 9 10 ...
##  $ local  : chr [1:120] "Monte Verde" "Monte Verde" "Monte Verde" "Monte Verde" ...
##  $ p.a.p  : chr [1:120] "Não usa" "Não usa" "Usa" "Não usa" ...
##  $ instr  : chr [1:120] "Ensino médio" "Ensino médio" "Ensino fundamental" "Ensino fundamental" ...
##  $ tam    : num [1:120] 4 4 4 5 4 1 2 3 6 4 ...
##  $ renda  : num [1:120] 10.3 15.4 9.6 5.5 9 2.4 4.1 8.4 10.3 4.6 ...
str (CARROS)
## 'data.frame':    32 obs. of  11 variables:
##  $ Kmporlitro             : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ Cilindros              : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ Preco                  : num  160 160 108 258 360 ...
##  $ HP                     : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ Amperagem_circ_eletrico: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ Peso                   : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ RPM                    : num  16.5 17 18.6 19.4 17 ...
##  $ Tipodecombustivel      : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ TipodeMarcha           : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ NumdeMarchas           : num  4 4 4 3 3 3 3 4 4 4 ...
##  $ NumdeValvulas          : num  4 4 1 1 2 1 4 2 2 4 ...
##  - attr(*, "variable.labels")= chr [1:11] "Km por litro" "Número de Cilindros" "Preço" "HP = Horse Power (potência do motor)" ...
summary(Familias)
##     familia          local              p.a.p              instr          
##  Min.   :  1.00   Length:120         Length:120         Length:120        
##  1st Qu.: 30.75   Class :character   Class :character   Class :character  
##  Median : 60.50   Mode  :character   Mode  :character   Mode  :character  
##  Mean   : 60.50                                                           
##  3rd Qu.: 90.25                                                           
##  Max.   :120.00                                                           
##                                                                           
##       tam            renda      
##  Min.   :1.000   Min.   : 0.10  
##  1st Qu.:3.750   1st Qu.: 3.90  
##  Median :4.000   Median : 5.40  
##  Mean   :4.492   Mean   : 6.34  
##  3rd Qu.:5.000   3rd Qu.: 8.15  
##  Max.   :9.000   Max.   :25.70  
##                  NA's   :1
summary (CARROS)
##    Kmporlitro      Cilindros         Preco             HP       
##  Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
##  1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5  
##  Median :19.20   Median :6.000   Median :196.3   Median :123.0  
##  Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7  
##  3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
##  Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
##  Amperagem_circ_eletrico      Peso            RPM        Tipodecombustivel
##  Min.   :2.760           Min.   :1.513   Min.   :14.50   Min.   :0.0000   
##  1st Qu.:3.080           1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000   
##  Median :3.695           Median :3.325   Median :17.71   Median :0.0000   
##  Mean   :3.597           Mean   :3.217   Mean   :17.85   Mean   :0.4375   
##  3rd Qu.:3.920           3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000   
##  Max.   :4.930           Max.   :5.424   Max.   :22.90   Max.   :1.0000   
##   TipodeMarcha     NumdeMarchas   NumdeValvulas  
##  Min.   :0.0000   Min.   :3.000   Min.   :1.000  
##  1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000  
##  Median :0.0000   Median :4.000   Median :2.000  
##  Mean   :0.4062   Mean   :3.688   Mean   :2.812  
##  3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :1.0000   Max.   :5.000   Max.   :8.000

Tratamento dos dados

Correção do problema.

CARROS$Tipodecombustivel <- ifelse(CARROS$Tipodecombustivel==0,"Gas","Alc")

CARROS$TipodeMarcha <- ifelse(CARROS$TipodeMarcha==0,"AUTO",
       ifelse(CARROS$TipodeMarcha==1,"manual","Não se aplica"))

Revisão dos dados

summary(CARROS)
##    Kmporlitro      Cilindros         Preco             HP       
##  Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
##  1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5  
##  Median :19.20   Median :6.000   Median :196.3   Median :123.0  
##  Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7  
##  3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
##  Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
##  Amperagem_circ_eletrico      Peso            RPM        Tipodecombustivel 
##  Min.   :2.760           Min.   :1.513   Min.   :14.50   Length:32         
##  1st Qu.:3.080           1st Qu.:2.581   1st Qu.:16.89   Class :character  
##  Median :3.695           Median :3.325   Median :17.71   Mode  :character  
##  Mean   :3.597           Mean   :3.217   Mean   :17.85                     
##  3rd Qu.:3.920           3rd Qu.:3.610   3rd Qu.:18.90                     
##  Max.   :4.930           Max.   :5.424   Max.   :22.90                     
##  TipodeMarcha        NumdeMarchas   NumdeValvulas  
##  Length:32          Min.   :3.000   Min.   :1.000  
##  Class :character   1st Qu.:3.000   1st Qu.:2.000  
##  Mode  :character   Median :4.000   Median :2.000  
##                     Mean   :3.688   Mean   :2.812  
##                     3rd Qu.:4.000   3rd Qu.:4.000  
##                     Max.   :5.000   Max.   :8.000

Tabelas

tabela_comb <- table(CARROS$Tipodecombustivel)
tabela_comb 
## 
## Alc Gas 
##  14  18
tabela_bairro <- table(Familias$local)
tabela_bairro
## 
##   Encosta do Morro        Monte Verde Parque da Figueira 
##                 37                 40                 43
# passo 6 - tabela com a proporção

prop.table(tabela_comb)*100
## 
##   Alc   Gas 
## 43.75 56.25
prop.table(tabela_bairro)*100
## 
##   Encosta do Morro        Monte Verde Parque da Figueira 
##           30.83333           33.33333           35.83333

Gráfico de Pizza

pie(tabela_comb)

pie(tabela_comb,main = "Meu primeiro gráfico no R")

pie(tabela_comb,col = c("pink","green"), 
    main = "Meu primeiro gráfico no R")

pie(tabela_bairro,col = c("#3cbd76","blue","red"),
    main = "Gráfico 2 - Bairros")

Conclusão/Parecer

Atividade do R em aula.