[R] Non-standard sorts on vectors

David Winsemius dwinsemius at comcast.net
Fri Aug 27 00:29:23 CEST 2010


On Aug 26, 2010, at 6:10 PM, Jorge Ivan Velez wrote:

> Hi Chipper,
>
> Try
>
> test.df[unlist(sort.v),]

That does work, and I suspect it is because data.frame converts  
character vectors to factors by default. So this also works:

test.df[factor(c("Lake","Shoreline","Floodplain")), ]

But why? I must be reading the wrong help pages. I would have assumed  
that the factor object was being read into the extract call as a  
numeric which would not have resulted in the observed behavior. I do  
not see in the Extract help page that factors offered as "i" arguments  
are supposed to be matched to the first column of dataframes.

-- 
David.
>
> HTH,
> Jorge
>
>
> On Thu, Aug 26, 2010 at 5:59 PM, chipmaney <> wrote:
>
>>
>> I have a dataset I need to sort:
>>
>> test.df<-data.frame(Zone=c("Floodplain", "Lake",
>> "Shoreline"),Cover=c(50,60,70))
>>
>> However, I don't want it sorted ascending/descending but in an  
>> order that I
>> define via a vector:
>>
>> sort.v<-data.frame(c("Lake","Shoreline","Floodplain"))
>>
>> I realize I could probably just create the vector of [factors] in  
>> the order
>> I want, then assign the value:
>>
>> sort.v$Cover<-test.df$Cover
>>
>> However, this is inefficient.  Is there source code or a  
>> syntactical trick
>> that will do the same more efficiently?  Note: I have investigated  
>> order()
>> and sort(), but neither seems to have an argument for defining the  
>> sorting
>> order.
>>
>> Thanks in advance, Chipper

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list