[R] Reshape Dataframe

hadley wickham h.wickham at gmail.com
Thu Dec 20 18:36:13 CET 2007


On Dec 20, 2007 4:56 PM, Bert Jacobs <b.jacobs at pandora.be> wrote:
> Hi,
>
> The problem is probably that my Var4, does not contain number but factor
> information, and therefore I think Gabor's suggestion does not work.
> The same holds for Hadley's solution with the functions melt/cast, where the
> resulting dataframe looks OK, but the dataframe is filled with the number of
> times Var3 occurred for a certain Var1 and a certain Var2. It looks more
> like:
>
> Var1    Var2    W1      W2      W3      W4
> A       Fa      1       1
> A       Si      1               1
> B       Si      1
> C       La              1
> C       Do                              2
>
> While it should be:
>
> Var1    Var2    W1      W2      W3      W4
> A       Fa      F1      F3
> A       Si      F2              F4
> B       Si      F5
> C       La              F6
> C       Do                              F7
> C       Do                              F8
>
> Or even better:
>
> Var1    Var2    W1      W2      W3      W4
> A       Fa      F1      F3
> A       Si      F2              F4
> B       Si      F5
> C       La              F6
> C       Do                              F7/F8

In that case you'll need to specify an aggregation function.  You probably want:

cast(dfm, ... ~ Var3, function(x) paste(x, collapse="/"), add.missing=TRUE)

-- 
http://had.co.nz/



More information about the R-help mailing list