OK. Then using aggregate(): > data$yes <- ifelse(data$response=="yes", 1, 0) > data$no <- ifelse(data$response=="no", 1, 0) > dataresp <- aggregate(cbind(no, yes)~region+district, data, sum) > dataresp[,3:4] <- dataresp[,3:4]/rowSums(dataresp[,3:4]) > # or dataresp[,3:4] <- prop.table(as.matrix(dataresp[,3:4]), 1) > dataresp region district no yes 1 A d 0.5 0.5 2 A e 0.0 1.0 3 B f 0.5 0.5 4 B g 0.5 0.5 5 C h 0.5 0.5 6 C i 0.0 1.0 7 C j 1.0 0.0
David From: Peter Maclean [mailto:pmaclean2...@yahoo.com] Sent: Sunday, November 10, 2013 12:52 PM To: dcarl...@tamu.edu Subject: Re: [R] Cross Tabulation Thanks. But I am creating lots of tables and I need Regions and Districts to appear so as to avoid to much editing. Peter Maclean Department of Economics UDSM On Sunday, November 10, 2013 12:32 PM, David Carlson <dcarl...@tamu.edu> wrote: The simplest would be to create a variable combining region and district: > data$region_district <- with(data, paste(region, district)) > prop.table(xtabs(~region_district+response, data), 1) response region_district no yes A d 0.5 0.5 A e 0.0 1.0 B f 0.5 0.5 B g 0.5 0.5 C h 0.5 0.5 C i 0.0 1.0 C j 1.0 0.0 ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Peter Maclean Sent: Sunday, November 10, 2013 12:06 AM To: r-help@r-project.org Subject: Re: [R] Cross Tabulation #Would like to create a cross-table (Region, district, response) and #(Region, district, cost. The flat table function does not look so good region <- c("A","A","A","A","B","B", "B", "B", "C","C", "C", "C") district <- c("d","d","e","e","f","f", "g", "g", "h","h", "i", "j") response <- c("yes", "no", "yes", "yes", "no", "yes", "yes", "no", "yes", "no", "yes","no") cost <- runif(12, 5.0, 9) var <- c("region", "response", "district") data <- data.frame(region, district, response, cost) var1 <- c("region", "district", "response") var2 <- c("region", "district", "cost") data1 <- data[var1] #This look okay with(data, aggregate(x=cost, by=list(region, district), FUN="mean")) #This does not look good #How do i remove the NaN or create a better one prop.table(ftable(data1, exclude = c(NA, NaN)), 1) prop.table(ftable(xtabs(~region + district+ response, data=data)),1) Peter Maclean Department of Economics UDSM [[alternative HTML version deleted]] ______________________________________________ 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. ______________________________________________ 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.