Skip to content

inserting zero instances with zeroes in a matrix

7 messages · Henrique Dallazuanna, Gabor Grothendieck, Simon Pickett

#
Try this:

as.data.frame(xtabs(count ~., d.f))
On Thu, Dec 18, 2008 at 6:25 AM, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
#
Thanks Gregor and Henrique for the eloquent and masterful replies,

These solutions have saved me hours (maybe even days) of work in the future,
I am very grateful. :-)

Simon.


----- Original Message ----- 
From: "Gabor Grothendieck" <ggrothendieck at gmail.com>
To: "Henrique Dallazuanna" <wwwhsd at gmail.com>
Cc: "Simon Pickett" <simon.pickett at bto.org>; <r-help at r-project.org>
Sent: Thursday, December 18, 2008 11:51 AM
Subject: Re: [R] inserting zero instances with zeroes in a matrix
#
Actually,

Both these solutions create contingency tables, with frequency rather than 
the original count values. Is there a way to retain the original count 
values?

Thanks again, Simon.


----- Original Message ----- 
From: "Simon Pickett" <simon.pickett at bto.org>
To: "Gabor Grothendieck" <ggrothendieck at gmail.com>; "Henrique Dallazuanna" 
<wwwhsd at gmail.com>
Cc: <r-help at r-project.org>
Sent: Thursday, December 18, 2008 12:11 PM
Subject: Re: [R] inserting zero instances with zeroes in a matrix
#
It does retain the original count values. Its only the column name
that has changed.  To change that you can use:

as.data.frame(xtabs(count ~., d.f), responseName = "count")
house     pet count
2 house1    cats     1
1 house1    dogs     2
3 house2    dogs     2
4 house3    dogs     1
5 house4  budgie     2
6 house4     cat     3
7 house4 hamster     4
house     pet count
9  house1    cats     1
13 house1    dogs     2
14 house2    dogs     2
15 house3    dogs     1
4  house4  budgie     2
8  house4     cat     3
20 house4 hamster     4
On Thu, Dec 18, 2008 at 9:46 AM, Simon Pickett <simon.pickett at bto.org> wrote: