[R] Table multiple answers variables

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Tue Mar 15 15:24:04 CET 2011


try the following:

DF <- as.data.frame(matrix(sample(2, 120, TRUE), 10, 12))

Results <- data.frame(
     var = names(DF),
     count = colSums(DF == 1),
     percentage = colMeans(DF == 1)
)


I hope it helps.

Best,
Dimitris


On 3/15/2011 3:13 PM, Orvalho Augusto wrote:
> I have a dataset like this:
>
>     q25_1 q25_2 q25_3 q25_4 q25_5 q25_6 q25_7 q25_8 q25_9 q25_10 q25_11 q25_12
> 1      2     2     1     1     2     1     2     1     1      2      1      3
> 2      2     2     2     1     2     1     2     1     1      2      1      2
> 3      2     1     1     1     2     1     2     1     2      2      1      2
> 4      2     2     1     1     2     2     2     1     1      1      2      2
> 5      2     2     1     1     2     3     2     1     2      2      2      2
> 6      2     2     1     1     2     2     2     1     1      1      2      2
> 7      2     2     1     1     2     3     2     1     2      2      3      2
> 8      2     2     1     1     1     1     2     1     1      1      2      2
> 9      2     2     1     1     2     2     2     1     2      2      2      2
> 10     2     2     1     1     2     2     2     1     2      1      2      2
>
> where those q25_ variables are answers (coded 1 for Yes 2 for No). Now I
> need a way to table counting the ones foreach variables like
>
> var        count    percentage
> q25_1     20          80
> q25_2     34          85
> ...
>
> Is it possible ir R?
>
> For who used stata might use mrtab.
>
> Thank you
> Orvalho
>
> 	[[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.
>

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/



More information about the R-help mailing list