Calculating rates in R

2

I have the following dataframe:

data <- data.frame(Individuo=c("1","2","3","4","5","6"), 
  Sexo=c(2,4,4,2,4,2),Ocupaçao=c(1,1,2,2,1,2),
  Raça=c("Branco","Negro","Pardo","Branco","Pardo","Branco"), 
  Estudo=c(10,12,14,16,13,11))

Where, in Sex, the male is represented by the number 2 and the female by the number 4, in Occupation the number 1 represents the individuals occupied and 2 represents the unemployed.

How do I calculate the following ratio, (Unemployed) / (Occupied + Unemployed)? (This is the formula for the unemployment rate)

In addition, is there any way to represent such a relationship graphically (Scatter, lines or bars) by different groups? For example, (Unemployed) / (Occupied + Unemployed) only for white men or any other combination in this sense.

    
asked by anonymous 13.03.2018 / 21:03

1 answer

2

I think the following does what it wants, except graphics. First, I've transformed the columns Sexo and Ocupaçao into factors and assign them labels descriptive, Masculino/Feminino and Ocupados/Desocupados .

data$Sexo <- factor(data$Sexo, levels = c(2, 4), labels = c("Masculino", "Feminino"))
data$Ocupaçao <- factor(data$Ocupaçao, levels = 1:2, labels <- c("Ocupado", "Desocupado"))

Now, just use the table and prop.table functions to get the Desocupados/(Ocupados+Desocupados) relation.

tbl_ocup <- table(data$Ocupaçao)
prop.table(tbl_ocup)
#    Ocupado  Desocupado 
#        0.5         0.5

In order to obtain relations by other more complex criteria, such as the occupation rate by race, we do in three operations, aggregate , tapply and the division of these last results by table .

agg <- aggregate(Ocupaçao ~ Raça, data, length)
TaxaOcup <- tapply(data$Ocupaçao, data$Raça, FUN = function(x) sum(x == "Desocupado"))
TaxaOcup <- TaxaOcup/table(data$Raça)
tbl_ocup_raca <- cbind(agg, TaxaOcup)
row.names(tbl_ocup_raca ) <- NULL
tbl_ocup_raca <- tbl_ocup_raca[c(1, 2, 4)]
names(tbl_ocup_raca)[3] <- "TaxaOcup"
tbl_ocup_raca
#    Raça Ocupaçao  TaxaOcup
#1 Branco        3 0.6666667
#2  Negro        1 0.0000000
#3  Pardo        2 0.5000000
    
13.03.2018 / 23:51