extract descriptive stats for categorial data from dataframe

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

extract descriptive stats for categorial data from dataframe

Alain D.
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]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: extract descriptive stats for categorial data from dataframe

Jim Lemon
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

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: extract descriptive stats for categorial data from dataframe

Alain D.
Thank you Jim,

this is a good step in the right direction. But is there also a way to get the
output in a nice dataframe with VAR1 to VAR 1 to 9 as rows?

Best wishes

Alain

> Jim Lemon <[hidden email]> hat am 5. August 2014 um 14:35 geschrieben:
>
>
> 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
>
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: extract descriptive stats for categorial data from dataframe

arun kirshna
In reply to this post by Alain D.
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. <[hidden email]> 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]]

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: extract descriptive stats for categorial data from dataframe

Alain D.
Great! This is exactly what I was locking for!

Thank you very much, arun.

Best wishes

Alain

> arun <[hidden email]> hat am 5. August 2014 um 16:47 geschrieben:
>
>
> 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. <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.
>
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.