This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
# Carregando o dataset
data("mtcars")
cars <- mtcars
head(cars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
# 1) Renomear coluna para facilitar leitura
cars <- cars %>%
tibble::rownames_to_column(var = "model")
# 2) Criar a variável power_to_weight = hp / wt
cars <- cars %>%
mutate(power_to_weight = hp / wt)
# 3) Filtrar carros com mpg >= 20 (economia razoável)
economicos <- cars %>% filter(mpg >= 20)
# 4) Ordenar por power_to_weight decrescente
cars_sorted <- cars %>% arrange(desc(power_to_weight))
# 5) Agrupar por número de cilindros e calcular médias
resumo_cil <- cars %>%
group_by(cyl) %>%
summarise(n = n(),
mpg_mean = mean(mpg),
hp_mean = mean(hp),
wt_mean = mean(wt),
ptw_mean = mean(power_to_weight))
list(economicos = economicos, head_sorted = head(cars_sorted), resumo_cil = resumo_cil)
## $economicos
## model mpg cyl disp hp drat wt qsec vs am gear carb
## 1 Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## 2 Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## 3 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## 4 Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## 5 Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## 6 Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## 7 Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## 8 Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## 9 Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## 10 Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## 11 Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## 12 Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## 13 Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## 14 Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
## power_to_weight
## 1 41.98473
## 2 38.26087
## 3 40.08621
## 4 34.21462
## 5 19.43574
## 6 30.15873
## 7 30.00000
## 8 32.19814
## 9 35.42234
## 10 39.35091
## 11 34.10853
## 12 42.52336
## 13 74.68605
## 14 39.20863
##
## $head_sorted
## model mpg cyl disp hp drat wt qsec vs am gear carb
## 1 Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## 2 Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## 3 Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## 4 Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## 5 Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## 6 Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## power_to_weight
## 1 93.83754
## 2 83.28076
## 3 74.68605
## 4 68.62745
## 5 63.80208
## 6 63.17690
##
## $resumo_cil
## # A tibble: 3 × 6
## cyl n mpg_mean hp_mean wt_mean ptw_mean
## <dbl> <int> <dbl> <dbl> <dbl> <dbl>
## 1 4 11 26.7 82.6 2.29 37.9
## 2 6 7 19.7 122. 3.12 39.9
## 3 8 14 15.1 209. 4.00 53.9
# Mostrar tabela interativa com DT (todas as colunas)
DT::datatable(cars,
options = list(pageLength = 10,
searchHighlight = TRUE,
autoWidth = TRUE),
rownames = FALSE)
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.