[R] order() not producing results as I expect
peter dalgaard
pdalgd at gmail.com
Fri Oct 11 17:03:08 CEST 2013
I'd wager that the problem lies in the expectations. Karl: Read up on what order() actually does; it is not what I think you think it does.
Perhaps
str.dat[match(gen.names, str.dat$ind.names),]
or
rownames(str.dat) <- str.dat$ind.names
str.dat[gen.names,]
was intended? (Both untested, some adjustment may be required.)
-pd
On Oct 10, 2013, at 20:56 , Duncan Murdoch wrote:
> On 10/10/2013 12:00 PM, Karl Fetter wrote:
>> Hello,
>>
>> I'm using R version 3.0.0 on a mac. I'm having trouble getting order to
>> behave as I expect it should. I'm trying to sort a data.frame according to
>> a character vector. I'm able to sort the data.frame, but it retruns an
>> unexpected result. I have no idea where the order that is being produced
>> comes from.
>
> It comes from gen.names, which is not a column of your dataset. I think you'll have to give us something reproducible before we can help you.
>
> Duncan Murdoch
>
>>
>> Any ideas on how to properly order a data frame by a character vector?
>>
>> Here is the current order of the data frame (called str.dat):
>>
>> > head(str.dat)
>> str.names POPINFO POPFLAG LOCDATA Loc1 Loc2 Loc3 ind.names
>> 1 alba1.pop3 3 0 1 12 3 2 alba1
>> 2 alba2.pop3 3 0 1 3 3 2 alba2
>> 3 alch1.pop4 4 0 2 2 3 2 alch1
>> 4 alch2.pop4 4 0 2 2 3 2 alch2
>> 5 alco1.pop4 4 0 3 3 3 2 alco1
>> 6 alco2.pop4 4 0 3 3 3 2 alco2
>>
>>
>>
>> Here's the order I expect it to be in when I use order:
>>
>> > head(data.frame(gen.names))
>> gen.names
>> 1 magv1
>> 2 magv2
>> 3 magv3
>> 4 magv4
>> 5 lc1
>> 6 lc2
>>
>>
>> Here's the order I'm getting:
>>
>> > head(str.dat[order(gen.names),])
>> str.names POPINFO POPFLAG LOCDATA Loc1 Loc2 Loc3 ind.names
>> 111 ncle2.pop5 5 0 39 3 3 2 ncle2
>> 112 ncle3.pop5 5 0 39 2 2 2 ncle3
>> 146 wvma1.pop8 8 0 57 3 3 2 wvma1
>> 145 wvfa2.pop8 8 0 56 3 3 2 wvfa2
>> 55 flse6.pop2 2 0 19 2 5 4 flse6
>> 54 flse5.pop2 2 0 19 2 5 4 flse5
>>
>>
>>
>>
>> Many thanks,
>>
>> Karl
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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.
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
More information about the R-help
mailing list