Re: [R] adding tables
How about this: t2 - table(c(10,11,12,13)) t1 -table(c(1,1,2,4,5)) t12-(rbind(as.data.frame(t1),as.data.frame(t2))) xtabs(Freq~.,t12) it works for overlapping tables t2 - table(c(10,11,12,13,1,2)) t1 -table(c(1,1,2,4,5,11,12)) t12-(rbind(as.data.frame(t1),as.data.frame(t2))) xtabs(Freq~.,t12) and it will work for two-dimensional tables as well: t2 - table(c(10,11,12,13),letters[rep(1:2,each=2)]) t1 -table(c(1,1,2,4,5),letters[rep(c(1,3),length.out=5)]) t12a-(rbind(as.data.frame(t1),as.data.frame(t2))) xtabs(Freq~.,t12a) cheers. Am 10.12.2013 00:59, schrieb Ross Boylan: Can anyone recommend a good way to add tables? Ideally I would like t1 - table(x1) t2 - table(x2) t1+t2 It t1 and t2 have the same levels this works fine, but I need something that will work even if they differ, e.g., t1 1 2 4 5 2 1 1 1 t2 - table(c(10, 11, 12, 13)) t1+t2 # apparently does simple vector addition 1 2 4 5 3 2 2 2 whereas I want 1 2 4 5 10 11 12 13 2 1 1 1 1 1 1 1 __ R-help@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. -- Eik Vettorazzi Department of Medical Biometry and Epidemiology University Medical Center Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790 -- Besuchen Sie uns auf: www.uke.de _ Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; Gerichtsstand: Hamburg Vorstandsmitglieder: Prof. Dr. Christian Gerloff (Vertreter des Vorsitzenden), Prof. Dr. Dr. Uwe Koch-Gromus, Joachim Prölß, Rainer Schoppik _ SAVE PAPER - THINK BEFORE PRINTING __ R-help@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] adding tables
Can anyone recommend a good way to add tables? Ideally I would like t1 - table(x1) t2 - table(x2) t1+t2 It t1 and t2 have the same levels this works fine, but I need something that will work even if they differ, e.g., t1 1 2 4 5 2 1 1 1 t2 - table(c(10, 11, 12, 13)) t1+t2 # apparently does simple vector addition 1 2 4 5 3 2 2 2 whereas I want 1 2 4 5 10 11 12 13 2 1 1 1 1 1 1 1 __ R-help@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.
Re: [R] adding tables [partial solution]
Answering myself... On Mon, 2013-12-09 at 15:59 -0800, Ross Boylan wrote: Can anyone recommend a good way to add tables? For count data, which were my main concern, it looks as if tabulate with nbins will work. I'm not sure how this works with a cross-classifying factor, which I will also need. It's possible the factor may have missing values in some tabulations as well. For the one dimensional case t1 - tabulate(c(1, 2, 2, 4), nbins=5) t2 - tabulate(c(2, 3, 5), nbins=5) t1 [1] 1 2 0 1 0 t2 [1] 0 1 1 0 1 t1+t2 [1] 1 3 1 1 1 Ideally I would like t1 - table(x1) t2 - table(x2) t1+t2 It t1 and t2 have the same levels this works fine, but I need something that will work even if they differ, e.g., t1 1 2 4 5 2 1 1 1 t2 - table(c(10, 11, 12, 13)) t1+t2 # apparently does simple vector addition 1 2 4 5 3 2 2 2 whereas I want 1 2 4 5 10 11 12 13 2 1 1 1 1 1 1 1 __ R-help@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.
Re: [R] adding tables [partial solution]
Is this what you are after? http://www.statmethods.net/management/merging.html -Roy On Dec 9, 2013, at 4:09 PM, Ross Boylan r...@biostat.ucsf.edu wrote: Answering myself... On Mon, 2013-12-09 at 15:59 -0800, Ross Boylan wrote: Can anyone recommend a good way to add tables? For count data, which were my main concern, it looks as if tabulate with nbins will work. I'm not sure how this works with a cross-classifying factor, which I will also need. It's possible the factor may have missing values in some tabulations as well. For the one dimensional case t1 - tabulate(c(1, 2, 2, 4), nbins=5) t2 - tabulate(c(2, 3, 5), nbins=5) t1 [1] 1 2 0 1 0 t2 [1] 0 1 1 0 1 t1+t2 [1] 1 3 1 1 1 Ideally I would like t1 - table(x1) t2 - table(x2) t1+t2 It t1 and t2 have the same levels this works fine, but I need something that will work even if they differ, e.g., t1 1 2 4 5 2 1 1 1 t2 - table(c(10, 11, 12, 13)) t1+t2 # apparently does simple vector addition 1 2 4 5 3 2 2 2 whereas I want 1 2 4 5 10 11 12 13 2 1 1 1 1 1 1 1 __ R-help@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. ** The contents of this message do not reflect any position of the U.S. Government or NOAA. ** Roy Mendelssohn Supervisory Operations Research Analyst NOAA/NMFS Environmental Research Division Southwest Fisheries Science Center 1352 Lighthouse Avenue Pacific Grove, CA 93950-2097 e-mail: roy.mendelss...@noaa.gov (Note new e-mail address) voice: (831)-648-9029 fax: (831)-648-8440 www: http://www.pfeg.noaa.gov/ Old age and treachery will overcome youth and skill. From those who have been given much, much will be expected the arc of the moral universe is long, but it bends toward justice -MLK Jr. __ R-help@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.
Re: [R] adding tables [partial solution]
On Mon, 2013-12-09 at 16:20 -0800, Roy Mendelssohn wrote: Is this what you are after? http://www.statmethods.net/management/merging.html Not directly. Something could probably be fashioned using it and as.data.frame on the result of table(), but merge doesn't sum values. Ross -Roy On Dec 9, 2013, at 4:09 PM, Ross Boylan r...@biostat.ucsf.edu wrote: Answering myself... On Mon, 2013-12-09 at 15:59 -0800, Ross Boylan wrote: Can anyone recommend a good way to add tables? For count data, which were my main concern, it looks as if tabulate with nbins will work. I'm not sure how this works with a cross-classifying factor, which I will also need. It's possible the factor may have missing values in some tabulations as well. For the one dimensional case t1 - tabulate(c(1, 2, 2, 4), nbins=5) t2 - tabulate(c(2, 3, 5), nbins=5) t1 [1] 1 2 0 1 0 t2 [1] 0 1 1 0 1 t1+t2 [1] 1 3 1 1 1 Ideally I would like t1 - table(x1) t2 - table(x2) t1+t2 It t1 and t2 have the same levels this works fine, but I need something that will work even if they differ, e.g., t1 1 2 4 5 2 1 1 1 t2 - table(c(10, 11, 12, 13)) t1+t2 # apparently does simple vector addition 1 2 4 5 3 2 2 2 whereas I want 1 2 4 5 10 11 12 13 2 1 1 1 1 1 1 1 __ R-help@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. ** The contents of this message do not reflect any position of the U.S. Government or NOAA. ** Roy Mendelssohn Supervisory Operations Research Analyst NOAA/NMFS Environmental Research Division Southwest Fisheries Science Center 1352 Lighthouse Avenue Pacific Grove, CA 93950-2097 e-mail: roy.mendelss...@noaa.gov (Note new e-mail address) voice: (831)-648-9029 fax: (831)-648-8440 www: http://www.pfeg.noaa.gov/ Old age and treachery will overcome youth and skill. From those who have been given much, much will be expected the arc of the moral universe is long, but it bends toward justice -MLK Jr. __ R-help@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.
Re: [R] adding tables
On Dec 9, 2013, at 3:59 PM, Ross Boylan wrote: Can anyone recommend a good way to add tables? Ideally I would like t1 - table(x1) t2 - table(x2) t1+t2 It t1 and t2 have the same levels this works fine, but I need something that will work even if they differ, e.g., t1 1 2 4 5 2 1 1 1 t2 - table(c(10, 11, 12, 13)) t1+t2 # apparently does simple vector addition 1 2 4 5 3 2 2 2 whereas I want 1 2 4 5 10 11 12 13 2 1 1 1 1 1 1 1 R contingency tables are really matrices, so the `cbind` matrix-method appears to be what you want: cbind(t1,t2) David Winsemius Alameda, CA, USA __ R-help@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.
Re: [R] adding tables [solution]
On Mon, 2013-12-09 at 16:09 -0800, Ross Boylan wrote: Answering myself... On Mon, 2013-12-09 at 15:59 -0800, Ross Boylan wrote: Can anyone recommend a good way to add tables? For count data, which were my main concern, it looks as if tabulate with nbins will work. I'm not sure how this works with a cross-classifying factor, which I will also need. It's possible the factor may have missing values in some tabulations as well. By using the levels argument to factor one can ensure that all possible levels are present. Then table() will do the right thing. An example with numbers: t1 - factor(c(3, 2), levels=1:5) t1 [1] 3 2 Levels: 1 2 3 4 5 table(t1) t1 1 2 3 4 5 0 1 1 0 0 This works in arbitrary dimensions. I figured this out after looking at the code for table, which does the hard part when there is more than one dimension. Ross For the one dimensional case t1 - tabulate(c(1, 2, 2, 4), nbins=5) t2 - tabulate(c(2, 3, 5), nbins=5) t1 [1] 1 2 0 1 0 t2 [1] 0 1 1 0 1 t1+t2 [1] 1 3 1 1 1 Ideally I would like t1 - table(x1) t2 - table(x2) t1+t2 It t1 and t2 have the same levels this works fine, but I need something that will work even if they differ, e.g., t1 1 2 4 5 2 1 1 1 t2 - table(c(10, 11, 12, 13)) t1+t2 # apparently does simple vector addition 1 2 4 5 3 2 2 2 whereas I want 1 2 4 5 10 11 12 13 2 1 1 1 1 1 1 1 __ R-help@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.
Re: [R] adding tables
HI, May be this helps: t2 - table(c(10,11,12,13)) t1 -table(c(1,1,2,4,5)) t - c(t1,t2) tapply(t,sort(as.numeric(names(t))),sum) A.K. On Monday, December 9, 2013 7:01 PM, Ross Boylan r...@biostat.ucsf.edu wrote: Can anyone recommend a good way to add tables? Ideally I would like t1 - table(x1) t2 - table(x2) t1+t2 It t1 and t2 have the same levels this works fine, but I need something that will work even if they differ, e.g., t1 1 2 4 5 2 1 1 1 t2 - table(c(10, 11, 12, 13)) t1+t2 # apparently does simple vector addition 1 2 4 5 3 2 2 2 whereas I want 1 2 4 5 10 11 12 13 2 1 1 1 1 1 1 1 __ R-help@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@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.