[R] How to apply table() on subdata and stack outputs

Sean Zhang
2009-02-11T18:34:00Z

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?

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]]