Re: [R] Counting with multiple criteria using data table
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 Gunterwrote: >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 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 A20May 1 >> >> 2: green B25 June 2 >> >> 3: green A10 April 3 >> >> 4: black A17 August 3 >> >> 5:red C 5May 5 >> >> 6: orange D 0 June 13 >> >> 7: orange E12 April 5 >> >> 8: orange F11 August 8 >> >> 9: orange F99 April 23 >> >> 10: orange F70May 7 >> >> 11: black A77 June 11 >> >> 12: green B87 August 33 >> >> 13: black A79 April 9 >> >> 14: green A68May 14 >> >> 15: black C90 June 31 >> >> 16: green D79 August 11 >> >> 17: black E 101 April 17 >> >> 18:red F90 June 21 >> >> 19:red F 112 August 13 >> >> 20:red F 101 April 20 >> >>> Test[Color=="green"=="A", .N, by=Month] >> >>Month N >> >> 1: April 1 >> >> 2: May 1 >> >>> Test[Color=="orange"=="F", .N, by=Month] >> >> Month N >> >> 1: August 1 >> >> 2: April 1 >> >> 3:May 1 >> >> >> >>> Test[Color=="orange"=="F", .N, by=Month] >> >> Month N >> >> 1: August 1 >> >> 2: April 1 >> >> 3:May 1 >> >>> Test[Color=="red"=="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 >> >> June0 0 0 >> >> August 01 1 >> >> [[alternative HTML version deleted]] >> >> __ >> R-help@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@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@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.
Re: [R] Counting with multiple criteria using data table
> On Jun 21, 2017, at 2:50 PM, Ek Esawiwrote: > > 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 A20May 1 > > 2: green B25 June 2 > > 3: green A10 April 3 > > 4: black A17 August 3 > > 5:red C 5May 5 > > 6: orange D 0 June 13 > > 7: orange E12 April 5 > > 8: orange F11 August 8 > > 9: orange F99 April 23 > > 10: orange F70May 7 > > 11: black A77 June 11 > > 12: green B87 August 33 > > 13: black A79 April 9 > > 14: green A68May 14 > > 15: black C90 June 31 > > 16: green D79 August 11 > > 17: black E 101 April 17 > > 18:red F90 June 21 > > 19:red F 112 August 13 > > 20:red F 101 April 20 You should have offered the output of: dput(Test) > >> Test[Color=="green"=="A", .N, by=Month] > > Month N > > 1: April 1 > > 2: May 1 > >> Test[Color=="orange"=="F", .N, by=Month] > >Month N > > 1: August 1 > > 2: April 1 > > 3:May 1 > > > >> Test[Color=="orange"=="F", .N, by=Month] > >Month N > > 1: August 1 > > 2: April 1 > > 3:May 1 > >> Test[Color=="red"=="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 > > June0 0 0 > > August 01 1 I count 4 data.tables and a total of 11 items so why only 3 columns and 9 items? Were you tabulating colors by month? > Test[ (Color=="green"=="A") | (Color=="red"=="F") | (Color=="orange"=="F")| (Color=="orange"=="F")| (Color=="red"=="F") ,table(Month, Color)] Color Monthgreen orange red April 1 1 1 August 0 1 1 June 0 0 1 May1 1 0 > > > [[alternative HTML version deleted]] Rhelp is plain-text. Do read the Posting Guide: Another possibility: Test[(Color=="green"=="A") |These are the conditions separated by logical OR's in the first argument to `[data.table` (Color=="red"=="F") | (Color=="orange"=="F")| (Color=="orange"=="F")| (Color=="red"=="F") , table(Month, Grade)] Grade MonthA F April 1 2 August 0 2 June 0 1 May1 1 > __ > R-help@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. David Winsemius Alameda, CA, USA __ R-help@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.
Re: [R] Counting with multiple criteria using data table
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 Esawiwrote: > 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 A20May 1 > > 2: green B25 June 2 > > 3: green A10 April 3 > > 4: black A17 August 3 > > 5:red C 5May 5 > > 6: orange D 0 June 13 > > 7: orange E12 April 5 > > 8: orange F11 August 8 > > 9: orange F99 April 23 > > 10: orange F70May 7 > > 11: black A77 June 11 > > 12: green B87 August 33 > > 13: black A79 April 9 > > 14: green A68May 14 > > 15: black C90 June 31 > > 16: green D79 August 11 > > 17: black E 101 April 17 > > 18:red F90 June 21 > > 19:red F 112 August 13 > > 20:red F 101 April 20 > >> Test[Color=="green"=="A", .N, by=Month] > >Month N > > 1: April 1 > > 2: May 1 > >> Test[Color=="orange"=="F", .N, by=Month] > > Month N > > 1: August 1 > > 2: April 1 > > 3:May 1 > > > >> Test[Color=="orange"=="F", .N, by=Month] > > Month N > > 1: August 1 > > 2: April 1 > > 3:May 1 > >> Test[Color=="red"=="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 > > June0 0 0 > > August 01 1 > > [[alternative HTML version deleted]] > > __ > R-help@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@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] Counting with multiple criteria using data table
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 A20May 1 2: green B25 June 2 3: green A10 April 3 4: black A17 August 3 5:red C 5May 5 6: orange D 0 June 13 7: orange E12 April 5 8: orange F11 August 8 9: orange F99 April 23 10: orange F70May 7 11: black A77 June 11 12: green B87 August 33 13: black A79 April 9 14: green A68May 14 15: black C90 June 31 16: green D79 August 11 17: black E 101 April 17 18:red F90 June 21 19:red F 112 August 13 20:red F 101 April 20 > Test[Color=="green"=="A", .N, by=Month] Month N 1: April 1 2: May 1 > Test[Color=="orange"=="F", .N, by=Month] Month N 1: August 1 2: April 1 3:May 1 > Test[Color=="orange"=="F", .N, by=Month] Month N 1: August 1 2: April 1 3:May 1 > Test[Color=="red"=="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 June0 0 0 August 01 1 [[alternative HTML version deleted]] __ R-help@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.