Skip to content
Back to formatted view

Raw Message

Message-ID: <49931E85.6030508@statistik.tu-dortmund.de>
Date: 2009-02-11T18:52:53Z
From: Uwe Ligges
Subject: How to apply table() on subdata and stack outputs
In-Reply-To: <97c8246d0902111034t28c52f93qdfb0868e7cabeed3@mail.gmail.com>

Sean Zhang wrote:
> Dear R helpers:
> 
> I am a R novice and have a question about using table() to extract
> frequences over many sub-datasets.
> A small example input dataframe and wanted output dataframe are provided
> below. The real data is very large so a for loop is what I try to avoid.
> 
> Can someone englithen me how to use sapply or the like to achieve it?


I'd simply use

   table(input.df)

or perhaps closer to the result you want:

  reshape(as.data.frame(table(input.df)), direction="wide",
          timevar="var_interest")


Uwe Ligges



> Many thanks in advance!
> 
> -Sean
> 
> #example input dataframe
> id <- c('tom', 'tom', 'tom', 'jack', 'jack', 'jack', 'jack')
> var_interest <- c("happy","unhappy", "", "happy", "unhappy", 'soso','happy')
> input.df <- data.frame(id=id, var_interest=var_interest)
> input.df
> wanted.df <-
> 
> #output dataframe I want
> id_unique <- c('tom','jack')
> happy_freq<-c(1,2)
> unhappy_freq<-c(1,1)
> soso_freq<-c(0,1)
> miss_freq<-c(1,0)
> output.df <-data.frame(id_unique=id_unique, happy_freq=happy_freq,
> unhappy_freq=unhappy_freq, soso_freq=soso_freq, miss_freq=miss_freq)
> output.df
> 
> 	[[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.