A.K., thanks for your reply. I'm getting an error at res2: Error: unexpected symbol in "res2 <- within(as.data.frame(res1),`Count of Case ID` <- dcast(FLSAdata_melt, ViolationDesc + ReasonDesc ~ variable, length, margins=TRUE)[,3])[,c(4,1:3)] colnames" >
I've tried a couple of modifications, but obviously don't know what I'm doing because I haven't fixed it. If anything comes to you in the meantime, please advise. Thanks. -----Original Message----- From: arun [mailto:smartpink...@yahoo.com] Sent: Tuesday, February 18, 2014 2:28 AM To: r-help@r-project.org Cc: Crombie, Burnette N Subject: Re: [R] count and sum simultaneously in R pivot table Hi, Check if this works: library(reshape2) res1 <- acast(FLSAdata_melt, ViolationDesc + ReasonDesc ~ variable, sum, margins=TRUE)[,-4] res2 <- within(as.data.frame(res1),`Count of Case ID` <- dcast(FLSAdata_melt, ViolationDesc + ReasonDesc ~ variable, length, margins=TRUE)[,3])[,c(4,1:3)] colnames(res2)[2:4] <- paste("Sum of",colnames(res2)[2:4]) rownames(res2)[length(rownames(res2))] <- "Grand Total" indx <- grepl("all",rownames(res2)) ord1 <- unlist(tapply(seq_len(nrow(res2)),list(cumsum(c(TRUE,diff(indx)<0))),FUN=function(x) c(tail(x,1),head(x,-1)) ),use.names=FALSE) res3 <- res2[ord1,] rownames(res3) <- gsub("\\_\\(all\\)","",rownames(res3)) A.K. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.