Diretrizes gerais:

  1. Baixe o arquivo e abra no RStudio .

  2. Execute as operações assinaladas nos exercícios a seguir.

  3. Salve o .Rmd e submeta-o por meio desta tarefa.

Dica: As barras que delimitam o endereçamento do arquivo no seu computador, quando exibidas no explorer do Windows, são invertidas (). O R trabalha com barras normais (/) para endereçamento.

Operações matemáticas e vetores

Exercícios:

Questão 1:

Qual é a classe dos vetores abaixo? Imprima o vetor com print e tente advinhar. Use a função class para descobrir a resposta.

pv1 <- c(1, 2, TRUE, 4)
v2 <- c("T", "TRUE", "FALSE", "T")
v3 <- c("1", "2", "3", "4")
v4 <- c(1, "4", 4, 1)
v5 <- c(1, 2, "feijao com arroz")
v6 <- c("Beatriz", "Pedro", TRUE)
v7 <- c(T, T, F, T, F, F, 42)

Você consegue identificar as regras de combinação de tipos de dados diferentes em um mesmo vetor? Se tiver dúvidas, pergunte.

Questão 2:

Veja as cores de veículos comercializados por uma concessionária na semana passada.

colours <- factor(c("red","blue","red","white",
                    "silver","red","white","silver",
                    "red","red","white","silver","silver"),
                  levels=c("red","blue","white","silver","black"))

Se você colocar o código a seguir, o que acontece? Por que? Responda na atividade da semana.

colours[4] <- "orange"
colours
##  [1] red    blue   red    <NA>   silver red    white  silver red    red   
## [11] white  silver silver
## Levels: red blue white silver black

Utilize a função table para determinar a quantidade de automóveis comercializados de cada cor. Pesquise na internet e pergunte caso tenha dúvidas.

table(colours)
## colours
##    red   blue  white silver  black 
##      5      1      2      4      0
d11 <- plyr::count(colours)
d12 <- plyr::count(mtcars, vars = "cyl")
d13 <- gmodels::CrossTable(colours)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  12 
## 
##  
##           |       red |      blue |     white |    silver | 
##           |-----------|-----------|-----------|-----------|
##           |         5 |         1 |         2 |         4 | 
##           |     0.417 |     0.083 |     0.167 |     0.333 | 
##           |-----------|-----------|-----------|-----------|
## 
## 
## 
## 

Questão 3:

Considere:

car.type <- factor(c("saloon","saloon","hatchback",
    "saloon","convertible","hatchback","convertible",
    "saloon", "hatchback","saloon", "saloon",
    "saloon","hatchback"),
    levels=c("saloon","hatchback","convertible"))

Gere a tabela de frequências para os dados car.type.

Experimente:

table(car.type, colours)
##              colours
## car.type      red blue white silver black
##   saloon        2    1     1      2     0
##   hatchback     3    0     0      1     0
##   convertible   0    0     1      1     0
gmodels::CrossTable(car.type, colours)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  12 
## 
##  
##              | colours 
##     car.type |       red |      blue |     white |    silver | Row Total | 
## -------------|-----------|-----------|-----------|-----------|-----------|
##       saloon |         2 |         1 |         1 |         2 |         6 | 
##              |     0.100 |     0.500 |     0.000 |     0.000 |           | 
##              |     0.333 |     0.167 |     0.167 |     0.333 |     0.500 | 
##              |     0.400 |     1.000 |     0.500 |     0.500 |           | 
##              |     0.167 |     0.083 |     0.083 |     0.167 |           | 
## -------------|-----------|-----------|-----------|-----------|-----------|
##    hatchback |         3 |         0 |         0 |         1 |         4 | 
##              |     1.067 |     0.333 |     0.667 |     0.083 |           | 
##              |     0.750 |     0.000 |     0.000 |     0.250 |     0.333 | 
##              |     0.600 |     0.000 |     0.000 |     0.250 |           | 
##              |     0.250 |     0.000 |     0.000 |     0.083 |           | 
## -------------|-----------|-----------|-----------|-----------|-----------|
##  convertible |         0 |         0 |         1 |         1 |         2 | 
##              |     0.833 |     0.167 |     1.333 |     0.167 |           | 
##              |     0.000 |     0.000 |     0.500 |     0.500 |     0.167 | 
##              |     0.000 |     0.000 |     0.500 |     0.250 |           | 
##              |     0.000 |     0.000 |     0.083 |     0.083 |           | 
## -------------|-----------|-----------|-----------|-----------|-----------|
## Column Total |         5 |         1 |         2 |         4 |        12 | 
##              |     0.417 |     0.083 |     0.167 |     0.333 |           | 
## -------------|-----------|-----------|-----------|-----------|-----------|
## 
## 

*Qual a diferença entre o uso da função table com um ou argumentos? Qual a diferença entre table(car.type, colours) e table(colours, car.type)?

Questão 4:

  • Crie dois novos vetores. No primeiro, anote (invente) o número de palavras que você escreveu no último trabalho acadêmico ou relatório técnico em cada semana, considerando os últimos três meses. No segundo, anote (chute, novamente) quantos litros de café você tomou em cada semana.

  • Nomeie os elementos dos 2 vetores.

  • Calcule sua produtividade em “palavras por Litro de café”. Atribua o resultado a um novo vetor.

Show in New Window [1] red blue red silver red white silver red red
[11] white silver silver Levels: red blue white silver black Show in New Window colours red blue white silver black 5 1 2 4 0 Show in New Window

Cell Contents |————————-| | N | | N / Table Total | |————————-|

Total Observations in Table: 12

      |       red |      blue |     white |    silver | 
      |-----------|-----------|-----------|-----------|
      |         5 |         1 |         2 |         4 | 
      |     0.417 |     0.083 |     0.167 |     0.333 | 
      |-----------|-----------|-----------|-----------|

Show in New Window colours car.type red blue white silver black saloon 2 1 1 2 0 hatchback 3 0 0 1 0 convertible 0 0 1 1 0

Cell Contents |————————-| | N | | Chi-square contribution | | N / Row Total | | N / Col Total | | N / Table Total | |————————-|

Total Observations in Table: 12

         | colours 
car.type |       red |      blue |     white |    silver | Row Total | 

————-|———–|———–|———–|———–|———–| saloon | 2 | 1 | 1 | 2 | 6 | | 0.100 | 0.500 | 0.000 | 0.000 | | | 0.333 | 0.167 | 0.167 | 0.333 | 0.500 | | 0.400 | 1.000 | 0.500 | 0.500 | | | 0.167 | 0.083 | 0.083 | 0.167 | | ————-|———–|———–|———–|———–|———–| hatchback | 3 | 0 | 0 | 1 | 4 | | 1.067 | 0.333 | 0.667 | 0.083 | | | 0.750 | 0.000 | 0.000 | 0.250 | 0.333 | | 0.600 | 0.000 | 0.000 | 0.250 | | | 0.250 | 0.000 | 0.000 | 0.083 | | ————-|———–|———–|———–|———–|———–| convertible | 0 | 0 | 1 | 1 | 2 | | 0.833 | 0.167 | 1.333 | 0.167 | | | 0.000 | 0.000 | 0.500 | 0.500 | 0.167 | | 0.000 | 0.000 | 0.500 | 0.250 | | | 0.000 | 0.000 | 0.083 | 0.083 | | ————-|———–|———–|———–|———–|———–| Column Total | 5 | 1 | 2 | 4 | 12 | | 0.417 | 0.083 | 0.167 | 0.333 | | ————-|———–|———–|———–|———–|———–|

R version 4.1.1 (2021-08-10) – “Kick Things” Copyright (C) 2021 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type ‘license()’ or ‘licence()’ for distribution details.

R is a collaborative project with many contributors. Type ‘contributors()’ for more information and ‘citation()’ on how to cite R or R packages in publications.

Type ‘demo()’ for some demos, ‘help()’ for on-line help, or ‘help.start()’ for an HTML browser interface to help. Type ‘q()’ to quit R.

pv1 <- c(1, 2, TRUE, 4) v2 <- c(“T”, “TRUE”, “FALSE”, “T”) v3 <- c(“1”, “2”, “3”, “4”) v4 <- c(1, “4”, 4, 1) v5 <- c(1, 2, “feijao com arroz”) v6 <- c(“Beatriz”, “Pedro”, TRUE) v7 <- c(T, T, F, T, F, F, 42) print(pv1) [1] 1 2 1 4 print(v2) [1] “T” “TRUE” “FALSE” “T”
print(v3) [1] “1” “2” “3” “4” print(v4) [1] “1” “4” “4” “1” print(v5) [1] “1” “2” “feijao com arroz” print(v6) [1] “Beatriz” “Pedro” “TRUE”
printv7 Error: object ‘printv7’ not found print(v7) [1] 1 1 0 1 0 0 42 class(pv1) [1] “numeric” class(v2) [1] “character” class(v3) [1] “character” class(v4) [1] “character” class(v5) [1] “character” class(v6) [1] “character” class(v7) [1] “numeric” colours <- factor(c(“red”,“blue”,“red”,“white”, + “silver”,“red”,“white”,“silver”, + “red”,“red”,“white”,“silver”,“silver”), + levels=c(“red”,“blue”,“white”,“silver”,“black”)) colours[4] <- “orange” colours [1] red blue red silver red white silver red red
[11] white silver silver Levels: red blue white silver black *** O elemento nao e adicionado, pois nao faz parte dos niveis (levels)* Error: unexpected ‘^’ in "" table(colours) colours red blue white silver black 5 1 2 4 0

d11 <- plyr::count(colours) d12 <- plyr::count(mtcars, vars = “cyl”) d13 <- gmodels::CrossTable(colours)

Cell Contents |————————-| | N | | N / Table Total | |————————-|

Total Observations in Table: 12

      |       red |      blue |     white |    silver | 
      |-----------|-----------|-----------|-----------|
      |         5 |         1 |         2 |         4 | 
      |     0.417 |     0.083 |     0.167 |     0.333 | 
      |-----------|-----------|-----------|-----------|

car.type <- factor(c(“saloon”,“saloon”,“hatchback”, + “saloon”,“convertible”,“hatchback”,“convertible”, + “saloon”, “hatchback”,“saloon”, “saloon”, + “saloon”,“hatchback”), + levels=c(“saloon”,“hatchback”,“convertible”)) table(car.type, colours) colours car.type red blue white silver black saloon 2 1 1 2 0 hatchback 3 0 0 1 0 convertible 0 0 1 1 0 gmodels::CrossTable(car.type, colours)

Cell Contents |————————-| | N | | Chi-square contribution | | N / Row Total | | N / Col Total | | N / Table Total | |————————-|

Total Observations in Table: 12

         | colours 
car.type |       red |      blue |     white |    silver | Row Total | 

————-|———–|———–|———–|———–|———–| saloon | 2 | 1 | 1 | 2 | 6 | | 0.100 | 0.500 | 0.000 | 0.000 | | | 0.333 | 0.167 | 0.167 | 0.333 | 0.500 | | 0.400 | 1.000 | 0.500 | 0.500 | | | 0.167 | 0.083 | 0.083 | 0.167 | | ————-|———–|———–|———–|———–|———–| hatchback | 3 | 0 | 0 | 1 | 4 | | 1.067 | 0.333 | 0.667 | 0.083 | | | 0.750 | 0.000 | 0.000 | 0.250 | 0.333 | | 0.600 | 0.000 | 0.000 | 0.250 | | | 0.250 | 0.000 | 0.000 | 0.083 | | ————-|———–|———–|———–|———–|———–| convertible | 0 | 0 | 1 | 1 | 2 | | 0.833 | 0.167 | 1.333 | 0.167 | | | 0.000 | 0.000 | 0.500 | 0.500 | 0.167 | | 0.000 | 0.000 | 0.500 | 0.250 | | | 0.000 | 0.000 | 0.083 | 0.083 | | ————-|———–|———–|———–|———–|———–| Column Total | 5 | 1 | 2 | 4 | 12 | | 0.417 | 0.083 | 0.167 | 0.333 | | ————-|———–|———–|———–|———–|———–|

table(car.type, colours) colours car.type red blue white silver black saloon 2 1 1 2 0 hatchback 3 0 0 1 0 convertible 0 0 1 1 0 gmodels::CrossTable(car.type, colours)

Cell Contents |————————-| | N | | Chi-square contribution | | N / Row Total | | N / Col Total | | N / Table Total | |————————-|

Total Observations in Table: 12

         | colours 
car.type |       red |      blue |     white |    silver | Row Total | 

————-|———–|———–|———–|———–|———–| saloon | 2 | 1 | 1 | 2 | 6 | | 0.100 | 0.500 | 0.000 | 0.000 | | | 0.333 | 0.167 | 0.167 | 0.333 | 0.500 | | 0.400 | 1.000 | 0.500 | 0.500 | | | 0.167 | 0.083 | 0.083 | 0.167 | | ————-|———–|———–|———–|———–|———–| hatchback | 3 | 0 | 0 | 1 | 4 | | 1.067 | 0.333 | 0.667 | 0.083 | | | 0.750 | 0.000 | 0.000 | 0.250 | 0.333 | | 0.600 | 0.000 | 0.000 | 0.250 | | | 0.250 | 0.000 | 0.000 | 0.083 | | ————-|———–|———–|———–|———–|———–| convertible | 0 | 0 | 1 | 1 | 2 | | 0.833 | 0.167 | 1.333 | 0.167 | | | 0.000 | 0.000 | 0.500 | 0.500 | 0.167 | | 0.000 | 0.000 | 0.500 | 0.250 | | | 0.000 | 0.000 | 0.083 | 0.083 | | ————-|———–|———–|———–|———–|———–| Column Total | 5 | 1 | 2 | 4 | 12 | | 0.417 | 0.083 | 0.167 | 0.333 | | ————-|———–|———–|———–|———–|———–|

*Table com 1 argumento gera uma lista, com dois gera uma tabela. A diferenca e que uma e a matriz inversa da outra. Error: unexpected ‘^’ in "" num.palavras <- factor(c(2000,7000,8000,10000,2000,10000,8000,2000,10000,8000,2000,2000)“white”,“silver”, Error: unexpected string constant in “num.palavras <- factor(c(2000,7000,8000,10000,2000,10000,8000,2000,10000,8000,2000,2000)”white"" “red”,“red”,“white”,“silver”,“silver”), Error: unexpected ‘,’ in " “red”," levels=c(“red”,“blue”,“white”,“silver”,“black”)) Error: unexpected ‘)’ in " levels=c(“red”,“blue”,“white”,“silver”,“black”))" table(car.type, colours) colours car.type red blue white silver black saloon 2 1 1 2 0 hatchback 3 0 0 1 0 convertible 0 0 1 1 0

num.palavras <- factor(c(“2000”,“8000”,“7000”,“2000”,“7000”,“10000”,“2000”,“2000”,“7000”,“8000”,“2000”,“7000”), + levels=c(“2000”,“7000”,“8000”,“10000”)) litros.cafe <- factor(c(“1”,“7”,“9”,“10”,“4”,“3”,“2”,“4”,“7”,“9”,“10”,“2”)), Error: unexpected ‘,’ in “litros.cafe <- factor(c(”1“,”7“,”9“,”10“,”4“,”3“,”2“,”4“,”7“,”9“,”10“,”2“)),” litros.cafe <- factor(c(“1”,“7”,“9”,“10”,“4”,“3”,“2”,“4”,“7”,“9”,“10”,“2”)), levels(“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”.“9”.“10”) Error: unexpected ‘,’ in “litros.cafe <- factor(c(”1“,”7“,”9“,”10“,”4“,”3“,”2“,”4“,”7“,”9“,”10“,”2“)),” litros.cafe <- factor(c(“1”,“7”,“9”,“10”,“4”,“3”,“2”,“4”,“7”,“9”,“10”,“2”)), levels=c(“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”.“9”.“10”)) Error: unexpected ‘,’ in “litros.cafe <- factor(c(”1“,”7“,”9“,”10“,”4“,”3“,”2“,”4“,”7“,”9“,”10“,”2“)),” litros.cafe <- factor(c(“1”,“7”,“9”,“10”,“4”,“3”,“2”,“4”,“7”,“9”,“10”,“2”), levels=c(“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”.“9”.“10”)) Error: unexpected symbol in “litros.cafe <- factor(c(”1“,”7“,”9“,”10“,”4“,”3“,”2“,”4“,”7“,”9“,”10“,”2“), levels=c(”1“,”2“,”3“,”4“,”5“,”6“,”7“,”8“.” litros.cafe <- factor(c(“1”,“7”,“9”,“10”,“4”,“3”,“2”,“4”,“7”,“9”,“10”,“2”)), levels=c(“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”.“10”)) Error: unexpected ‘,’ in “litros.cafe <- factor(c(”1“,”7“,”9“,”10“,”4“,”3“,”2“,”4“,”7“,”9“,”10“,”2“)),” > litros.cafe <- factor(c(“1”,“7”,“9”,“10”,“4”,“3”,“2”,“4”,“7”,“9”,“10”,“2”)), levels=c(“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”)) Error: unexpected ‘>’ in “>” litros.cafe <- factor(c(“1”,“7”,“9”,“10”,“4”,“3”,“2”,“4”,“7”,“9”,“10”,“2”)), levels=c(“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”)) Error: unexpected ‘,’ in “litros.cafe <- factor(c(”1“,”7“,”9“,”10“,”4“,”3“,”2“,”4“,”7“,”9“,”10“,”2“)),” litros.cafe <- factor(c(“1”,“7”,“9”,“10”,“4”,“3”,“2”,“4”,“7”,“9”,“10”,“2”), levels=c(“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”)) table(num.palavras,litros.cafe) litros.cafe num.palavras 1 2 3 4 5 6 7 8 9 10 2000 1 1 0 1 0 0 0 0 0 2 7000 0 1 0 1 0 0 1 0 1 0 8000 0 0 0 0 0 0 1 0 1 0 10000 0 0 1 0 0 0 0 0 0 0 gmodels::CrossTable(num.palavras,litros.cafe)

Cell Contents |————————-| | N | | Chi-square contribution | | N / Row Total | | N / Col Total | | N / Table Total | |————————-|

Total Observations in Table: 12

         | litros.cafe 
num.palavras 1 2 3 4 7 9 10 Row Total
2000 1 1 0 1 0 0 2 5
0.817 0.033 0.417 0.033 0.833 0.833 1.633
0.200 0.200 0.000 0.200 0.000 0.000 0.400 0.417
1.000 0.500 0.000 0.500 0.000 0.000 1.000
0.083 0.083 0.000 0.083 0.000 0.000 0.167
————- ———– ———– ———– ———– ———– ———– ———– ———–
7000 0 1 0 1 1 1 0 4
0.333 0.167 0.333 0.167 0.167 0.167 0.667
0.000 0.250 0.000 0.250 0.250 0.250 0.000 0.333
0.000 0.500 0.000 0.500 0.500 0.500 0.000
0.000 0.083 0.000 0.083 0.083 0.083 0.000
————- ———– ———– ———– ———– ———– ———– ———– ———–
8000 0 0 0 0 1 1 0 2
0.167 0.333 0.167 0.333 1.333 1.333 0.333
0.000 0.000 0.000 0.000 0.500 0.500 0.000 0.167
0.000 0.000 0.000 0.000 0.500 0.500 0.000
0.000 0.000 0.000 0.000 0.083 0.083 0.000
————- ———– ———– ———– ———– ———– ———– ———– ———–
10000 0 0 1 0 0 0 0 1
0.083 0.167 10.083 0.167 0.167 0.167 0.167
0.000 0.000 1.000 0.000 0.000 0.000 0.000 0.083
0.000 0.000 1.000 0.000 0.000 0.000 0.000
0.000 0.000 0.083 0.000 0.000 0.000 0.000
————- ———– ———– ———– ———– ———– ———– ———– ———–
Column Total 1 2 1 2 2 2 2 12
0.083 0.167 0.083 0.167 0.167 0.167 0.167
————- ———– ———– ———– ———– ———– ———– ———– ———–