How do I create a list to save countless Numbers generated by my function in R?

Asked

Viewed 60 times

3

Good evening dear friends!

I created a script that generates several age pyramids simultaneously. In this case, the great advantage is to generate hundreds, or even thousands of pyramids in a few minutes. I am in need of help to index the generated plots (pyramids) to be able to access them later. The ideal would be to index by the name of the locality.

Anyway, I will share my script that makes 646 pyramids for all the cities of the State of SP.

Well, if you run this code with the database I used you will see that it works and generates all pyramids, however, as I said at the beginning I need some hint to be able to index this much Plot generated.... Thank you.

library(plotrix)
library(GetoptLong)
library(tidyverse)


#Importando base de dados com valores absolutos estimados para 2020 
#produzidos pela Funda??o SEADE 

p_etarias_raw <-
  read.csv2(
    "C:\\Users\\User\\Desktop\\projetos\\piramides_etarias\\p_etarias_estado_sp.csv",
    encoding = "windows-1250",
    sep = ";",
    dec = ",",
    header = TRUE)

view(p_etarias_raw)


p_etarias <- p_etarias_raw[,2:647]



dataset_length <- length(names(p_etarias))

for (a in 2:dataset_length) {
  p_etarias[2,a] <- (p_etarias[2,a]/sum(p_etarias[1,a]) * 100)
  p_etarias[3,a] <- (p_etarias[3,a]/sum(p_etarias[1,a]) * 100)
  p_etarias[4,a] <- (p_etarias[4,a]/sum(p_etarias[1,a]) * 100)
  p_etarias[5,a] <- (p_etarias[5,a]/sum(p_etarias[1,a]) * 100)
  p_etarias[6,a] <- (p_etarias[6,a]/sum(p_etarias[1,a]) * 100)
  p_etarias[7,a] <- (p_etarias[7,a]/sum(p_etarias[1,a]) * 100)
  p_etarias[8,a] <- (p_etarias[8,a]/sum(p_etarias[1,a]) * 100)
  p_etarias[9,a] <- (p_etarias[9,a]/sum(p_etarias[1,a]) * 100)
  p_etarias[10,a] <- (p_etarias[10,a]/sum(p_etarias[1,a]) * 100)
  p_etarias[11,a] <- (p_etarias[11,a]/sum(p_etarias[1,a]) * 100)
  p_etarias[12,a] <- (p_etarias[12,a]/sum(p_etarias[1,a]) * 100)
  p_etarias[13,a] <- (p_etarias[13,a]/sum(p_etarias[1,a]) * 100)
  p_etarias[14,a] <- (p_etarias[14,a]/sum(p_etarias[1,a]) * 100)
  p_etarias[15,a] <- (p_etarias[15,a]/sum(p_etarias[1,a]) * 100)
  p_etarias[16,a] <- (p_etarias[17,a]/sum(p_etarias[1,a]) * 100)
  p_etarias[17,a] <- (p_etarias[17,a]/sum(p_etarias[1,a]) * 100)
  p_etarias[19,a] <- (p_etarias[19,a]/sum(p_etarias[18,a]) * 100)
  p_etarias[20,a] <- (p_etarias[20,a]/sum(p_etarias[18,a]) * 100)
  p_etarias[21,a] <- (p_etarias[21,a]/sum(p_etarias[18,a]) * 100)
  p_etarias[22,a] <- (p_etarias[22,a]/sum(p_etarias[18,a]) * 100)
  p_etarias[23,a] <- (p_etarias[23,a]/sum(p_etarias[18,a]) * 100)
  p_etarias[24,a] <- (p_etarias[24,a]/sum(p_etarias[18,a]) * 100)
  p_etarias[25,a] <- (p_etarias[25,a]/sum(p_etarias[18,a]) * 100)
  p_etarias[26,a] <- (p_etarias[26,a]/sum(p_etarias[18,a]) * 100)
  p_etarias[27,a] <- (p_etarias[27,a]/sum(p_etarias[18,a]) * 100)
  p_etarias[28,a] <- (p_etarias[28,a]/sum(p_etarias[18,a]) * 100)
  p_etarias[29,a] <- (p_etarias[29,a]/sum(p_etarias[18,a]) * 100)
  p_etarias[30,a] <- (p_etarias[30,a]/sum(p_etarias[18,a]) * 100)
  p_etarias[31,a] <- (p_etarias[31,a]/sum(p_etarias[18,a]) * 100)
  p_etarias[32,a] <- (p_etarias[32,a]/sum(p_etarias[18,a]) * 100)
  p_etarias[33,a] <- (p_etarias[33,a]/sum(p_etarias[18,a]) * 100)
  p_etarias[34,a] <- (p_etarias[34,a]/sum(p_etarias[18,a]) * 100)
}


agelabels <- c(
  "0-4",
  "5-9",
  "10-14",
  "15-19",
  "20-24",
  "25-29",
  "30-34",
  "35-39",
  "40-44",
  "45-49",
  "50-54",
  "55-59",
  "60-64",
  "65-69",
  "70-74",
  "75+"
)

#Definindo as cores das barras
mcol <- color.id('#5882FA')
fcol <- color.id('#FE2E2E')



for (i in 2:dataset_length) {
  
  xy.pop <- c(p_etarias[2:17, i])
  xx.pop <- c(p_etarias[19:34, i])
  
  municipality_name = names(p_etarias[i])
  
  piramide1 <- par(
    mar = pyramid.plot(
      xy.pop,
      xx.pop,
      labels = agelabels,
      main = qq("Pirâmide Etária @{municipality_name}"),
      lxcol = mcol,
      rxcol = fcol,
      gap = 1.3,
      show.values = FALSE,
      top.labels = c("Masc", "Idade", "Fem"),
      ndig = 3,
    )
  )
  
}```

  • 1

    Lucca, good night! Can you make the file available: p_etarias_estado_sp.csv? Hug

1 answer

5


I’m going to use a generic example, so the answer is useful to as many users as possible. In case, generate a list with the Plots box of each column of a data frame..

# Dados de exemplo
dados <- as.data.frame(matrix(rnorm(100), 10, 10))
names(dados) <- LETTERS[1:10]

To store graphics in objects, there is the function recordPlot. It works for graphics generated via graphics and grid (then does not serve, for example, for graphics generated by ggplot2).

Create an empty list to receive the charts and use a loop to update:

plot.list <- vector("list", ncol(dados))
names(plot.list) <- names(dados)
# Poderia usar simplesmente `plot.list <- NULL', mas é uma boa prática já criar o objeto com o tamanho final.

for (id in names(dados)) {
  plot.new()
  boxplot(dados[[id]], main = paste("Boxplot da coluna", id))
  plot.list[id] <- list(recordPlot())
}

Or, to avoid the use of loop (a good practice on R), put the code that generates the graph into a function and use lapply:

plot.bp <- function(j) {
  plot.new()
  boxplot(dados[[j]], main = paste("Boxplot da coluna", names(dados)[j]))
  recordPlot()
}

plot.list <- lapply(seq(ncol(dados)), plot.bp)
names(plot.list) <- names(dados)

To display, for example, the Plot of variable B:

plot.list["B"]
# ou
plot.list$B

Browser other questions tagged

You are not signed in. Login or sign up in order to post.