Shoaaib Mehmood
2007-Nov-16 10:21 UTC
[R] sorting factor levels by data frequency of levels
using an example from r online help> state <- c("tas", "sa", "qld", "nsw", "nsw", "nt", "wa", "wa","qld", "vic", "nsw", "vic", "qld", "qld", "sa", "tas", "sa", "nt", "wa", "vic", "qld", "nsw", "nsw", "wa", "sa", "act", "nsw", "vic", "vic", "act")>statefac<-factor(state)now if i use levels function to print factor levels they are shown sorted by alphabetical order. the output is shown below>levels(statefac)[1] "act" "nsw" "nt" "qld" "sa" "tas" "vic" "wa" I WANT THE FACTOR LEVELS TO BE SORTED IN ORDER OF DESCENDING DATA FREQUENCY IN EACH LEVEL. IN OTHER WORDS I WANT THE LEVEL WHICH HAS THE HIGHEST FREQUENCY IN STATE TO APPEAR FIRST, FOLLOWED BY ONE WHICH HAS THE SECOND HIGHEST FREQUENCY AND SO ON. Any help will be appreciated -- Regards, Rana Shoaaib Mehmood
Dimitris Rizopoulos
2007-Nov-16 10:32 UTC
[R] sorting factor levels by data frequency of levels
one way is the following: tb <- table(state) statefac <- factor(state, levels = names(tb[order(tb, decreasing = TRUE)])) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student 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://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Shoaaib Mehmood" <shoaaib at gmail.com> To: <r-help at stat.math.ethz.ch>; <r-help at r-project.org> Sent: Friday, November 16, 2007 11:21 AM Subject: [R] sorting factor levels by data frequency of levels> using an example from r online help > >> state <- c("tas", "sa", "qld", "nsw", "nsw", "nt", "wa", "wa", > "qld", "vic", "nsw", "vic", "qld", "qld", "sa", "tas", > "sa", "nt", "wa", "vic", "qld", "nsw", "nsw", "wa", > "sa", "act", "nsw", "vic", "vic", "act") >>statefac<-factor(state) > > now if i use levels function to print factor levels they are shown > sorted by alphabetical order. the output is shown below > >>levels(statefac) > [1] "act" "nsw" "nt" "qld" "sa" "tas" "vic" "wa" > > I WANT THE FACTOR LEVELS TO BE SORTED IN ORDER OF DESCENDING DATA > FREQUENCY IN EACH LEVEL. IN OTHER WORDS I WANT THE LEVEL WHICH HAS > THE > HIGHEST FREQUENCY IN STATE TO APPEAR FIRST, FOLLOWED BY ONE WHICH > HAS > THE SECOND HIGHEST FREQUENCY AND SO ON. > > Any help will be appreciated > > > -- > Regards, > Rana Shoaaib Mehmood > > ______________________________________________ > 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Hello R people I have a column of numeric values that are grouped in blocks of 25 and to be centered and scaled within each block. (That is subtract the mean and divide by the standard deviation.) Is there a neater way to do this? i.e not using a loop? Example looped code: testdata<-1:100 csvalues<-NULL for (i in 1:(length(testdata)/25)) { st<-(i-1) * 25 + 1 sel<-st:(st+24) selvals<-testdata[sel] csvalues<-c(csvalues, scale(selvals, scale=sd(selvals))) } Thanks for any help. John Seers --- John Seers Institute of Food Research Norwich Research Park Colney Norwich NR4 7UA tel +44 (0)1603 251497 fax +44 (0)1603 507723 e-mail john.seers at bbsrc.ac.uk e-disclaimer at http://www.ifr.ac.uk/edisclaimer/ Web sites: www.ifr.ac.uk www.foodandhealthnetwork.com
Try this: c(apply(matrix(testdata, 25), 2, scale)) On Nov 16, 2007 9:20 AM, john seers (IFR) <john.seers at bbsrc.ac.uk> wrote:> > Hello R people > > I have a column of numeric values that are grouped in blocks of 25 and > to be centered and scaled within each block. (That is subtract the mean > and divide by the standard deviation.) > > Is there a neater way to do this? i.e not using a loop? > > Example looped code: > > testdata<-1:100 > csvalues<-NULL > for (i in 1:(length(testdata)/25)) { > st<-(i-1) * 25 + 1 > sel<-st:(st+24) > selvals<-testdata[sel] > csvalues<-c(csvalues, scale(selvals, scale=sd(selvals))) > } > > Thanks for any help. > > > John Seers > > > > > --- > > John Seers > Institute of Food Research > Norwich Research Park > Colney > Norwich > NR4 7UA > > > tel +44 (0)1603 251497 > fax +44 (0)1603 507723 > e-mail john.seers at bbsrc.ac.uk > e-disclaimer at http://www.ifr.ac.uk/edisclaimer/ > > Web sites: > > www.ifr.ac.uk > www.foodandhealthnetwork.com > > ______________________________________________ > 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. >
Ah, that is neat. Thanks. JS -----Original Message----- From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com] Sent: 16 November 2007 15:12 To: john seers (IFR) Cc: r-help at r-project.org Subject: Re: [R] Scaling a column in groups Try this: c(apply(matrix(testdata, 25), 2, scale)) On Nov 16, 2007 9:20 AM, john seers (IFR) <john.seers at bbsrc.ac.uk> wrote:> > Hello R people > > I have a column of numeric values that are grouped in blocks of 25 and> to be centered and scaled within each block. (That is subtract the > mean and divide by the standard deviation.) > > Is there a neater way to do this? i.e not using a loop? > > Example looped code: > > testdata<-1:100 > csvalues<-NULL > for (i in 1:(length(testdata)/25)) { > st<-(i-1) * 25 + 1 > sel<-st:(st+24) > selvals<-testdata[sel] > csvalues<-c(csvalues, scale(selvals, scale=sd(selvals))) } > > Thanks for any help. > > > John Seers > > > > > --- > > John Seers > Institute of Food Research > Norwich Research Park > Colney > Norwich > NR4 7UA > > > tel +44 (0)1603 251497 > fax +44 (0)1603 507723 > e-mail john.seers at bbsrc.ac.uk > e-disclaimer at http://www.ifr.ac.uk/edisclaimer/ > > Web sites: > > www.ifr.ac.uk > www.foodandhealthnetwork.com > > ______________________________________________ > 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. >
Shoaaib Mehmood wrote:> using an example from r online help > > >>state <- c("tas", "sa", "qld", "nsw", "nsw", "nt", "wa", "wa", > > "qld", "vic", "nsw", "vic", "qld", "qld", "sa", "tas", > "sa", "nt", "wa", "vic", "qld", "nsw", "nsw", "wa", > "sa", "act", "nsw", "vic", "vic", "act") > >>statefac<-factor(state) > > > now if i use levels function to print factor levels they are shown > sorted by alphabetical order. the output is shown below > > >>levels(statefac) > > [1] "act" "nsw" "nt" "qld" "sa" "tas" "vic" "wa" > > I WANT THE FACTOR LEVELS TO BE SORTED IN ORDER OF DESCENDING DATA > FREQUENCY IN EACH LEVEL. IN OTHER WORDS I WANT THE LEVEL WHICH HAS THE > HIGHEST FREQUENCY IN STATE TO APPEAR FIRST, FOLLOWED BY ONE WHICH HAS > THE SECOND HIGHEST FREQUENCY AND SO ON. >levels(state)<-levels(state)[rev(order(tabulate(state)))] Jim
?ave -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of john seers (IFR) > Sent: Friday, November 16, 2007 7:21 AM > To: r-help at r-project.org > Subject: [R] Scaling a column in groups > > > > Hello R people > > I have a column of numeric values that are grouped in blocks > of 25 and to be centered and scaled within each block. (That > is subtract the mean and divide by the standard deviation.) > > Is there a neater way to do this? i.e not using a loop? > > Example looped code: > > testdata<-1:100 > csvalues<-NULL > for (i in 1:(length(testdata)/25)) { > st<-(i-1) * 25 + 1 > sel<-st:(st+24) > selvals<-testdata[sel] > csvalues<-c(csvalues, scale(selvals, scale=sd(selvals))) } > > Thanks for any help. > > > John Seers > > > > > --- > > John Seers > Institute of Food Research > Norwich Research Park > Colney > Norwich > NR4 7UA > > > tel +44 (0)1603 251497 > fax +44 (0)1603 507723 > e-mail john.seers at bbsrc.ac.uk > e-disclaimer at http://www.ifr.ac.uk/edisclaimer/ > > Web sites: > > www.ifr.ac.uk > www.foodandhealthnetwork.com > > ______________________________________________ > 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. >
?reorder Try:> newstate <- reorder(state,state, length)Or> newstate <- reorder(state,state, function(x) -length(x) )Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Shoaaib Mehmood > Sent: Friday, November 16, 2007 3:22 AM > To: r-help at stat.math.ethz.ch; r-help at r-project.org > Subject: [R] sorting factor levels by data frequency of levels > > using an example from r online help > > > state <- c("tas", "sa", "qld", "nsw", "nsw", "nt", "wa", "wa", > "qld", "vic", "nsw", "vic", "qld", "qld", "sa", "tas", > "sa", "nt", "wa", "vic", "qld", "nsw", "nsw", "wa", > "sa", "act", "nsw", "vic", "vic", "act") > >statefac<-factor(state) > > now if i use levels function to print factor levels they are > shown sorted by alphabetical order. the output is shown below > > >levels(statefac) > [1] "act" "nsw" "nt" "qld" "sa" "tas" "vic" "wa" > > I WANT THE FACTOR LEVELS TO BE SORTED IN ORDER OF DESCENDING > DATA FREQUENCY IN EACH LEVEL. IN OTHER WORDS I WANT THE LEVEL > WHICH HAS THE HIGHEST FREQUENCY IN STATE TO APPEAR FIRST, > FOLLOWED BY ONE WHICH HAS THE SECOND HIGHEST FREQUENCY AND SO ON. > > Any help will be appreciated > > > -- > Regards, > Rana Shoaaib Mehmood > > ______________________________________________ > 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. >