Advertisement
Vermiculus

R Lab 9

Apr 23rd, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.77 KB | None | 0 0
  1. # R Lab
  2. # 18/4/2012
  3. # Sean Allred
  4.  
  5. path <- "~/cs/R/t/test2.txt"
  6. data <- read.table(path)
  7. types <- unique(data[[2]])
  8. months <- unique(data[[3]])
  9.  
  10. mat <- matrix(nrow=length(types), ncol=length(months))
  11. rownames(mat) = types
  12. colnames(mat) = months
  13.  
  14. for (month in 1:length(months)) {
  15.     for (type in 1:length(types)) {
  16.         # Set the failsafe value
  17.         value <- 0
  18.        
  19.         # Try setting value to the sum of the relevant data (an error is thrown if there is no relevant data)
  20.         try(value <- sum(subset(data, V3 == months[month] & V2 == types[type])[1]), silent=TRUE)
  21.        
  22.         # insert the sum into the matrix appropriately
  23.         mat[type, month] <- value
  24.     }
  25. }
  26.  
  27. 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