Calculating rates in R

Asked

Viewed 120 times

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 individuals are represented by the number 2 and the female by the number 4, in Occupation the number 1 represents the occupied individuals and 2 represents the unemployed.

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

In addition, is there any way to represent graphically (dispersion, lines or bars) such relation by different groups? For example, (Unoccupied)/(Occupied+Unoccupied) only for white males or any other combination accordingly.

1 answer

3


I believe the following does what it wants, except graphs.
First of all, I turned the columns Sexo and Ocupaçao in factors and assigned 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 functions table and prop.table to obtain the relationship Desocupados/(Ocupados+Desocupados).

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

To get relationships by other more complex criteria, such as the occupancy rate per race, we do three operations, aggregate, tapply and the division of these 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

Browser other questions tagged

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