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@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.

Reply via email to