tags:

views:

80

answers:

2

Hi!

I have to make a report of a survey where we have a question like this (all answers are checkboxes).

What is you favorite cake(s) (please choose more than one):
[] Tiramisù
[] Carrot Cake
[] Cupcake

Then the survey software exports into CSV like this:

"username","likes_tiramisu","likes_carrotcake","likes_cupcake"
"test01",1,1,1
"test02",0,1,1
"test03",0,1,0
"test04",0,0,1

I would like to make a barplot where every histogram represent the frequency of every cake. How can I combine a table like this:

"likes_tiramisu"  "likes_carrotcake"  "likes_cupcake"
               1                   3                3

Could I just solve it with sum of all elements in each column? Is it conceptually valid?

+2  A: 

Suppose your data is in the file: survey.csv

Then to create a barplot use the following commands:

#Read in the data
d = read.table("survey.csv", sep=",", header=TRUE)

#Need to skip the username column, so d[,2:4]
#Use apply to calculate the totals in your table
barplot(apply(d[,2:4], 2, sum))

HTH

csgillespie
Colin, thank you for the tip!!I have a small issue because in the columns I have also NA. Is there a command to treat NA as 0?
Libo Cannici
Ok I found it: barplot(apply(d[,2:4], 2, sum, na.rm=TRUE)) I love R more and more
Libo Cannici
There is a dedicated function to sum columns - `colSums`. You can use `colSums(d[,2:4])` or `colSums(d[2:4])` and with `NA` removing `colSums(d[,2:4], na.rm=TRUE)` or `colSums(d[2:4], na.rm=TRUE)`.
Marek
+1  A: 

A ggplot2 approach would look like this:

library(ggplot2)

data.melt <- melt(data, id = "username")
qplot(variable, value, data = data.melt, geom = "bar", stat = "identity")
JoFrhwld
I didn't know the melt command in ggplot2. Looks quite powerful! Thank you a lot for this example
Libo Cannici
melt() is actually from the reshape package, which is loaded by ggplot2.
JoFrhwld