[R] Generate Crosstab in R
arun
smartpink111 at yahoo.com
Wed Apr 9 21:47:06 CEST 2014
Hi,
Try:
datNew <- read.csv("customer_samples.csv",stringsAsFactors=FALSE)
#I could reproduce similar error message with:
dat[] <- lapply(dat,as.factor)
dat1 <- within(dat, Categ <- ave(Product, Custom, FUN= function(x) if(length(x)>1) "A and B" else x))
#Warning messages:
1: In `[<-.factor`(`*tmp*`, i, value = "A and B") : invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, i, value = "A and B") : invalid factor level, NA generated
3: In `[<-.factor`(`*tmp*`, i, value = "A and B") : invalid factor level, NA generated
4: In `[<-.factor`(`*tmp*`, i, value = "A and B") : invalid factor level, NA generated
A.K.
Hello A.K. , Thank you very much for your reply. I tried the following codes but got some warning messages: ------------------------- Codes I tried --------------
dat <- read.csv ("customer samples.csv") 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 --------------------------------- Waring messages I got: 1: In '[<-.factor' ('*tmp*', i, value = "A and B"): invalid factor level, NA generated 2: In '[<-.factor' ('*tmp*', i, value = "A and B"): invalid factor level, NA generated 3: In '[<-.factor' ('*tmp*', i, value = "A and B"): invalid factor level, NA generated 4: In '[<-.factor' ('*tmp*', i, value = "A and B"): invalid factor level, NA generated ------------------------------------------------- Could you please help me out? Thanks a lot!
On Wednesday, April 9, 2014 12:18 PM, arun <smartpink111 at yahoo.com> wrote:
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