How to make a cross-weighted tabulation?

3

I would like to cross-tab but with prop.table I can not use the weight variable.

I use the command below:

prop.table(table(mydata$Q_15,mydata$Q_35),1)
    
asked by anonymous 18.09.2014 / 20:03

2 answers

2

I got what I wanted as follows:

I used crosstab and weight:

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

I just selected absolute values

tabela <- tabela$t

Then, 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.

    
18.09.2014 / 20:21
2

Can be obtained through 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 sums the variables on the left of the formula by the levels of the variables on the right, allowing negative values (as is the case of the profit variable).

    
19.09.2014 / 15:38