[R] extract descriptive stats for categorial data from dataframe

arun smartpink111 at yahoo.com
Tue Aug 5 16:47:02 CEST 2014


You could try:
lv <- levels(unique(unlist(df)))
as.data.frame(t(apply(df, 2, function(x) table(factor(x, levels=lv)))))
    +  - 0
i1 10  0 0
i2 10  0 0
i3  0 10 0
i4  0  9 1
i5 10  0 0
i6  1  9 0
i7  9  0 1
i8  4  2 4
i9  7  1 2
A.K.




On Tuesday, August 5, 2014 5:36 AM, Alain D. <dialvac-r at yahoo.de> wrote:
Dear R-List,

I want to have descriptive stats in a special form and cannot figure out a nice
solution.

df<-as.data.frame(cbind(i1=rep("+"),i2=rep("+",10),i3=rep("-",10),i4=c(rep("-",2),"0",rep("-",7)),i5=rep("+",10),i6=c(rep("-",9),"+"),i7=c(rep("+",4),"0",rep("+",5)),i8=c(rep(0,4),rep("+",3),"-","+","-"),i9=c(rep("+",5),"-",rep("+",2),rep(0,2))))

now I want the categories as var labels arranged in cols with IDs as first col
and then frequencies for each category. Something like this:

var   +   -    0
i1    10  0    0
i2    10  0    0
i3     0 10    0
i4     0  9    1
i5    10  0    0
i6     1  9    0
i7     9  0    1
i8     4  2    4
i9     7  1    2

I tried different combinations of

freq<-as.data.frame(df<-lapply(df,table))

but was not very successful.

I would be very thankful for an easy solution which is probably to obvious for
me to spot.

Thank you very much.

Best wishes

Alain
    [[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