I’ve got following data frame:
T S V
1 s0 A 2.5
2 s1 A 1
3 s2 A 3
4 s0 B 5.6
5 s1 B 7
6 s0 C 8
I would like to turn it into:
s0 s1 s2
A 2.5 1 3
B 5.6 7 0
C 8 0 0
So that it can be used by a chisq.test.
I’ve tried the following, which only takes into account the occurrence but not the values:
table(d$T, d$S)
Try:
You are actually stretching the definition of contingency table a bit but I have not had problems with fractional values as long as there are no repeated levels. If there were you might need to use
tapplywith an appropriate aggregation function, and “correct” or “zero-out” NA’s if you wanted zeros in the missing factor levels.There are quite a few other methods that might work for this “long” to “wide” transformaltion. The plyr package has a more consistent syntax for it’s methods. Look at the
dcastfunction in plyr. There is also thereshapefunction in base-R and search SO for worked examples. Thedata.tablepackage is deserving of a good look if you start needing speed. It’s got a different syntax than plyr and needs bit of mental adjustment, but it has gained a following among “power useRs”.