[R] Frequencies from a matrix - spider from frequencies
William Dunlap
wdunlap at tibco.com
Mon Mar 15 18:41:40 CET 2010
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Uwe Dippel
> Sent: Monday, March 15, 2010 8:27 AM
> To: Dennis Murphy
> Cc: r-help at r-project.org
> Subject: Re: [R] Frequencies from a matrix - spider from frequencies
>
> Dennis Murphy wrote:
> >
> > The first part is straightforward. You didn't supply example data,
> > but it's easy to generate it oneself:
> >
> > likmat <- matrix(sample(1:5, 310, replace = TRUE), nrow = 31)
> > dim(likmat)
> > # [1] 31 10
> > frq <- t(apply(likmat, 2, table)) # 10 x 5 matrix,
> questions in rows
>
> Yours works, mine doesn't. As I wrote, newbie.
> The difference here is the naming of rows and columns. Your
> matrix looks
> like
> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
> [1,] 2 3 3 4 5 2 1 2 2 2
> [2,] 2 1 3 1 3 2 1 5 2 4
> while mine goes like this:
> V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
> 1 1 2 3 4 5 6 7 8 9 10
> 2 4 4 4 3 4 3 4 4 3 4
You probably have a "data.frame", not a "matrix". Look
at class(yourData) or str(yourData) to verify this.
Both are 2 dimensional structures but they differ in
many ways. This is why the posting guidelines ask
that you send a way another user can recreate your data
objects by copying and pasting R commands from your
mail to an R session.
However, the reason you and Dennis got different results
is probably that in your example not every column had the
same set of unique values ("levels") in it, hence the tables
produced for each column have a different number of entries
and apply() did not "simplify" its output to be a matrix.
You were lucky that you had different numbers of levels
in the various columns: if there had been the same number
then it would have silently pasted together misaligned outputs.
E.g.,
> d1 <- data.frame(x=c(11,12,12), y=c(12,13,13))
> apply(d1, 2, table) # incorrect results
x y
[1,] 1 1
[2,] 2 2
One way to deal with this is tell table() to use the same
set of levels to tabulate for each column as in
> apply(d1, 2, function(column)table(factor(column, levels=11:13)))
x y
11 1 0
12 2 1
13 0 2
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> and then the 'apply' gets me some
> > apply(m_learn, 2, table)
> $V1
> 1 3 4 5
> 3 5 12 9
> $V2
> respectively
> t(apply(m_learn, 2, table))
> V1 V2 V3 V4 V5 V6
> V7
> [1,] Integer,4 Integer,5 Integer,4 Integer,5 Integer,5
> Integer,5 Integer,6
> V8 V9 V10
> [1,] Integer,6 Integer,5 Integer,5
> , while your matrix results exactly in what I was looking for.
>
> I have attached the data as file, hoping for it to go through.
>
> >
> >
> >
> > The stars() function allows one to generate spider/radar
> plots as you
> > requested. Since there are several forms that it can take, I suggest
> > you run the example:
> >
> > example(stars)
> >
> > and mimic the one(s) you want.
>
> Neither was what I had hoped for. (I attach the sample,
> hoping for it to
> go through. It is made with gnumeric.)
>
> Thanks very much anyway, I really appreciate your help!
>
> Uwe
>
>
More information about the R-help
mailing list