I an stumbling on something that is probably very simple, but I cannot see the solution. I have an object generated by the table () function and want to recompute this table so each cell represents the percentage of the corresponding row sum. Of course a dedicated function can be written (which I have done), containing the necessary loops etc., but there should be a simpler way. I'd prefer something simple and as transparent as possible since it is for use in a text I am writing for my students. I have fiddled around with the apply () function but have so far been unable to find something that works. Any suggestions? Tom
Tom Backer Johnsen wrote:> I an stumbling on something that is probably very simple, but I cannot > see the solution. I have an object generated by the table () function > and want to recompute this table so each cell represents the > percentage of the corresponding row sum. > > Of course a dedicated function can be written (which I have done), > containing the necessary loops etc., but there should be a simpler > way. I'd prefer something simple and as transparent as possible since > it is for use in a text I am writing for my students. I have fiddled > around with the apply () function but have so far been unable to find > something that works. > > Any suggestions? > >prop.table (which is really just a wrapper for apply & sweep)> Tom > > ______________________________________________ > 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. >-- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On 2/7/2008 2:03 PM, Tom Backer Johnsen wrote:> I an stumbling on something that is probably very simple, but I cannot > see the solution. I have an object generated by the table () function > and want to recompute this table so each cell represents the > percentage of the corresponding row sum. > > Of course a dedicated function can be written (which I have done), > containing the necessary loops etc., but there should be a simpler > way. I'd prefer something simple and as transparent as possible since > it is for use in a text I am writing for my students. I have fiddled > around with the apply () function but have so far been unable to find > something that works. > > Any suggestions?See ?prop.table and also possibly ?CrossTable in the gmodels package.> Tom > > ______________________________________________ > 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.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Try this: prop.table(table(data), 1) On 07/02/2008, Tom Backer Johnsen <backer at psych.uib.no> wrote:> I an stumbling on something that is probably very simple, but I cannot > see the solution. I have an object generated by the table () function > and want to recompute this table so each cell represents the > percentage of the corresponding row sum. > > Of course a dedicated function can be written (which I have done), > containing the necessary loops etc., but there should be a simpler > way. I'd prefer something simple and as transparent as possible since > it is for use in a text I am writing for my students. I have fiddled > around with the apply () function but have so far been unable to find > something that works. > > Any suggestions? > > Tom > > ______________________________________________ > 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
Tom Backer Johnsen wrote:> Henrique Dallazuanna wrote: >> Try this: >> >> prop.table(table(data), 1) > > Ah. I misunderstood Peter Dalgaard (unnskyld Peter!). That gives what > I want. Thank you! > > Tom >> >> On 07/02/2008, Tom Backer Johnsen <backer at psych.uib.no> wrote: >>> I an stumbling on something that is probably very simple, but I cannot >>> see the solution. I have an object generated by the table () function >>> and want to recompute this table so each cell represents the >>> percentage of the corresponding row sum. >>> >>> Of course a dedicated function can be written (which I have done), >>> containing the necessary loops etc., but there should be a simpler >>> way. I'd prefer something simple and as transparent as possible since >>> it is for use in a text I am writing for my students. I have fiddled >>> around with the apply () function but have so far been unable to find >>> something that works. >>> >>> Any suggestions? >>> >>> Tom >>> >>> ______________________________________________ >>> 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. >>> >> >> > >-- +----------------------------------------------------------------+ | Tom Backer Johnsen, Psychometrics Unit, Faculty of Psychology | | University of Bergen, Christies gt. 12, N-5015 Bergen, NORWAY | | Tel : +47-5558-9185 Fax : +47-5558-9879 | | Email : backer at psych.uib.no URL : http://www.galton.uib.no/ | +----------------------------------------------------------------+
Tom Backer Johnsen wrote:> I an stumbling on something that is probably very simple, but I cannot > see the solution. I have an object generated by the table () function > and want to recompute this table so each cell represents the > percentage of the corresponding row sum. > > Of course a dedicated function can be written (which I have done), > containing the necessary loops etc., but there should be a simpler > way. I'd prefer something simple and as transparent as possible since > it is for use in a text I am writing for my students. I have fiddled > around with the apply () function but have so far been unable to find > something that works. >Hi Tom, Does "xtab" in the prettyR package do what you want? Jim