[R] descriptive stats by cells in factorial design
Mike Miller
mbmiller+l at gmail.com
Thu Aug 8 04:58:56 CEST 2013
On Tue, 6 Aug 2013, David Winsemius wrote:
> Look at the code. You are attributing behavior to `summaryBy` that
> should be ascribed to `print.data.frame`, and to `format.data.frame`.
> Your function is returning a numeric vector and getting displayed by
> `print.default`.
Thanks! That's the thing I wasn't getting. I spent so many years in the
world of Octave/MATLAB that I often forget that function output in R is
often in the form of a neat data frame, but I do like the R way better.
Using print.data.frame() and specifying digits=3 or digits=4, I get the
appearance in the output that I was looking for.
Example:
> sumtab <- summaryBy(Age ~ Generation + Zygosity + Sex, data=x, FUN=descriptivefun, na.rm=T, order=T)
> str(sumtab)
'data.frame': 8 obs. of 12 variables:
$ Generation: Factor w/ 2 levels "Offspring","Parent": 1 1 1 1 2 2 2 2
$ Zygosity : Factor w/ 2 levels "DZ","MZ": 1 1 2 2 1 1 2 2
$ Sex : Factor w/ 2 levels "Female","Male": 1 2 1 2 1 2 1 2
$ Age.mean : num 17.8 17.7 17.7 17.7 44.3 ...
$ Age.sd : num 0.555 0.456 0.603 0.515 5.011 ...
$ Age.0% : num 16.6 16.7 16.6 16.6 32.2 ...
$ Age.25% : num 17.4 17.4 17.2 17.3 40.8 ...
$ Age.50% : num 17.8 17.7 17.8 17.7 44.2 ...
$ Age.75% : num 18.1 18 18.1 18 47.5 ...
$ Age.100% : num 19.5 19 19.7 20 62.7 ...
$ Age.N : num 459 452 882 811 296 342 533 659
$ Age.NAs : num 0 0 0 0 0 0 0 0
> print.data.frame(sumtab, digits=3)
Generation Zygosity Sex Age.mean Age.sd Age.0% Age.25% Age.50% Age.75% Age.100% Age.N Age.NAs
1 Offspring DZ Female 17.8 0.555 16.6 17.4 17.8 18.1 19.5 459 0
2 Offspring DZ Male 17.7 0.456 16.7 17.4 17.7 18.0 19.0 452 0
3 Offspring MZ Female 17.7 0.603 16.6 17.2 17.8 18.1 19.7 882 0
4 Offspring MZ Male 17.7 0.515 16.6 17.3 17.7 18.0 20.0 811 0
5 Parent DZ Female 44.3 5.011 32.2 40.8 44.2 47.5 62.7 296 0
6 Parent DZ Male 44.1 4.884 32.0 41.1 44.3 47.2 61.1 342 0
7 Parent MZ Female 44.2 5.406 29.6 40.5 44.0 47.7 63.2 533 0
8 Parent MZ Male 43.1 5.349 31.3 39.4 42.9 46.2 65.3 659 0
Or I can produce the output and format it in one step:
> print.data.frame(summaryBy(Age ~ Generation + Zygosity + Sex + Cohort + ESstatus, data=x, FUN=descriptivefun, na.rm=T, order=T), digits=3)
Generation Zygosity Sex Cohort ESstatus Age.mean Age.sd Age.0% Age.25% Age.50% Age.75% Age.100% Age.N Age.NAs
1 Offspring DZ Female 11 ES 17.8 0.354 16.9 17.6 17.8 18.0 18.9 106 0
2 Offspring DZ Female 11 notES 18.1 0.556 16.8 17.9 18.2 18.5 19.5 162 0
3 Offspring DZ Female 17 notES 17.5 0.457 16.6 17.1 17.6 17.9 18.3 191 0
4 Offspring DZ Male 11 ES 17.8 0.347 17.2 17.5 17.7 18.0 18.7 134 0
5 Offspring DZ Male 11 notES 17.9 0.519 16.8 17.5 17.9 18.2 19.0 153 0
6 Offspring DZ Male 17 notES 17.5 0.392 16.7 17.2 17.5 17.8 18.5 165 0
7 Offspring MZ Female 11 ES 17.9 0.451 16.9 17.7 17.8 18.1 19.1 196 0
8 Offspring MZ Female 11 notES 18.1 0.610 16.8 17.6 18.1 18.4 19.7 291 0
9 Offspring MZ Female 17 notES 17.4 0.496 16.6 17.0 17.3 17.8 18.4 395 0
10 Offspring MZ Male 11 ES 17.8 0.324 16.8 17.6 17.8 18.0 19.0 195 0
11 Offspring MZ Male 11 notES 17.9 0.647 16.6 17.3 17.9 18.2 20.0 284 0
12 Offspring MZ Male 17 notES 17.5 0.396 16.6 17.2 17.5 17.8 18.4 332 0
13 Parent DZ Female 11 ES 44.6 5.125 32.2 41.3 44.7 48.3 58.0 121 0
14 Parent DZ Female 11 notES 42.5 4.367 34.0 39.3 42.1 45.5 57.1 107 0
15 Parent DZ Female 17 notES 46.3 4.918 36.1 42.7 45.8 48.3 62.7 68 0
16 Parent DZ Male 11 ES 44.6 4.561 34.3 41.4 44.9 47.5 58.8 126 0
17 Parent DZ Male 11 notES 42.7 4.960 32.0 39.2 42.8 45.3 58.2 157 0
18 Parent DZ Male 17 notES 46.8 4.023 40.2 44.1 46.0 48.8 61.1 59 0
19 Parent MZ Female 11 ES 44.2 5.021 29.6 40.7 44.1 47.7 56.7 206 0
20 Parent MZ Female 11 notES 42.3 5.362 30.3 38.6 41.8 46.0 56.6 172 0
21 Parent MZ Female 17 notES 46.4 5.177 34.9 42.4 46.0 49.5 63.2 155 0
22 Parent MZ Male 11 ES 43.4 5.351 31.3 40.0 43.4 46.5 64.7 197 0
23 Parent MZ Male 11 notES 41.6 4.656 32.1 38.0 41.4 44.6 65.3 331 0
24 Parent MZ Male 17 notES 46.7 5.242 34.5 43.1 45.9 49.0 63.8 131 0
Thanks to all.
Mike
--
Michael B. Miller, Ph.D.
Minnesota Center for Twin and Family Research
Department of Psychology
University of Minnesota
More information about the R-help
mailing list