Lista de Exercícios

Questão 1

clock <- c(0.1, 0.5, 1, 1.5, 2, 2.5, 3)



MRT_1F <- c(517.1468515630205, 85.13094142168089, 30.333207896694553, 

            12.694776264558937, 3.3041601673945418, 1.1823111717498882, 1.1892293502386786)



MRT_3F <- c(156.68929936163462, 11.540837783562276, 0.4512835621696538, 

            0.4509797929766453, 0.4502068233039181, 0.4496185276300172, 0.4543157082191288)



MRT_5F <- c(83.90319666471157, 0.3068151086494968, 0.30522314133037304, 

            0.3072588968084928, 0.30655265997285697, 0.3055812715727718, 0.3053297166713006)



MRT_10F <- c(29.55430642951759, 0.19832832665772515, 0.1971923924717474, 

             0.19796648905716516, 0.19615594370806338, 0.2034569237883263, 0.19617420889447737)



MRT_15F <- c(11.317736530583566, 0.167364215666193, 0.16172168266811013, 

             0.16701085329580515, 0.1598052657153692, 0.1645934043532696, 0.16216563797118075)



MRT_sem_F <- c(11.93430909937736, 0.6095414637034009, 0.6060645101029295, 

               0.612167181646899, 0.6146761002685637, 0.6096747087200697, 0.6125810476877268)



matriz_layout <- matrix(c(1, 1,

                          2, 3,

                          4, 5,

                          6, 0), nrow = 4, byrow = TRUE)

layout(matriz_layout)



par(mar = c(4, 4, 2, 1)) 



plot(clock, MRT_1F, type = "o", pch = 4, col = "black", ylim = c(0, 550),

     xlab = "Time between Things requests (seconds)", ylab = "Response Time (sec.)")

lines(clock, MRT_3F, type = "o", pch = 7, col = "gold")

lines(clock, MRT_5F, type = "o", pch = 1, col = "red")

lines(clock, MRT_10F, type = "o", pch = 2, col = "blue")

lines(clock, MRT_15F, type = "o", pch = 5, col = "purple")

lines(clock, MRT_sem_F, type = "o", pch = 8, col = "green")



legend("topright", legend = c("1 Fog", "3 Fogs", "5 Fogs", "10 Fogs", "15 Fogs", "w/o Fog"),

       col = c("black", "gold", "red", "blue", "purple", "green"),

       pch = c(4, 7, 1, 2, 5, 8), bty = "o", cex = 0.8)



cores_barras <- c("#E6E6E6", "#666666")



barplot(rbind(MRT_sem_F, MRT_1F), beside = TRUE, names.arg = clock, col = cores_barras,

        log = "y", xlab = "Time between Things requests", ylab = "Response time (s)")

legend("topright", legend = c("w/o Fog", "1 Fog"), fill = cores_barras, cex = 0.8)



barplot(rbind(MRT_sem_F, MRT_3F), beside = TRUE, names.arg = clock, col = cores_barras,

        log = "y", xlab = "Time between Things requests", ylab = "Response time (s)")

legend("topright", legend = c("w/o Fog", "3 Fogs"), fill = cores_barras, cex = 0.8)



barplot(rbind(MRT_sem_F, MRT_5F), beside = TRUE, names.arg = clock, col = cores_barras,

        log = "y", xlab = "Time between Things requests", ylab = "Response time (s)")

legend("topright", legend = c("w/o Fog", "5 Fogs"), fill = cores_barras, cex = 0.8)



barplot(rbind(MRT_sem_F, MRT_10F), beside = TRUE, names.arg = clock, col = cores_barras,

        log = "y", xlab = "Time between Thin requests", ylab = "Response time (s)")

legend("topright", legend = c("w/o Fog", "10 Fogs"), fill = cores_barras, cex = 0.8)



barplot(rbind(MRT_sem_F, MRT_15F), beside = TRUE, names.arg = clock, col = cores_barras,

        log = "y", xlab = "Time between Things requests", ylab = "Response time (s)")

legend("topright", legend = c("w/o Fog", "15 Fogs"), fill = cores_barras, cex = 0.8)

Questão 2

dados_refeicao <- matrix(c(53.8, 43.6,  2.6,

                           33.9, 54.2, 11.9,

                           2.6, 60.5, 36.8,

                           0.0, 21.4, 78.6), 

                         nrow = 3, byrow = FALSE)



colnames(dados_refeicao) <- c("$10-19", "$20-29", "$30-39", "$40-49")

rownames(dados_refeicao) <- c("Good", "Very Good", "Excellent")



cores <- c("lightblue", "steelblue", "darkblue")



barplot(dados_refeicao, 

        main = "Qualidade da Refeição por Categoria de Preço",

        xlab = "Preço da Refeição (Meal Price)", 

        ylab = "Porcentagem (%)",

        col = cores)



legend("topleft", legend = rownames(dados_refeicao), fill = cores, cex = 0.8)

Questão 3

data("airquality")



temp_maio_f <- airquality$Temp[airquality$Month == 5]

temp_maio_c <- (temp_maio_f - 32) / 1.8



hist(temp_maio_c, 

     probability = TRUE, 

     main = "Histograma de Temperaturas - Maio",

     xlab = "Temperatura (°C)", 

     ylab = "Densidade",

     col = "lightgreen")



lines(density(temp_maio_c), col = "red", lwd = 2)

Questão 4

url_sales <- "https://www.dropbox.com/scl/fi/mpjt4uuqb5vtmfiw38phk/Sales.txt?rlkey=93jlz79xwrrt7ah66d5k403d7&dl=1"

sales <- read.table(url_sales, header = TRUE)



vendas_por_pais <- aggregate(SALES ~ COUNTRY, data = sales, sum)

porcentagens <- round((vendas_por_pais$Sales / sum(vendas_por_pais$Sales)) * 100, 1)

rotulos_fatias <- paste(porcentagens, "%", sep = "")

cores_pizza <- rainbow(nrow(vendas_por_pais))



pie(vendas_por_pais$SALES, 

    labels = rotulos_fatias, 

    col = cores_pizza, 

    main = "Porcentagem de Vendas por País")



legend("topright", 

       legend = vendas_por_pais$COUNTRY, 

       fill = cores_pizza, 

       cex = 0.8,

       title = "Países")

Questão 5

data("InsectSprays")



boxplot(count ~ spray, data = InsectSprays,

        outline = FALSE,            

        col = "yellow",             

        main = "Efetividade de Diferentes Inseticidas",

        xlab = "Tipo de Inseticida",

        ylab = "Contagem de Insetos")

Questão 6

preparar_dados <- function(nome_arquivo) {

  df <- read.csv(nome_arquivo, stringsAsFactors = FALSE)

  df$currentTime <- as.POSIXct(df$currentTime)

  

  tempo_inicial <- df$currentTime[1]

  df$Time_hour <- as.numeric(difftime(df$currentTime, tempo_inicial, units = "hours"))

  

  converter_para_mb <- function(memoria_str) {

    valor <- as.numeric(gsub("[^0-9\\.]", "", memoria_str))

    if (grepl("TB|tb|Tb", memoria_str)) {

      valor <- valor * 1000000

    } else if (grepl("GB|gb|Gb", memoria_str)) {

      valor <- valor * 1024

    }

    return(valor)

  }

  

  df$usedMemory_MB <- sapply(df$usedMemory, converter_para_mb)

  return(df)

}




df_none <- preparar_dados("monitoringCloudData_NONE.csv")

df_01   <- preparar_dados("monitoringCloudData_0.1.csv")

df_05   <- preparar_dados("monitoringCloudData_0.5.csv")

df_10   <- preparar_dados("monitoringCloudData_1.csv")



matriz_layout <- matrix(c(1, 2, 

                          3, 4), nrow = 2, byrow = TRUE)

layout(matriz_layout)



plot(df_none$Time_hour, df_none$usedMemory_MB, type = "l",

     main = "Memory Analysis (None Workload)",

     xlab = "Time (hour)", ylab = "Used Memory (MB)")



plot(df_01$Time_hour, df_01$usedMemory_MB, type = "l",

     main = "Memory Analysis (Workload of 0.1)",

     xlab = "Time (hour)", ylab = "Used Memory (MB)")



plot(df_05$Time_hour, df_05$usedMemory_MB, type = "l",

     main = "Memory Analysis (Workload of 0.5)",

     xlab = "Time (hour)", ylab = "Used Memory (MB)")



plot(df_10$Time_hour, df_10$usedMemory_MB, type = "l",

     main = "Memory Analysis (Workload of 1.0)",

     xlab = "Time (hour)", ylab = "Used Memory (MB)")

Questão 7

library(plotly)





netflix <- read.csv("netflix_titles.csv", stringsAsFactors = FALSE)



df_q7 <- subset(netflix, !is.na(country) & country != "" & !grepl(",", country))



contagem_paises <- as.data.frame(table(df_q7$country))

colnames(contagem_paises) <- c("country", "count")



top10_paises <- contagem_paises[order(-contagem_paises$count), ][1:10, ]



fig7 <- plot_ly(data = top10_paises, 

                labels = ~country, 

                values = ~count, 

                type = 'pie',

                textinfo = 'label+percent') %>%

  layout(title = "Top 10 Países com Mais Conteúdo na Netflix")



fig7

Questão 8

fig8 <- plot_ly(

  type = 'table',

  header = list(

    values = c('<b>País</b>', '<b>Total de conteúdos</b>'), 

    align = c('center', 'center'),                          

    line = list(width = 1, color = 'black'),

    fill = list(color = 'grey'),                            

    font = list(family = "Arial", size = 14, color = "white") 

  ),

  cells = list(

    values = rbind(top10_paises$country, top10_paises$count), 

    align = c('center', 'center'),                            

    line = list(color = "black", width = 1),

    font = list(family = "Arial", size = 12, color = "black")

  )

)



fig8

Questão 9

netflix$decada <- floor(netflix$release_year / 10) * 10



contagem_decada <- as.data.frame(table(netflix$decada, netflix$type))

colnames(contagem_decada) <- c("Decada", "Tipo", "Quantidade")



contagem_decada$Decada <- as.numeric(as.character(contagem_decada$Decada))



df_series <- subset(contagem_decada, Tipo == "TV Show")

df_movies <- subset(contagem_decada, Tipo == "Movie")



fig9 <- plot_ly() %>%

  add_trace(data = df_series, x = ~Decada, y = ~Quantidade, 

            type = 'scatter', mode = 'lines+markers',

            name = 'TV Series', 

            line = list(color = 'blue'), marker = list(color = 'blue')) %>%

  add_trace(data = df_movies, x = ~Decada, y = ~Quantidade, 

            type = 'scatter', mode = 'lines+markers',

            name = 'Movies', 

            line = list(color = 'yellow'), marker = list(color = 'yellow')) %>%

  layout(title = "Quantidade de Conteúdo por Década",

         xaxis = list(title = "Década"),

         yaxis = list(title = "Qtd. Conteúdo"))



fig9

Questão 10

filmes_2000_2010 <- subset(netflix, type == "Movie" & release_year >= 2000 & release_year <= 2010)



filmes_2000_2010$primeiro_genero <- trimws(gsub(",.*", "", filmes_2000_2010$listed_in))



generos_desejados <- c("Dramas", "Action & Adventure", "Comedies")

df_filtrado <- subset(filmes_2000_2010, primeiro_genero %in% generos_desejados)



contagem <- as.data.frame(table(df_filtrado$release_year, df_filtrado$primeiro_genero))

colnames(contagem) <- c("Ano", "Genero", "Quantidade")



contagem$Ano <- as.numeric(as.character(contagem$Ano))



df_dramas <- subset(contagem, Genero == "Dramas")

df_action <- subset(contagem, Genero == "Action & Adventure")

df_comedies <- subset(contagem, Genero == "Comedies")



fig10 <- plot_ly() %>%

  add_trace(data = df_dramas, x = ~Ano, y = ~Quantidade, type = 'bar', name = 'Drama') %>%

  add_trace(data = df_action, x = ~Ano, y = ~Quantidade, type = 'bar', name = 'Ação e Aventura') %>%

  add_trace(data = df_comedies, x = ~Ano, y = ~Quantidade, type = 'bar', name = 'Comédia') %>%

  layout(

    title = "Quantidade de Filmes Lançados por Gênero (2000 - 2010)",

    xaxis = list(title = "Ano de Lançamento", tickmode = "linear"), 

    yaxis = list(title = "Qnt. de Lançamentos"),

    barmode = 'group' 

  )



fig10