If the format is not a problem, you could also use: dat1<-read.table(text=" Gender Age Rate Female 0-10 Good Male 0-10 Good Female 11-20 Bad Male 11-20 Bad Male >20 N/A ",sep="",header=TRUE) res<-summary(dat1) res # Gender Age Rate #Female:2 0-10 :2 Bad :2 # Male :3 11-20:2 Good:2 # >20 :1 N/A :1 library(stringr) library(plyr) res1<-mutate(data.frame(Categ=rep(c("Gender","Age","Rate"),c(2,3,3)),na.omit(str_split_fixed(str_trim(str_replace(res,":"," "))," ",2)),stringsAsFactors=FALSE),X2=as.numeric(X2))
res1 # Categ X1 X2 #1 Gender Female 2 #2 Gender Male 3 #3 Age 0-10 2 #4 Age 11-20 2 #5 Age >20 1 #6 Rate Bad 2 #7 Rate Good 2 #8 Rate N/A 1 A.K. ----- Original Message ----- From: arun <smartpink...@yahoo.com> To: Ye Lin <ye...@lbl.gov> Cc: R help <r-help@r-project.org> Sent: Thursday, April 18, 2013 7:04 PM Subject: Re: [R] count each answer category in each column Hi, Try this: Assuming that "table" is "data.frame" dat1<-read.table(text=" Gender Age Rate Female 0-10 Good Male 0-10 Good Female 11-20 Bad Male 11-20 Bad Male >20 N/A ",sep="",header=TRUE,stringsAsFactors=FALSE,na.strings="N/A") lapply(seq_len(ncol(dat1)),function(i) {x1<-as.data.frame(table(dat1[,i],useNA="always"));colnames(x1)[2]<-colnames(dat1)[i];x1}) #[[1]] # Var1 Gender #1 Female 2 #2 Male 3 #3 <NA> 0 #[[2]] # Var1 Age #1 0-10 2 #2 11-20 2 #3 >20 1 #4 <NA> 0 #[[3]] # Var1 Rate #1 Bad 2 #2 Good 2 #3 <NA> 1 A.K. ----- Original Message ----- From: Ye Lin <ye...@lbl.gov> To: R help <r-help@r-project.org> Cc: Sent: Thursday, April 18, 2013 6:46 PM Subject: [R] count each answer category in each column Hey, Is it possible that R can calculate each options under each column and return a summary table? Suppose I have a table like this: Gender Age Rate Female 0-10 Good Male 0-10 Good Female 11-20 Bad Male 11-20 Bad Male >20 N/A I want to have a summary table including the information that how many answers in each category, sth like this: X Gender Male 3 Female 2 N/A 0 X Age 0-10 2 11-20 2 >20 1 N/A 0 X Rate Good 2 Bad 2 N/A 1 So basically I want to calculate, in each column, how many people choose each answer, including N/A. I know I can do it in Excel in a very visualized way, but is there anyway to do it in R in a robust way if I have a fairly large dataset. Thanks! [[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.