An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20140805/2c20205b/attachment.pl>
extract descriptive stats for categorial data from dataframe
5 messages · Jim Lemon, arun, D. Alain
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20140805/195b672f/attachment.pl>
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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20140805/a2b209a5/attachment.pl>