[R] Counting with multiple criteria using data table

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Thu Jun 22 03:09:58 CEST 2017


To be fair, the OP did provide brief snippets of data.table usage below the data dump indicating some level of effort, but posted it all in HTML (what you see we do not see), did not make the example reproducible (dput is great, and library calls really clear things up [1][2][3]), and this looks suspiciously like homework  (not on topic here, see the Posting Guide).

[1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

[2] http://adv-r.had.co.nz/Reproducibility.html

[3] https://cran.r-project.org/web/packages/reprex/index.html
-- 
Sent from my phone. Please excuse my brevity.

On June 21, 2017 4:16:36 PM PDT, Bert Gunter <bgunter.4567 at gmail.com> wrote:
>Have you gone through any R tutorials? If not, why not? If so, maybe
>you need to spend some more time with them.
>
>It looks like you want us to do your work for you. We don't do this.
>See (and follow) the posting guide below for what we might do (we're
>volunteers, so no guarantees).
>
>Cheers,
>Bert
>
>
>Bert Gunter
>
>"The trouble with having an open mind is that people keep coming along
>and sticking things into it."
>-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
>On Wed, Jun 21, 2017 at 2:50 PM, Ek Esawi <esawiek at gmail.com> wrote:
>> I have a data.table which is shown below. I want to count
>combinations of
>> columns on i and count on j with by. A few examples are given below
>the
>> table.
>>
>>
>>
>> I want to:
>>
>> all months to show on the output including those that they have zero
>value
>>
>> I want the three statements combined in on if possible so the output
>will
>> be one data table; that is the outputs are next to each other as
>manually
>> illustrated on the last part (desired output).
>>
>>
>>
>>
>>
>> Thanks--EK
>>
>>
>>
>>
>>
>>> Test
>>
>>      Color Grade Value  Month Day
>>
>>  1: yellow     A    20    May   1
>>
>>  2:  green     B    25   June   2
>>
>>  3:  green     A    10  April   3
>>
>>  4:  black     A    17 August   3
>>
>>  5:    red     C     5    May   5
>>
>>  6: orange     D     0   June  13
>>
>>  7: orange     E    12  April   5
>>
>>  8: orange     F    11 August   8
>>
>>  9: orange     F    99  April  23
>>
>> 10: orange     F    70    May   7
>>
>> 11:  black     A    77   June  11
>>
>> 12:  green     B    87 August  33
>>
>> 13:  black     A    79  April   9
>>
>> 14:  green     A    68    May  14
>>
>> 15:  black     C    90   June  31
>>
>> 16:  green     D    79 August  11
>>
>> 17:  black     E   101  April  17
>>
>> 18:    red     F    90   June  21
>>
>> 19:    red     F   112 August  13
>>
>> 20:    red     F   101  April  20
>>
>>> Test[Color=="green"&Grade=="A", .N, by=Month]
>>
>>    Month N
>>
>> 1: April 1
>>
>> 2:   May 1
>>
>>> Test[Color=="orange"&Grade=="F", .N, by=Month]
>>
>>     Month N
>>
>> 1: August 1
>>
>> 2:  April 1
>>
>> 3:    May 1
>>
>>
>>
>>> Test[Color=="orange"&Grade=="F", .N, by=Month]
>>
>>     Month     N
>>
>> 1: August     1
>>
>> 2:  April     1
>>
>> 3:    May     1
>>
>>> Test[Color=="red"&Grade=="F", .N, by=Month]
>>
>>     Month N
>>
>> 1:   June 1
>>
>> 2: August 1
>>
>> 3:  April 1
>>
>>
>>
>> Desired output
>>
>>                 N1           N2           N3
>>
>> April       1              1              1
>>
>> May       1              1              1
>>
>> June        0              0              0
>>
>> August 0                1              1
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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 -- To UNSUBSCRIBE and more, see
>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