[R] Generate Crosstab in R

arun smartpink111 at yahoo.com
Wed Apr 9 18:18:11 CEST 2014


Hi,
Try:

dat <- structure(list(Custom = c("Judi", "Judi", "Ben", "Tom", "Tom", 
"Bill", "Lindy", "Shary", "Judu", "Judu", "Billy", "Tommy", "Tommy", 
"Benjum", "Linda", "Shiry"), Gender = c("Female", "Female", "Male", 
"Male", "Male", "Male", "Female", "Female", "Female", "Female", 
"Male", "Male", "Male", "Male", "Female", "Female"), Product = c("A", 
"B", "A", "A", "B", "B", "A", "B", "A", "B", "A", "A", "B", "B", 
"A", "B"), Payment = c("Credit Card", "Credit Card", "Cash", 
"Cash", "Cash", "Credit Card", "Cash", "Credit Card", "Credit Card", 
"Credit Card", "Cash", "Cash", "Cash", "Credit Card", "Cash", 
"Credit Card")), .Names = c("Custom", "Gender", "Product", "Payment"
), class = "data.frame", row.names = c(NA, -16L))

 dat1 <- within(dat, Categ <- ave(Product, Custom, FUN= function(x) if(length(x)>1) "A and B" else x))

 library(reshape2)
 res <- acast(dat1,Categ~Gender+Payment,length,value.var="Categ") #or dcast()

res[2,] <- res[2,]/2 
res 


A.K.


Hello experts, I am a beginner of R and need your kind help for a R question. Any advice will be greatly appreciated. I have a sample data set like below: Customs purchase either product A or B or both using either Credit card or Cash. I would like to summarize the data as a crosstab in R ---- show how many customs purchase product A only or product B only or product A and B using either credit card or cash. Is that possible in R? Thank you very much for your time and help. Customer_Sample.xlsx




More information about the R-help mailing list