[R] inserting zero instances with zeroes in a matrix

Simon Pickett simon.pickett at bto.org
Thu Dec 18 15:46:51 CET 2008


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


> 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
>
>
>> 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:
>>> Try this:
>>>
>>> with(d.f,
>>>    {merge(data.frame(house = rep(unique(house), each =
>>> length(unique(pet))),
>>>             pet = unique(pet)), d.f, by = c("house", "pet"), all = TRUE)
>>>      }
>>>     )
>>>
>>> On Thu, Dec 18, 2008 at 8:58 AM, Simon Pickett 
>>> <simon.pickett at bto.org>wrote:
>>>
>>>> Hi all,
>>>>
>>>> Suppose I had the below example where a survey was carried out 
>>>> recording
>>>> the number of each type of pet in each house
>>>>
>>>> count<-c(2,1,2,1,2,3,4)
>>>> house<-c("house1","house1","house2","house3","house4","house4","house4")
>>>> pet<-c("dogs","cats","dogs","dogs","budgie","cat","hamster")
>>>> d.f<-data.frame(house,pet,count)
>>>>
>>>> How would I acheive a dataframe that had every instance of house in 
>>>> column
>>>> 1, all possible pets in column 2 and counts in column 3 like this...
>>>>
>>>> newhouse<-rep(unique(house),1,each=4)
>>>> newpets<-rep(unique(pet),4)
>>>> newcount<-c(2,1,0,0,2,0,0,0,1,0,0,0,0,3,2,4)
>>>> newdf<-data.frame(newhouse,newpets,newcount)
>>>>
>>>> Is there a way of doing this without creating a new matrix and using
>>>> programming loops?
>>>>
>>>> Thanks in advance, Simon.
>>>>
>>>>
>>>>
>>>>        [[alternative HTML version deleted]]
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>
>>>
>>>
>>> --
>>> Henrique Dallazuanna
>>> Curitiba-Paraná-Brasil
>>> 25° 25' 40" S 49° 16' 22" O
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide 
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>>
>>
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list