I have a series of tables created using the "tables" package in R, to allow for multiple variables, e.g.
##create sample data frame
x<-runif(1000, 0, 1)
x<-round(x,0)
y<-runif(1000, 0, 1)
y<-round(y,0)
z<-runif(1000, 1, 6)
z<-round(z, 0)
data<-as.data.frame(cbind(x,y,z))
names(data)<-c("Q1_1", "gender", "agegrp")
data$Q1_1<-as.factor(data$Q1_1)
data$weights<-runif(1000, 0, 0.5)
##create table
tabular((Q1_1+1)~((factor(agegrp)+factor(gender))*Percent("row")), data=data)
This works fine (and allows the addition of more variables, which I would need), but I would like to produce the same tables using weighted data. The "survey" package gives me the svytable option, but this is limited to producing crosstables of two variables, rather than crossing 1 or more variables against a series of others. Within tables, I can produce a weighted mean, but can see no way of weighting percentages. One alternative is to create several weighted tables, where I can create a list of variables to cross against a single other and then bind them, but this seems a little inefficient?
Does anybody know of a way to create such a table using the tabular command, or am I better off finding another way to create the table which will suit weighting better?
The default result of Percent
is function(x, y) 100*length(x)/length(y)
, where x
are the values in the current cell, and y
are the values in the reference cells, i.e. the whole row in your sample table.
To get a weighted percentage, you'd want the function to use the sum of weights rather than the length of vectors, as in the discussion in ?Percent
in the package. That's easy, just use
tabular((Q1_1 + 1) ~ ((factor(agegrp) + factor(gender)) *
Percent("row",
fn = function(x, y) 100*sum(x)/sum(y)) *
weights),
data = data)