[R] Cross Tabulation

David Carlson dcarlson at tamu.edu
Sun Nov 10 19:32:45 CET 2013

The simplest would be to create a variable combining region and

> data$region_district <- with(data, paste(region, district))
> prop.table(xtabs(~region_district+response, data), 1)
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-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of Peter Maclean
Sent: Sunday, November 10, 2013 12:06 AM
To: r-help at r-project.org
Subject: Re: [R] Cross Tabulation

#Would like to create a cross-table (Region, district, response)
#(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",
district <- c("d","d","e","e","f","f", "g", "g", "h","h", "i",
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),
#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,

Peter Maclean
Department of Economics
	[[alternative HTML version deleted]]

R-help at r-project.org mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible

More information about the R-help mailing list