Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # R Lab
- # 18/4/2012
- # Sean Allred
- path <- "~/cs/R/t/test2.txt"
- data <- read.table(path)
- types <- unique(data[[2]])
- months <- unique(data[[3]])
- mat <- matrix(nrow=length(types), ncol=length(months))
- rownames(mat) = types
- colnames(mat) = months
- for (month in 1:length(months)) {
- for (type in 1:length(types)) {
- # Set the failsafe value
- value <- 0
- # Try setting value to the sum of the relevant data (an error is thrown if there is no relevant data)
- try(value <- sum(subset(data, V3 == months[month] & V2 == types[type])[1]), silent=TRUE)
- # insert the sum into the matrix appropriately
- mat[type, month] <- value
- }
- }
- barplot(mat, beside=TRUE, legend.text=TRUE, xlab="Months", ylab="Money Spent (USD)", main="Spending", col=rainbow(length(types)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement