Questão 1

plot(clock, MRT_1F, type = "o", col = "red", pch = 16, lwd = 2,
     xlab = "Clock (GHz)", ylab = "MRT (ms)", main = "Time between Things requests (seconds)")
lines(clock, MRT_3F, type = "o", col = "blue", pch = 17, lwd = 2)
lines(clock, MRT_5F, type = "o", col = "green", pch = 15, lwd = 2)
lines(clock, MRT_10F, type = "o", col = "orange", pch = 18, lwd = 2)
lines(clock, MRT_15F, type = "o", col = "purple", pch = 19, lwd = 2)
lines(clock, MRT_sem_F, type = "o", col = "black", pch = 8, lwd = 2)
legend("topright", legend = c("1F", "3F", "5F", "10F", "15F", "sem F"),
       col = c("red", "blue", "green", "orange", "purple", "black"),
       pch = c(16,17,15,18,19,8), lwd = 2, cex = 0.8)

layout(matrix(1:6, nrow = 2, ncol = 3, byrow = TRUE))
par(mar = c(5, 5, 4, 2) + 0.1)

# Função auxiliar para criar cada gráfico de comparação
plot_comparativo <- function(data, label) {
  bar_data <- rbind(data, MRT_sem_F)
  barplot(bar_data, beside = TRUE,
          col = c("#E6E6E6", "#666666"),
          names.arg = clock, log = "y",
          xlab = "Clock (GHz)", ylab = "MRT (escala log)",
          main = paste("Comparativo:", label, "vs sem F"))
  legend("topright", legend = c(label, "sem F"),
         fill = c("#E6E6E6", "#666666"), cex = 0.8, bty = "n")
}

plot_comparativo(MRT_1F, "1F")
plot_comparativo(MRT_3F, "3F")
plot_comparativo(MRT_5F, "5F")
plot_comparativo(MRT_10F, "10F")
plot_comparativo(MRT_15F, "15F")

Questão 2

dados <- c(53.8, 33.9, 2.6, 0,
           43.6, 54.2, 60.6, 21.4,
           2.6, 11.9, 36.8, 78.6)

qualidade <- matrix(dados, nrow = 3, byrow = TRUE)

rownames(qualidade) <- c("Good", "Very Good", "Excellent")
colnames(qualidade) <- c("$10-19", "$20-29", "$30-39", "$40-49")

qualidade_total <- rbind(qualidade, Total = colSums(qualidade))

knitr::kable(
  qualidade_total,
  caption = "Qualidade da refeição por categoria de preço (%)"
)
Qualidade da refeição por categoria de preço (%)
$10-19 $20-29 $30-39 $40-49
Good 53.8 33.9 2.6 0.0
Very Good 43.6 54.2 60.6 21.4
Excellent 2.6 11.9 36.8 78.6
Total 100.0 100.0 100.0 100.0
par(mar = c(5, 5, 4, 8), xpd = TRUE)
cores <- c("#A8E6CF", "#56C596", "#FFD166")

barplot(qualidade,
        beside = FALSE,
        col = cores,
        main = "Qualidade da Refeição por Faixa de Preço",
        xlab = "Categoria de Preço",
        ylab = "Percentual (%)")

legend("topright",
       inset = c(-0.3, 0),
       legend = rownames(qualidade),
       fill = cores,
       bty = "n",
       title = "Qualidade")

Questão 3

data("airquality")

maio <- subset(airquality, Month == 5)

temp_c <- (maio$Temp - 32) / 1.8

hist(temp_c,
     breaks = 8,            
     col = "#56C596",            
     main = "Distribuição das Temperaturas em Maio (°C)",
     xlab = "Temperatura (°C)",
     ylab = "Frequência",
     probability = TRUE)       

lines(density(temp_c), col = "#333333", lwd = 2)

Questão 4

sales <- read.table("https://training-course-material.com/images/8/8f/Sales.txt", header = TRUE)

total_por_pais <- tapply(sales$SALES, sales$COUNTRY, sum)

porcentagem <- round(100 * total_por_pais / sum(total_por_pais), 1)

rotulos <- paste(names(total_por_pais), "-", porcentagem, "%")
cores <- rainbow(length(total_por_pais))

pie(total_por_pais,
    labels = rotulos,
    col = cores,
    main = "Distribuição Percentual das Vendas por País"
)

legend("topright",
       inset = c(0, 0),
       legend = names(total_por_pais),
       fill = cores,
       title = "Países"
)

Questão 5

data("InsectSprays")

boxplot(count ~ spray,
        data = InsectSprays,
        outline = FALSE, 
        col = "yellow",
        main = "Contagem de Insetos por Tipo de Inseticida",
        xlab = "Tipo de Inseticida",
        ylab = "Número de Insetos")

Questão 6

processar_dados <- function(arquivo) {
  dados <- read.csv(arquivo, stringsAsFactors = FALSE)
  dados$currentTime <- as.POSIXct(dados$currentTime)
  dados$tempo_horas <- as.numeric(difftime(dados$currentTime, min(dados$currentTime), units = "hours"))
  memoria_raw <- dados$usedMemory
  memoria_mb <- numeric(length(memoria_raw))
  for (i in seq_along(memoria_raw)) {
    valor <- as.numeric(gsub("[^0-9\\.]", "", memoria_raw[i]))
    if (grepl("TB", memoria_raw[i], ignore.case = TRUE)) {
      memoria_mb[i] <- valor * 1000000
    } else if (grepl("GB", memoria_raw[i], ignore.case = TRUE)) {
      memoria_mb[i] <- valor * 1024
    } else if (grepl("MB", memoria_raw[i], ignore.case = TRUE)) {
      memoria_mb[i] <- valor
    } else if (grepl("KB", memoria_raw[i], ignore.case = TRUE)) {
      memoria_mb[i] <- valor / 1024
    } else {
      memoria_mb[i] <- valor
    }
  }
  dados$usedMemory_MB <- memoria_mb
  return(dados)
}
dados_05$tempo_horas <- as.numeric(difftime(dados_05$currentTime, min(dados_05$currentTime), units = "hours"))

layout(matrix(1:4, nrow = 2, ncol = 2))

plot(dados_01$tempo_horas, dados_01$usedMemory_MB, type = "l", col = "blue",
     main = "Used Memory - Workload 0.1", xlab = "Tempo (h)", ylab = "Memória usada (MB)")

plot(dados_05$tempo_horas, dados_05$usedMemory_MB, type = "l", col = "red",
     main = "Used Memory - Workload 0.5", xlab = "Tempo (h)", ylab = "Memória usada (MB)")

plot(dados_1$tempo_horas, dados_1$usedMemory_MB, type = "l", col = "darkgreen",
     main = "Used Memory - Workload 1", xlab = "Tempo (h)", ylab = "Memória usada (MB)")

plot(dados_none$tempo_horas, dados_none$usedMemory_MB, type = "l", col = "purple",
     main = "Used Memory - Workload NONE", xlab = "Tempo (h)", ylab = "Memória usada (MB)")

Questão 7

conteudos_filtrados <- conteudos %>%
  filter(!is.na(country) & country != "") %>%
  mutate(country = trimws(country)) %>%
  filter(!grepl(",", country))

top10_paises <- conteudos_filtrados %>%
  group_by(country) %>%
  summarise(total = n()) %>%
  arrange(desc(total)) %>%
  head(10)

plot_ly(top10_paises, labels = ~country, values = ~total, type = "pie") %>%
  layout(title = "Top 10 Países com Mais Conteúdos na Netflix",
         legend = list(title = list(text = "Países")))

Questão 8

conteudos_filtrados <- conteudos %>%
  filter(!is.na(country) & country != "") %>%
  mutate(country = trimws(country)) %>%
  filter(!grepl(",", country))

top10_paises <- conteudos_filtrados %>%
  group_by(country) %>%
  summarise(total = n()) %>%
  arrange(desc(total)) %>%
  head(10)

plot_ly(
  type = 'table',
  header = list(
    values = c("País", "Total de conteúdos"),
    fill = list(color = "#666666"),
    font = list(color = "white", size = 14),
    align = "center"
  ),
  cells = list(
    values = list(top10_paises$country, top10_paises$total),
    align = "center"
  )
)

Questão 9

df <- conteudos %>%
  filter(!is.na(release_year)) %>%
  mutate(decada_num = floor(release_year / 10) * 10,
         decada = paste0(decada_num, "s"))

decadas_seq <- seq(min(df$decada_num, na.rm = TRUE), max(df$decada_num, na.rm = TRUE), by = 10)
decadas_labels <- paste0(decadas_seq, "s")

types <- c("TV Show", "Movie")

conteudos_decadas <- df %>%
  group_by(decada_num, decada, type) %>%
  summarise(total = n(), .groups = "drop") %>%
  ungroup() %>%
  complete(decada_num = decadas_seq, type = types, 
           fill = list(total = 0)) %>%
  mutate(decada = paste0(decada_num, "s")) %>%
  arrange(decada_num)

conteudos_decadas$decada <- factor(conteudos_decadas$decada, levels = decadas_labels)

tv_df <- conteudos_decadas %>% filter(type == "TV Show")
movie_df <- conteudos_decadas %>% filter(type == "Movie")

plot_ly() %>%
  add_trace(x = ~tv_df$decada, y = ~tv_df$total, type = 'scatter', mode = 'lines+markers',
            name = "Series", line = list(color = "blue", width = 2), marker = list(size = 6)) %>%
  add_trace(x = ~movie_df$decada, y = ~movie_df$total, type = 'scatter', mode = 'lines+markers',
            name = "Movies", line = list(color = "yellow", width = 2), marker = list(size = 6)) %>%
  layout(title = "Quantidade de Conteúdos por Década na Netflix",
         xaxis = list(title = "Década"),
         yaxis = list(title = "Quantidade de Conteúdo"),
         legend = list(title = list(text = "Tipo de Conteúdo")),
         hovermode = "x unified")

Questão 10

df_filmes <- conteudos %>%
  filter(type == "Movie" & release_year >= 2000 & release_year <= 2010) %>%
  mutate(genero_principal = trimws(sub(",.*", "", listed_in))) %>%
  filter(genero_principal %in% c("Dramas", "Action & Adventure", "Comedies")) %>%
  group_by(release_year, genero_principal) %>%
  summarise(total = n(), .groups = "drop")

plot_ly(df_filmes, x = ~release_year, y = ~total, color = ~genero_principal,
        colors = c("blue", "red", "orange"), type = 'bar') %>%
  layout(
    barmode = "group",
    title = "Filmes por Gênero (2000–2010)",
    xaxis = list(title = "Ano de Lançamento"),
    yaxis = list(title = "Quantidade de Filmes"),
    legend = list(title = list(text = "Gênero"))
  )