[R] categorical analysis - grouping rows

Rui Barradas ruipbarradas at sapo.pt
Sun May 13 13:33:50 CEST 2012


Hello,

If the output of ftable is kept in an object named 'ft', try this.


ftperc <- t(apply(ft, 1, function(x)
     if(sum(x)) round(100*x/sum(x)) else rep(0, length(x))))
attributes(ftperc) <- attributes(ft)
ftperc


(And if you need it to process several datasets, a function with 'ft' as 
the argument and the code above as its body is straightforward to write.)

Hope this helps,

Rui Barradas

Em 13-05-2012 11:00, r-help-request at r-project.org escreveu:
> Date: Sat, 12 May 2012 14:00:03 -0700 (PDT)
> From: cassiorx<cassiodorus at hotmail.com>
> To:r-help at r-project.org
> Subject: [R] categorical analysis - grouping rows
> Message-ID:<1336856403964-4629503.post at n4.nabble.com>
> Content-Type: text/plain; charset=us-ascii
>
> I apologize up front if this has been covered elsewhere - but I can't find
> any such question.
>
> I have a data set that contains academic data: term (i.e., semester),
> student id, dept, class, success (1=Y, 0=N)
>
> I want to look at dept by term to determine descriptive statistics for
> success to failure ratios. The intent being to discover if there are
> departments that contribute significantly to the Simpson Paradox, that is,
> that make overall success/failure rates undependable.
>
> It's easy to use ftable to get the counts for what I need (row names dept
> and success, col name success.  So I get something that looks like this:
>
>               Term      1st   2nd    3rd    4th    5th
> dept success
> AAA  0               155    240    163    286    293
>            1               424    570    349    582    429
> AAB  0                55      64    103       46    109
>           1               122    117    145    112    145
> AAC  0                11         3        4         4         4
>            1                19       12      23       11        7
>
> How can I calculate percentages by dept so that I get
>
> AAA 0         27  ....
>           1         73  ....
> AAB 0        ...
>
> Part of my lack of understanding is that I don't see a way to get the dept
> (by term) totals into a data structure that I can use to calculate the
> percentages. I can write procedural code to do this but is there some r-way
> that would be better?
>
> --
> View this message in context:http://r.789695.n4.nabble.com/categorical-analysis-grouping-rows-tp4629503.html
> Sent from the R help mailing list archive at Nabble.com.
>



More information about the R-help mailing list