[R] count each answer category in each column

arun smartpink111 at yahoo.com
Fri Apr 19 19:50:33 CEST 2013


Hi,
In the example given, there is only one female and one male for ID "A".
dat1<- read.table(text="
ID   Gender   Age   Rate
 A       Female    0-10   Good
  A      Male        0-10   Good
  B       Female     11-20  Bad
 B       Male         11-20  Bad
   C       Male         >20     N/A
",sep="",header=TRUE,stringsAsFactors=FALSE,na.strings="N/A")
res<-  lapply(split(dat1[,-1],dat1$ID),function(x) lapply(seq_len(ncol(x)) ,function(i){x1<- as.data.frame(table(x[,i],useNA="always"));colnames(x1)[2]<- colnames(x)[i];x1}))


 res[1]
$A
$A[[1]]
    Var1 Gender
1 Female      1
2   Male      1
3   <NA>      0

$A[[2]]
  Var1 Age
1 0-10   2
2 <NA>   0

$A[[3]]
  Var1 Rate
1 Good    2
2 <NA>    0


library(plyr)
 res1<-  lapply(split(dat1[,-1],dat1$ID),function(x) join_all(lapply(seq_len(ncol(x)),function(i){x1<- as.data.frame(table(x[,i],useNA="always"));colnames(x1)[2]<- colnames(x)[i];x1}),type="full"))
res1
$A
#    Var1 Gender Age Rate
#1 Female      1  NA   NA
#2   Male      1  NA   NA
#3   <NA>      0   0    0
#4   0-10     NA   2   NA
#5   Good     NA  NA    2

#$B
 #   Var1 Gender Age Rate
#1 Female      1  NA   NA
#2   Male      1  NA   NA
#3   <NA>      0   0    0
#4  11-20     NA   2   NA
#5    Bad     NA  NA    2

#$C
 # Var1 Gender Age Rate
#1 Male      1  NA   NA
#2 <NA>      0   0    1
#3  >20     NA   1   NA
A.K.



________________________________
 From: Ye Lin <yelin at lbl.gov>
To: arun <smartpink111 at yahoo.com> 
Cc: R help <r-help at r-project.org> 
Sent: Friday, April 19, 2013 1:25 PM
Subject: Re: [R] count each answer category in each column
 


Thanks A.K


Is it possible to apply this to a more complicated situation , for example, I have an ID column for each row, say:


ID   Gender   Age   Rate 
 A       Female    0-10   Good
  A      Male        0-10   Good
  B       Female     11-20  Bad
 B       Male         11-20  Bad
   C       Male         >20     N/A



When return the results indicate how many answers are from each ID, say for gender, we have 2 female, and 1 from category A and 1 from category B??? Thanks.

Ye




On Thu, Apr 18, 2013 at 4:04 PM, arun <smartpink111 at yahoo.com> wrote:

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 <yelin at lbl.gov>
>To: R help <r-help at 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 at 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.
>
>



More information about the R-help mailing list