2013-09-03 12 views
10

ho bisogno di calcolare la frequenza degli individui per età e stato civile così normalmente userei:tabelle di frequenza con i dati ponderati in R

table(age, marital_status) 

Tuttavia ogni individuo ha un peso diverso, dopo il campionamento dei dati . Come inserisco questo nella mia tabella delle frequenze?

risposta

10

È possibile utilizzare la funzione svytable dal pacchetto survey o wtd.table da rgrs.

EDIT:rgrs ora si chiama questionr:

df <- data.frame(var = c("A", "A", "B", "B"), wt = c(30, 10, 20, 40)) 

library(questionr) 
wtd.table(x = df$var, weights = df$wt) 
# A B 
# 40 60 

Questo è possibile anche con dplyr:

library(dplyr) 
count(x = df, var, wt = wt) 
# # A tibble: 2 x 2 
#  var  n 
#  <fctr> <dbl> 
# 1  A 40 
# 2  B 60 
+0

eccellente, l'opzione wtd.table mi si addice perfettamente. Grazie. – user2568648