views:

67

answers:

2

Hi again all,

I have another newbie question;

lets say i have a set of numbers

graph_val <- c(4,2,3,4,1,1,9)

and i need to create a frequency table of them against this scale

           1            2            3            4            5            9 
 "Very Poor"       "Poor"    "Average"       "Good"  "Very Good" "Don't Know" 

Essentially what i want to know is how do i get a table into this format

 "Very Poor"       "Poor"    "Average"       "Good"  "Very Good" "Don't Know"
           2            1            1            1            0            1 

or at the very least

           1            2            3            4            5            9
           2            1            1            1            0            1 

And i can add the labels in later using names.arg with barplot 2.

I've been on this for most of the day, after this its clear sailing for the rest of my automation job. I thought i was on the right track with tabulate but couldn't quite get there.

Cheers in advanced.

+1  A: 

The following from "An Introduction to R" directly answers your question:

http://cran.r-project.org/doc/manuals/R-intro.html#Frequency-tables-from-factors

Michael Goldshteyn
The link you provided is not exactly the most helpful for his purpose. At least, not without first explaining what a factor is. http://cran.r-project.org/doc/manuals/R-intro.html#Factors and ?factor I think are much more appropriate.
Brandon Bertelsen
+2  A: 

First you need to factor your data. Think of a factor exactly in the way that you would think of a categorical variable. Levels tells it what to expect, labels gives it a pretty name.

graph_val <- factor(graph_val, levels=c(1,2,3,4,5,9), labels=strsplit('
Very Poor
Poor
Average
Good
Very Good
Don\'t Know
', '\n')[[1]][-1]) 
## Take note of the escape character in Don\'t Know

summary(graph_val)

If you need percentages, you can do something like this:

summary(graph_val)/length(graph_val)\

Or this:

round(summary(graph_val)/length(graph_val),2)
Brandon Bertelsen
Cheers for your help again, I seriously don't know how i could have got this far with this project if it wasn't for people like you helping :P
Cam B