I have a bunch of data in a table (imported from csv) in the following format:
date classes score
9/1/11 french 34
9/1/11 english 34
9/1/11 french 34
9/1/11 spanish 34
9/2/11 french 34
9/2/11 english 34
9/3/11 spanish 34
9/3/11 spanish 34
9/5/11 spanish 34
9/5/11 english 34
9/5/11 french 34
9/5/11 english 34
Ignore the score column, it's not important.
I need a tally of the total number of students taking English or Spanish or french class based on date, ie. I need to first group it by date and then divide each day into further blocks based on language and plot it as a stacked bar chart so it looks like the following. Each bar represents a date and each cross section of a bar represents a single language.
I've figured out how to do this once I get the data in a matrix form where each row represents a date and every column an attribute (or language). So I assuming the data is in that form in a csv:
ie french english spanish
9/1/11 2 1 1
9/2/11 1 1 0
9/3/11 0 0 2
9/5/11 1 2 1
then I can do:
directory<-"C:\\test\\language.csv"
ourdata6<-read.csv(directory)
language<-as.matrix(ourdata6)
barchart(prop.table(language), horizontal=FALSE, auto.key = list(space='right',cex=.5,border=T,points=F, lines=F,lwd=5,text=c('french','spanish','enligsh'),cex=.6), main = list(label="Distribution of classes 10",cex=2.5), ylab = list(", cex=1.7),xlab.top=list("testing",cex=1.2))
The challenge is to get the data from the original format into the format I need.
I tried
a<-count(language, c("date", "classes"))
where it gives me the counts sorted by both but its in a vertical form
ie
9/1/11 french 2
9/1/11 english 1
9/1/11 spanish 1
etc...
I need to pivot this so it becomes a single row per date. Also if some of these might be zero so I need placeholders for them ie. the first column must correspond to french, the second must correspond to english for my current setup to work.
Any ideas on how to do this or if my approach with matrix + prop.table is even correct? Are there any simpler ways of doing this?