[R] Changing ungrouped cases to grouped cases

Peter Ehlers ehlers at ucalgary.ca
Fri Jul 20 21:17:10 CEST 2012

On 2012-07-20 12:09, Rui Barradas wrote:
> Hello,
>
> Still with aggregate, use length() not sum().
>
>
> dtagroup <- aggregate(y ~ A + B + C, data=dtf, sum)
> dtalength <- aggregate(y ~ A + B + C, data=dtf, length)
>
> # Now merge the two
> names(dtalength)[4] <- "count"
> mm <- merge(dtagroup, dtalength)
>
> # And make it pretty
> mm <- mm[, c("y", "A", "B", "C", "count")]
> mm
>
> Hope this helps,
>

Package plyr's ddply() with summarize does this sort
of thing nicely:

library(plyr)
ddply(dtf, .(A,B,C), summarize,
y = sum(y),
count = length(y))

Peter Ehlers

>
> Em 20-07-2012 19:52, Christopher Desjardins escreveu:
>> As a follow up is there any way to also get the count for each combination?
>> For example
>>
>>    y     A   B   C
>>    0     1    1   2
>>    0     1    2   1
>>    1     1    1   2
>>    0     1    1   2
>>    1     1    1   2
>>    1     1    2   1
>>    0     1    2   2
>>
>> Should become:
>>
>>    y   A  B  C  count
>>    2   1   1  2    4
>>    1   1   2  1    2
>>    0   1   2   2   1
>> .
>> .
>> .
>> So I would know there were 2 successes out of 4.
>> Thanks!
>> Chris
>>
>>
>>
>> On Fri, Jul 20, 2012 at 10:41 AM, Christopher Desjardins <
>> cddesjardins at gmail.com> wrote:
>>
>>> Thanks the aggregate() command is what I was looking for.
>>> Chris
>>>
>>>
>>> On Thu, Jul 19, 2012 at 9:03 PM, David L Carlson <dcarlson at tamu.edu>wrote:
>>>
>>>>> dtf <- read.table(text="y     A   B   C
>>>> + 0     1    1   2
>>>> + 0     1    2   1
>>>> + 1     1    1   2
>>>> + 0     1    1   2
>>>> + 1     1    1   2
>>>> + 1     1    2   1
>>>> + 0     1    2   2",
>>>>> dtagroup <- aggregate(y~A+B+C, dtf, sum)
>>>> # Gets you the groups. If you need the column/row order:
>>>>
>>>>> dtagroup <- dtagroup[order(dtagroup\$y, decreasing=TRUE),c(4, 1:3)]
>>>> ----------------------------------------------
>>>> David L Carlson
>>>> Associate Professor of Anthropology
>>>> Texas A&M University
>>>> College Station, TX 77843-4352
>>>>
>>>>> -----Original Message-----
>>>>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>>>>> project.org] On Behalf Of Christopher Desjardins
>>>>> Sent: Thursday, July 19, 2012 7:35 PM
>>>>> To: R help
>>>>> Subject: [R] Changing ungrouped cases to grouped cases
>>>>>
>>>>> Hi,
>>>>> I have my data the following way:
>>>>>
>>>>> y     A   B   C
>>>>> 0     1    1   2
>>>>> 0     1    2   1
>>>>> 1     1    1   2
>>>>> 0     1    1   2
>>>>> 1     1    1   2
>>>>> 1     1    2   1
>>>>> 0     1    2   2
>>>>> .
>>>>> .
>>>>> .
>>>>> And so on.  How can I make my data look like the following:
>>>>> y   A  B  C
>>>>> 2   1   1  2
>>>>> 1   1   2  1
>>>>> 0   1   2   2
>>>>> .
>>>>> .
>>>>> .
>>>>>
>>>>> In other words how can I change my ungrouped cases into grouped cases?
>>>>> Thanks!
>>>>> Chris
>>>>>
>>>>>         [[alternative HTML version deleted]]
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> guide.html
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help