R - Graphs with information from different columns

Asked

Viewed 75 times

0

Hello,

I have the following problem: I need to create a table and a chart with information on educational level for different positions of the firm for different years and regions.

I have 4 levels of schooling (fundinc, medioinc, superiorinc and supdout) and 3 levels of positions (support, operational and strategic).

Each level of education and each position is one column (if fundinc == 1, the remaining ones are 0, if support == 1, operational and strategic are 0).

Example:

fundinc | medioinc | superiorinc | supdout | apoio | operacional | estrategico
1       | 0        | 0           | 0       | 1     | 0           | 0
0       | 1        | 0           | 0       | 0     | 1           | 0
0       | 0        | 1           | 0       | 0     | 0           | 1
0       | 0        | 1           | 0       | 0     | 0           | 1
0       | 1        | 0           | 0       | 1     | 0           | 0
1       | 0        | 0           | 0       | 1     | 0           | 0
.
. 
.

The databases are separated by year and region (data2010northeast, data2010north, ..., data2016southeast, data2016south).

** Any suggestions how to do that? I’m completely lost.

*** I want to see what the level of education of each position, for example how many "fundinc", "medioinc", etc., are in the post "support" and others. I don’t know which chart can present this best, maybe a common barplot.

I tried to create a function

pegaescolaridadeapoio = function (base) {

  #Fundamental incompleto

  a <- base[base$fundinc==1 & base$apoio==1, ]

  #Medio incompleto

  b <- base[base$medioinc==1 & base$apoio==1,]

  #Superior incompleto

  c <- base[base$superiorinc==1 & base$apoio==1,]

  #superior e outros

  d <- base[base$supdout==1 & base$apoio==1,]

  vetor <- c(nrow(a),nrow(b),nrow(c),nrow(d))

  return (vetor)
}

After that, I tried to create some vectors and put them in tables, but without success.

Grateful from now on.

  • 3

    Welcome to Stackoverflow Brasil! Unfortunately, this question cannot be reproduced by anyone trying to answer it. Please, take a look at this link and see how to ask a reproducible question in R. So, people who wish to help you will be able to do this in the best possible way.

  • @Marcusnunes I tried to edit to make it more understandable. Unfortunately I’m beginner and I don’t think I did anything more useful than this :(

  • 2

    The best way to share data from R is through the command dput. This link shows how to do this. Sharing a piece of data this way is the best way to ensure that other site users will help you.

1 answer

2


First you will need to tidy up your data set so that you have only 2 columns:

base2 <- read.csv2("base2.csv") # Base de dados do seu exemplo
nova.base <- data.frame(Escolaridade = names(base2[1:4])[max.col(base2[1:4])],
                        Cargo = names(base2[5:7])[max.col(base2[5:7])])

Simple table:

with(nova.base, table(Escolaridade, Cargo))
             Cargo
Escolaridade  apoio estrategico operacional
  fundinc         2           0           0
  medioinc        1           0           1
  superiorinc     0           2           0

Single chart:

library(ggplot2)
ggplot(nova.base, aes(x = Escolaridade, fill = Cargo)) +
  geom_bar()

inserir a descrição da imagem aqui

Browser other questions tagged

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