Pergunta

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.

Foi útil?

Solução

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)

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top