How to perform a cross-tabulation and weighted?

Asked

Viewed 886 times

3

I would like to do a cross-tabulation but with the prop.table I cannot use the weight variable.

I use the command below:

prop.table(table(mydata$Q_15,mydata$Q_35),1)

2 answers

2

I got what I wanted as follows:

I used the crosstab and with the weight:

tabela <- crosstab(mydata$Q_15, mydata$Q_35, weight = mydata$PESO)

I selected only absolute values

tabela <- tabela$t

So, as an additional detail, I calculated the percentage per line and rounded:

(tabela <- 100*round(prop.table(tabela, 1), 2))

I would like another alternative.

2

It is possible to obtain through the xtabs:

dados <- data.frame(minusculas = sample(letters[1:5], 100, rep = TRUE),
MAIUSCULAS =  sample(LETTERS[1:5], 100, rep = TRUE),
peso <- rchisq(100, 10),
lucro <- rnorm(100, 0, 10))

with(dados, xtabs(peso ~ minusculas + MAIUSCULAS))
with(dados, xtabs(lucro ~ minusculas + MAIUSCULAS))
with(dados, xtabs(cbind(peso, lucro) ~ minusculas + MAIUSCULAS))

The xtabs sum the variables to the left of the formula by the levels of the variables to the right, allowing negative values (as is the case of the variable profit).

Browser other questions tagged

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