First of all, I really like R! Still being a newbie, I find things (the difficult ones) to be very simple. Alas, some 'simple' things still escape me. (Maybe the tutorials are often too much focused on the 'difficult' items??) 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 not found any better (any more simple) than for (in in 1.10) print (table(learn[,i])) And then, still, the scale is printed 10 times as well. I am sure, there is a better function, but I didn't find one. Actually, I would want the scale once, atop ('names'), and then the 10(questions) * 5 (length.of.scale) thereunder, like 1 2 3 4 5 1 3 4 2 1 2 2 5 9 2 1 4 3 4 4 6 1 3 .... And, now somewhat less of the ordinary, a spider/radar showing the number of responses for each question (circular axis, 10), with the frequencies as radial axes. That is, 5 polygons showing the frequencies of the responses per each question. Any help is appreciated, and my excuses for asking a simple question, Uwe
Dennis Murphy
2010-Mar-15 08:00 UTC
[R] Frequencies from a matrix - spider from frequencies
Hi: 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 As for the plot, ?stars 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. HTH, Dennis On Mon, Mar 15, 2010 at 12:24 AM, Uwe Dippel <udippel@uniten.edu.my> wrote:> First of all, I really like R! Still being a newbie, I find things (the > difficult ones) to be very simple. > Alas, some 'simple' things still escape me. (Maybe the tutorials are often > too much focused on the 'difficult' items??) > > 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 not > found any better (any more simple) than > for (in in 1.10) print (table(learn[,i])) > And then, still, the scale is printed 10 times as well. I am sure, there is > a better function, but I didn't find one. > Actually, I would want the scale once, atop ('names'), and then the > 10(questions) * 5 (length.of.scale) thereunder, like > 1 2 3 4 5 > 1 3 4 2 1 2 > 2 5 9 2 1 4 > 3 4 4 6 1 3 > .... > > And, now somewhat less of the ordinary, a spider/radar showing the number > of responses for each question (circular axis, 10), with the frequencies as > radial axes. That is, 5 polygons showing the frequencies of the responses > per each question. > > Any help is appreciated, and my excuses for asking a simple question, > > Uwe > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Dieter Menne
2010-Mar-15 08:25 UTC
[R] Frequencies from a matrix - spider from frequencies
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 > not found any better (any more simple) than > 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 http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=63 Dieter -- View this message in context: http://n4.nabble.com/Frequencies-from-a-matrix-spider-from-frequencies-tp1593012p1593062.html Sent from the R help mailing list archive at Nabble.com.
On 03/15/2010 06:24 PM, Uwe Dippel wrote:> ... > And, now somewhat less of the ordinary, a spider/radar showing the > number of responses for each question (circular axis, 10), with the > frequencies as radial axes. That is, 5 polygons showing the frequencies > of the responses per each question. >Hi Uwe, Here's one way to get your spider plot: ld1<-matrix(sample(1:5,310,TRUE),nrow=31) ld2<-apply(ld1,2,table) radial.plot(ld2,line.col=2:6,rp.type="p", radial.pos=seq(0,9*pi/5,by=pi/5), labels=paste("Q",1:10,sep=""),start=pi/2, clockwise=TRUE,main="Frequency of response by question") par(xpd=TRUE) legend(8,12,1:5,col=2:6,lty=1) par(xpd=FALSE) Quite a bit of overlap on the polygons, however. Jim