r - Contingency table with name of factors instead of counts -
i have dataframe 3 columns. first has names of categories, second (q2) , third (q3) columns contain other factors. es:
q1 q2 q3 w y g x x f y z
i'm using
t1 <- xtabs(~q2+q3, data=db)
to create contingency table , know how many combo categories there are.
how can obtain in itersection cells names of factors that're in column q1?
x y z w 0 1 0 x 1 0 0 y 0 0 1
in example, instead of 1, want a, g or f.
an option assign q1
t1
using q2
, q3
indices.
> dn <- lapply(db[2:3], levels) > t1 <- array(na, lengths(dn), dn) > t1[cbind(db$q2, db$q3)] <- levels(db$q1)[db$q1] > t1 q3 q2 x y z w na "a" na x "g" na na y na na "f"
Comments
Post a Comment