Reshaping with tidyr

14 views
Skip to first unread message

Simon Givoli

unread,
Oct 2, 2015, 5:17:04 AM10/2/15
to Israel R User Group

Hi all,


Might be silly but I'm a newbie with R…


My df looks like this:

Id type count

3    A    5

3    B    0   

3    c    1

4    A    5

4    A    4

4    A    3


I want to reshape it to this:

Id   A    B    C

3    5    0    1

4    5    4    3


I tried:

df_wide <- spread(df, Type, count)


but nothing happened, the df stayed the same! What am I doing wrong?


Simon

amit gal

unread,
Oct 2, 2015, 5:22:18 AM10/2/15
to israel-r-...@googlegroups.com

From package reshape2 use the dcast function
W = dcast(id ~ type)

--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Simon Givoli

unread,
Oct 2, 2015, 5:59:47 AM10/2/15
to Israel R User Group

Thanks, almost there.

 

My data has an additional column containing found/not found (I thought it wasn't important so didn't mention it earlier).

When applying your solution, I get:

Id   A       B        C

3    found not-found found

4    found found     found

With this message:

" Using Found as value column: use value.var to override."
 

I tried:

W <- dcast(df, Id ~ type, value.var = count)

But got this message:

Error: value.var (21567345
In addition: Warning message:
In if (!(value.var %in% names(data))) { :

  the condition has length > 1 and only the first element will be used
 
 

 

 


בתאריך יום שישי, 2 באוקטובר 2015 בשעה 12:22:18 UTC+3, מאת Amit Gal:
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-group+unsub...@googlegroups.com.

Simon Givoli

unread,
Oct 2, 2015, 6:56:17 AM10/2/15
to Israel R User Group

Found my mistake!

Should have wrote:

W <- dcast(df, Id ~ type, value.var="count")

 

Thanks! R rocks!

Simon


בתאריך יום שישי, 2 באוקטובר 2015 בשעה 12:59:47 UTC+3, מאת Simon Givoli:
Reply all
Reply to author
Forward
0 new messages