Quantcast

Table orderd by frequencies

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Table orderd by frequencies

Kunzler, Andreas
Dear List,

I try to order the output of a table by the frequencies of the vector I
am look at.

The object I am looking at is a factor with a lot of levels that were
named only once.

Therefore it would be much easier to order the output by the frequencies
of the levels.

E.g.
> levels(a)
"a" "b" "c" "d"

Preferred outcome:
table(a)

b  c a d
10 5 1 1

Thank you

______________________________________________
[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
|  
Report Content as Inappropriate
star

Re: Table orderd by frequencies

Dimitris Rizopoulos
check the following:

a <- factor(sample(letters[1:4], 50, TRUE))

freqs <- table(a)
ind <- order(freqs, decreasing = TRUE)

# reorder the frequencies in the table
freqs[ind]

# reorder the levels of the factor
levels(a) <- names(freqs)[ind]
table(a)


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://perswww.kuleuven.be/dimitris_rizopoulos/


----- Original Message -----
From: "Kunzler, Andreas" <[hidden email]>
To: <[hidden email]>
Sent: Tuesday, July 22, 2008 9:59 AM
Subject: [R] Table orderd by frequencies


> Dear List,
>
> I try to order the output of a table by the frequencies of the
> vector I
> am look at.
>
> The object I am looking at is a factor with a lot of levels that
> were
> named only once.
>
> Therefore it would be much easier to order the output by the
> frequencies
> of the levels.
>
> E.g.
>> levels(a)
> "a" "b" "c" "d"
>
> Preferred outcome:
> table(a)
>
> b  c a d
> 10 5 1 1
>
> Thank you
>
> ______________________________________________
> [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.
>


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

______________________________________________
[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
|  
Report Content as Inappropriate
star

Re: Table orderd by frequencies

Barry Rowlingson
In reply to this post by Kunzler, Andreas
2008/7/22 Kunzler, Andreas <[hidden email]>:

> Dear List,
>
> I try to order the output of a table by the frequencies of the vector I
> am look at.
>
> The object I am looking at is a factor with a lot of levels that were
> named only once.
>
> Therefore it would be much easier to order the output by the frequencies
> of the levels.
>
> E.g.
>> levels(a)
> "a" "b" "c" "d"
>
> Preferred outcome:
> table(a)
>
> b  c a d
> 10 5 1 1
>

 Can't you just sort the table:

 > set.seed(123)
 > a=as.factor(sample(letters[1:4],16,replace=TRUE))
 > sort(table(a),dec=TRUE)
 a
 d b c a
 6 4 4 2

Barry

______________________________________________
[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.
Loading...