# [R] frequency table-visualization for complex categorical variables

Mon Feb 25 21:31:15 CET 2013

```Hello,

I disagree with the way you've sorted the matrix, like this all A's
become first, then B's, etc, irrespective of the respondents. Each row
is a respondent, and the rows should be kept intact, but with a
different ordering. To this effect, use order():

z <- y[order(y[,1], y[,2], y[,3]), ]

Then use the rest of your code.

Or, which would save us the sorting, paste the rows elements together
directly from matrix 'y' and use the fact that table() sorts its output.

w2 <- apply( y , 1 , paste0 , collapse = "" )
table(w2)

Hope this helps,

Em 25-02-2013 18:32, Anthony Damico escreveu:
> in the future, please provide R code to re-create some example data :)
> http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-examplefor
> more detail..
>
>
>
> # create a data table with three unique columns' values..
> # treat these values just like letters
> x <-
>      cbind(
>          sample( LETTERS[1:6] , 100 , replace = TRUE ) ,
>          sample( LETTERS[1:6] , 100 , replace = TRUE ) ,
>          sample( LETTERS[1:6] , 100 , replace = TRUE )
>      )
>
> # look at x.. this is good data i hope?
> x
>
> # convert this to a matrix
> y <- as.matrix( x )
>
> # i don't think you care about ordering, so sort left-to-rightwards
> z <- apply( y , 2 , sort )
>
> # look at your results
> z
>
> # paste these results together across the matrix
> w <- apply( z , 1 , paste0 , collapse = "" )
>
> # count the final distinct results
> table( w )
>
>
>
>
> On Mon, Feb 25, 2013 at 1:04 PM, Niklas Fischer
> <niklasfischer980 at gmail.com>wrote:
>
>> Dear R users,
>>
>> I have three questions measuring close relationships.
>> The questions are same and the respondents put the answer in order.
>>
>> I'd like to examine the pattern of answers and visualize it.
>>
>> For example q1 (A,B,C,D,E) and q2 and q3 are the same. If the respondents
>> selects A B C (so BCA or BAC or CBA or CAB), I'd like to construct
>> frequency table for ABC and other combinations for example DEF.
>>
>>
>> Unfortunately, there are many answers, and three-way contingency table
>> includes lots of cells which make it diffucult to interpret and requires
>> lots of extra work to organize data.
>>
>> What is the best way to construct fruequency table of these kind of
>> variables and to visulize the results with the most simple form
>>
>>
>> All the bests,
>> Niklas
>>
>>          [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help