[R] Frequency and summary statistics table with different variables and categories
David Winsemius
dwinsemius at comcast.net
Thu Jan 22 17:09:28 CET 2009
You should look at how summary.data.frame does it. (You are, of
course, re-inventing the wheel.)
You probably ought to also look at the doBy package with its summaryBy
function. The help page example uses the length function for counts.
I often prefer the output of describe() in Hmisc.
--
David Winsemius
On Jan 22, 2009, at 8:32 AM, David Winsemius wrote:
> One of the various tabulation functions would seem to be the most
> appropriate for getting frequency summaries:
>
> ?table
> ?xtabs
>
> On Jan 22, 2009, at 6:17 AM, Stefan Björk wrote:
>
>> Hello helpers,
>>
>> This is probably quite simple, but I'm stuck.
>>
>> I want to create a summary statistics table with frequencies and
>> summary
>> statistics for a large number of variables. The problem here is
>> that (1)
>> there are two different classes of categories (sex, type of
>> substance abuse
>> and type of treatent) which overlap, (2) the data for different
>> variables
>> should be presented in different ways -- sometimes with relative
>> frequencies, other times with mean values.
>>
>> The table would finally look something like:
>>
>> All Male Female Alcohol Drug ...
>> Age (mean) (mean) ...
>> Sex (% male) (freq) (freq) ...
>> Alcohol CS (mean) (mean) ...
>> ... ...
>>
>> Data is in a data frame with quite a lot of columns (variables) and
>> each row
>> represents a single case.
>>
>> I have found out that part of this can be done with tapply, for
>> example
>> tapply(age, sex, mean) and join it with tapply(age, abuse, mean).
>> But how to
>> do with frequencies? Or is there an even simpler way?
>>
>> /S
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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.
More information about the R-help
mailing list