extract descriptive stats for categorial data from dataframe
On Tue, 5 Aug 2014 11:36:36 AM Alain D. 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.
Hi Alain,
You can get pretty much what you want if your variables are all factors
with the same levels like this:
varlevels<-c("+","-","0","1")
df<-data.frame(
i1=factor(rep("+",10),levels=varlevels),
i2=factor(rep("+",10),levels=varlevels),
i3=factor(rep("-",10),levels=varlevels),
i4=factor(c(rep("-",2),"0",rep("-",7)),levels=varlevels),
i5=factor(rep("+",10),levels=varlevels),
i6=factor(c(rep("-",9),"+"),levels=varlevels),
i7=factor(c(rep("+",4),"0",rep("+",5)),levels=varlevels),
i8=factor(c(rep(0,4),rep("+",3),"-","+","-"),levels=varlevels),
i9=factor(c(rep("+",5),"-",rep("+",2),rep(0,2)),levels=varlevels))
library(prettyR)
describe(df,horizontal=TRUE,fname.space=10)
Jim