[R] Sorting a data frame by specifying a vector

arun smartpink111 at yahoo.com
Thu Oct 11 20:20:38 CEST 2012


HI,
In this case, specifying the factor levels would be easier.
Try this:
set.seed(1)
df <- data.frame(Season=rep(c("Summer","Fall","Winter","Spring"),4),Obs=
runif(length(rep(c("Summer","Fall","Winter","Spring"),4))))
df1<-within(df,{Season<-factor(Season,levels=c("Summer","Fall","Winter","Spring"))})
library(plyr)
df2<-ddply(df1,.(Season),function(x) x)
df2
#   Season        Obs
#1  Summer 0.26550866
#2  Summer 0.20168193
#3  Summer 0.62911404
#4  Summer 0.68702285
#5    Fall 0.37212390
#6    Fall 0.89838968
#7    Fall 0.06178627
#8    Fall 0.38410372
#9  Winter 0.57285336
#10 Winter 0.94467527
#11 Winter 0.20597457
#12 Winter 0.76984142
#13 Spring 0.90820779
#14 Spring 0.66079779
#15 Spring 0.17655675
#16 Spring 0.49769924


Just curious, in your reply, the Obs column has only 4 values.  Do you want to get the means???


A.K.



----- Original Message -----
From: ROLL Josh F <JRoll at Lcog.org>
To: 'arun' <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Thursday, October 11, 2012 1:42 PM
Subject: RE: [R] Sorting a data frame by specifying a vector

Sorry if I wasn't clear but the result I am looking for is as follows
#   Season       Obs
#1  Summer 0.2141001
#5  Summer 0.2141001
#9  Summer 0.2141001
#13 Summer 0.2141001
#3    Fall 0.6722337
#7    Fall 0.6722337
#11   Fall 0.6722337
#15   Fall 0.6722337
#2  Winter 0.9318599
#6  Winter 0.9318599
#10 Winter 0.9318599
#14 Winter 0.9318599
#4  Spring 0.1927715
#8  Spring 0.1927715
#12 Spring 0.1927715
#16 Spring 0.1927715

The process you describe does not get me there

Any other recommendations?

-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com] 
Sent: Thursday, October 11, 2012 10:33 AM
To: ROLL Josh F
Cc: R help
Subject: Re: [R] Sorting a data frame by specifying a vector

Hi,
In your dataset, it seems like it is already ordered in the way you wanted to.
df.. <- data.frame(Season=rep(c("Summer","Fall","Winter","Spring"),4),Obs=
runif(length(rep(c("Summer","Fall","Winter","Spring"),4))))

#Suppose the order you want is:

 vec2<-c("Summer","Winter","Fall","Spring")
df1<-df..[match(df..$Season,vec2),]
 row.names(df1)<-1:nrow(df1)
 df1
#   Season       Obs
#1  Summer 0.2141001
#2  Winter 0.9318599
#3    Fall 0.6722337
#4  Spring 0.1927715
#5  Summer 0.2141001
#6  Winter 0.9318599
#7    Fall 0.6722337
#8  Spring 0.1927715
#9  Summer 0.2141001
#10 Winter 0.9318599
#11   Fall 0.6722337
#12 Spring 0.1927715
#13 Summer 0.2141001
#14 Winter 0.9318599
#15   Fall 0.6722337
#16 Spring 0.1927715


A.K.

----- Original Message -----
From: LCOG1 <jroll at lcog.org>
To: r-help at r-project.org
Cc: 
Sent: Thursday, October 11, 2012 1:08 PM
Subject: [R] Sorting a data frame by specifying a vector

Hello all,
   I cannot seem to figure out this seemingly simple procedure.  

I want to sort a data frame by a specified character vector.

So for :

df.. <- data.frame(Season=rep(c("Summer","Fall","Winter","Spring"),4),Obs=
runif(length(rep(c("Summer","Fall","Winter","Spring"),4))))

I want to sort the data frame by the seasons but in the order I specify since alphapetically would not put the season in sequential order

I tried the following and a few other things but no dice.  It looks like I will have to convert to factors.  Any thoughts?  Thanks

df.. <-
df..[sort(as.factor(Df..$Season,levels=c("Summer","Fall","Winter","Spring"))),]

Josh



--
View this message in context: http://r.789695.n4.nabble.com/Sorting-a-data-frame-by-specifying-a-vector-tp4645867.html
Sent from the R help mailing list archive at Nabble.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.




More information about the R-help mailing list