Reputation: 399
I have a data frame like this one:
v1 v2 wght
1 3 5 0.2
2 2 8 0.5
3 3 5 0.6
................
So I want to produce some descriptive statistics for multiple variables, a cross table more precisely. The values of v1 and v2 are all integers between 1 and 10 and wght is a weight variable. The desired result should be a data frame like this one for example:
v2_1 ... v2_5 ... v2_8 ... v2_10
v1_1
v1_2 0.5
v1_3 0.8=0.2+0.6
...
v1_10
where in each cell you have the number of people who are v2_i amongst those who have v1_j for i and j in [1:10]. These data should be weighted by wght. I tried using Crosstable but the results are impossible to be used afterwards because I need a data frame. Thanks in advance
Upvotes: 1
Views: 832
Reputation: 887901
Try
library(reshape2)
df1[1:2] <- lapply(df1[1:2], function(x) factor(x, levels=1:10))
res1 <- as.data.frame(acast(df1, v1 ~v2 , value.var='wght',
fill=0, drop=FALSE, sum))
Or
as.data.frame.matrix(xtabs(wght~v1+v2, df1))
set.seed(24)
df1 <- data.frame(v1= sample(1:10, 20, replace=TRUE),
v2= sample(1:10, 20, replace=TRUE), wght=rnorm(20))
Upvotes: 3