greut
greut

Reputation: 4363

Transforming a data frame to wide table with R

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)

Upvotes: 0

Views: 122

Answers (1)

IRTFM
IRTFM

Reputation: 263332

Try:

xtabs(V ~ S + T, data=d)  #

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 tapply with an appropriate aggregation function, and "correct" or "zero-out" NA's if you wanted zeros in the missing factor levels.

> td <- tapply(d$V, list(d$S, d$T), sum)
> td[is.na(td) ] <- 0
> td
   s0 s1 s2
A 2.5  1  3
B 5.6  7  0
C 8.0  0  0

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 dcast function in plyr. There is also the reshape function in base-R and search SO for worked examples. The data.table package 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".

Upvotes: 5

Related Questions