# [R] Frequencies from a matrix - spider from frequencies

Dieter Menne dieter.menne at menne-biomed.de
Mon Mar 15 09:25:46 CET 2010

```
Uwe Dippel-2 wrote:
>
> Here comes my 'problem', over which I have sweated for the last 2 hours:
> My data are of a matrix 10x31, Likert Scale (1-5). 10 questions, 31
> respondents. Now, I want to display the frequencies per question. I have
> for (in in 1.10) print (table(learn[,i]))
>

Dennis has shown one way to do it. I personally prefer to arrange the data
in the "long" format shown below from the beginning, because it is much more
flexible when I want to derive summaries and plot the data:

nsubj = 5
nquest= 4
d = matrix(as.integer(runif(nsubj*nquest,1,6)),nrow=nquest)
colnames(d) = paste("subj",1:nsubj,sep="")
rownames(d) = paste("quest",1:nquest,sep="")
# These data are in the wide format

# Convert data to the "long" format. It is much more flexible,
# for example when you have missing data, and is the format of choice
# when data are stored in a database.
#
dframe = data.frame(
quest = rep(rownames(d),nsubj),
subj  = rep(colnames(d), nquest),
resp  = as.vector(d)
)
dframe
# Now we have the data in the long format, and the world is our limit
# Give it a first try with xtabs
xtabs(resp~quest+subj,data=dframe)
# oops, not that, that is the original
# Try ftable: looks good
ftable(resp~quest,data=dframe)

For the spider see ?star or