[R] as.ordered
Birgit Lemcke
birgit.lemcke at systbot.uzh.ch
Tue Oct 9 18:26:06 CEST 2007
Hello Gavin,
I am so sorry that I haven´t read your mail accurately.
Now I tried all and it works very well and is exactly what I need.
Many thanks for your help.
And also many thanks to all the other people helping me.
Greetings
Birgit
Am 09.10.2007 um 17:38 schrieb Gavin Simpson:
> On Tue, 2007-10-09 at 17:12 +0200, Birgit Lemcke wrote:
>> Hello Gavin,
>>
>> thanks for your answer.
>>
>> Answering to your sort.list a data frame. I tried to convert it to a
>> vector but it was also not successful:
>>
>> bract.awnMin<-as.vector(bract.awnMin)
>
> Why did you expect that to work? I guess you didn't check the result:
>
> ## using dat.fac from my earlier email:
>> str(as.vector(dat.fac))
> 'data.frame': 10 obs. of 3 variables:
> $ fac1: Factor w/ 4 levels "2","3","4","5": 4 1 3 4 3 3 4 2 2 3
> $ fac2: Factor w/ 6 levels "1","2","4","8",..: 6 5 5 3 1 1 6 4 2 6
> $ fac3: Factor w/ 4 levels "1","3","6","8": 4 3 3 2 4 1 4 4 3 3
>> class(as.vector(dat.fac))
> [1] "data.frame"
>
>> sort.list(bract.awnMin)
>> Fehler in sort.list(bract.awnMin) :
>> 'x' must be atomic for 'sort.list'
>> Have you called 'sort' on a list?
>
> Whilst you are learning, you are going to get yourself in all sorts of
> mess if you make assumptions about what certain functions do. Take
> things step by step, and check the output of your commands as you go.
> Here class() and str() are useful friends to have around. If you'd
> used
> them, as I have here, you'd have seen that you'd made a mistake very
> early on and could then act to correct that, rather than get stuck
> under
> lots of permutations of things you think might work.
>
>>
>> Now I will explain what I need.
>>
>> I have two datasets, each with two ordered multistate variables in
>> columns.
>> I would like to calculate the dissmiliarity using daisy(cluster).
>>
>> As I understood I have to order the values to be able to convert the
>> variables into ordered factors?
>> Is that right?
>
> No, you don't need to order then at all. The levels are what is
> important. Below burried in your original email your example data
> shows
> something like I have here, that the levels go from low to high.
> as.ordered treats these appropriately in this case.
>
> So the answer is in my email, you don't need order():
>
>> dat.fac
> fac1 fac2 fac3
> 1 5 10 8
> 2 2 9 6
> 3 4 9 6
> 4 5 4 3
> 5 4 1 8
> 6 4 1 1
> 7 5 10 8
> 8 3 8 8
> 9 3 2 6
> 10 4 10 6
>> dat.fac.ordered <- data.frame(lapply(dat.fac, as.ordered))
>> dat.fac.ordered
> fac1 fac2 fac3
> 1 5 10 8
> 2 2 9 6
> 3 4 9 6
> 4 5 4 3
> 5 4 1 8
> 6 4 1 1
> 7 5 10 8
> 8 3 8 8
> 9 3 2 6
> 10 4 10 6
>> str(dat.fac.ordered)
> 'data.frame': 10 obs. of 3 variables:
> $ fac1: Ord.factor w/ 4 levels "2"<"3"<"4"<"5": 4 1 3 4 3 3 4 2 2 3
> $ fac2: Ord.factor w/ 6 levels "1"<"2"<"4"<"8"<..: 6 5 5 3 1 1 6 4
> 2 6
> $ fac3: Ord.factor w/ 4 levels "1"<"3"<"6"<"8": 4 3 3 2 4 1 4 4 3 3
>
> Note this hasn't sorted the data frame in any way, R just now knows
> that
> each of the elements of the data frame are *ordered* factors - note
> the
> "<" in the levels info printed by str().
>
> This of course assumes that all variables in your data frame are
> factors, which they are in your case.
>
> Is this what you want?
>
> HTH
>
> G
>
>>
>> But for me the problem here is, that the rows should stay together as
>> they are in the original dataset what is not possible if I have to
>> order each variable separately.
>>
>> Is there another way to classify variables as ordered factors?
>>
>> Thanks in advance and sorry for my surely sometimes stupid questions.
>>
>> Birgit
>>
>> Am 09.10.2007 um 16:23 schrieb Gavin Simpson:
>>
>>> Birgit,
>>>
>>> First things first, stop trying to sort.list a data frame. This
>>> is why
>>> you are getting the error. It is still a dataframe whether it has 1
>>> column or 100. ?sort.list clearly says argument 'x' is a vector,
>>> and as
>>> this shows, you are not passing it a vector
>>>
>>>> dat0 <- data.frame(var1 = runif(10))
>>>> class(dat0)
>>> [1] "data.frame"
>>>> sort.list(dat0)
>>> Error in sort.list(dat0) : 'x' must be atomic for 'sort.list'
>>> Have you called 'sort' on a list?
>>>
>>> This will work,
>>>
>>>> sort.list(dat$var1)
>>> [1] 8 2 3 10 9 4 6 7 1 5
>>>
>>> But returns the sorted *vector*, which is not what you want if I
>>> understand you.
>>>
>>> Secondly, sort is not the correct tool for this job (again, if I've
>>> understood what you are trying to achieve). Consider this example
>>> with 3
>>> variables and I want to order them on the first two columns only:
>>>
>>>> dat <- data.frame(var1 = runif(10), var2 = runif(10), var3 =
>>> runif(10))
>>>> dat
>>> var1 var2 var3
>>> 1 0.6489870 0.0007092352 0.1577805
>>> 2 0.2248372 0.6350688518 0.5345221
>>> 3 0.3031260 0.9814894125 0.9830289
>>> 4 0.3512622 0.7134463033 0.3758332
>>> 5 0.9920157 0.0905250614 0.4813042
>>> 6 0.3578282 0.9150679281 0.3739445
>>> 7 0.4420517 0.2544773000 0.3243123
>>> 8 0.1610078 0.3951566671 0.5922013
>>> 9 0.3454540 0.7033491128 0.2121476
>>> 10 0.3224135 0.2658058712 0.9959194
>>>> ord <- order(dat$var1, dat$var2)
>>>> ## if you need ordering on more columns, just add
>>>> ## then in the line above
>>>> dat.ord <- dat[ord, ]
>>>> dat.ord
>>> var1 var2 var3
>>> 8 0.1610078 0.3951566671 0.5922013
>>> 2 0.2248372 0.6350688518 0.5345221
>>> 3 0.3031260 0.9814894125 0.9830289
>>> 10 0.3224135 0.2658058712 0.9959194
>>> 9 0.3454540 0.7033491128 0.2121476
>>> 4 0.3512622 0.7134463033 0.3758332
>>> 6 0.3578282 0.9150679281 0.3739445
>>> 7 0.4420517 0.2544773000 0.3243123
>>> 1 0.6489870 0.0007092352 0.1577805
>>> 5 0.9920157 0.0905250614 0.4813042
>>>
>>> Now I see that you have factors, so lets try that:
>>>
>>>> dat.fac <- data.frame(fac1 = as.factor(sample(1:5, 10, replace =
>>>> TRUE)),
>>> fac2 = as.factor(sample(1:10, 10, replace =
>>> TRUE)),
>>> fac3 = as.factor(sample(c(1,3,6,8), 10,
>>> replace = TRUE)))
>>>> dat.fac
>>> fac1 fac2 fac3
>>> 1 5 10 8
>>> 2 2 9 6
>>> 3 4 9 6
>>> 4 5 4 3
>>> 5 4 1 8
>>> 6 4 1 1
>>> 7 5 10 8
>>> 8 3 8 8
>>> 9 3 2 6
>>> 10 4 10 6
>>>> ord <- order(dat.fac$fac1, dat.fac$fac2)
>>>> ord [1] 2 9 8 5 6 3 10 4 1 7
>>>> dat.fac.ord <- dat.fac[ord, ]
>>>> dat.fac.ord fac1 fac2 fac3
>>> 2 2 9 6
>>> 9 3 2 6
>>> 8 3 8 8
>>> 5 4 1 8
>>> 6 4 1 1
>>> 3 4 9 6
>>> 10 4 10 6
>>> 4 5 4 3
>>> 1 5 10 8
>>> 7 5 10 8
>>>
>>> Read ?order, and see that it's first argument is ... and this is
>>> supposed to be (quoting from ?order):
>>>
>>> Arguments:
>>>
>>> ...: a sequence of numeric, complex, character or logical
>>> vectors,
>>> all of the same length.
>>>
>>> Hence why my example works - assuming that is what you wanted of
>>> course.
>>>
>>>> From now on though I'm less clear what you actually need. Is what
>>>> I did
>>> above sufficient? If you need to convert the factors to be
>>> *ordered* in
>>> the R sense, such that one level is treated higher or lower in value
>>> than another, we need something else - perhaps in addition, which
>>> applies as.ordered() to each element of the data frame in turn:
>>>
>>>> ## continue with the ordered data frame of factors from above
>>>> ## but now convert each factor to an ordered factor
>>>> new.fac.ord <- data.frame(lapply(dat.fac.ord, as.ordered))
>>>> new.fac.ord
>>> fac1 fac2 fac3
>>> 1 2 9 6
>>> 2 3 2 6
>>> 3 3 8 8
>>> 4 4 1 8
>>> 5 4 1 1
>>> 6 4 9 6
>>> 7 4 10 6
>>> 8 5 4 3
>>> 9 5 10 8
>>> 10 5 10 8
>>>> str(new.fac.ord)
>>> 'data.frame': 10 obs. of 3 variables:
>>> $ fac1: Ord.factor w/ 4 levels "2"<"3"<"4"<"5": 1 2 2 3 3 3 3 4 4 4
>>> $ fac2: Ord.factor w/ 6 levels "1"<"2"<"4"<"8"<..: 5 2 4 1 1 5 6 3
>>> 6 6
>>> $ fac3: Ord.factor w/ 4 levels "1"<"3"<"6"<"8": 3 3 4 4 1 3 3 2 4 4
>>>
>>> But I suspect this is not what you wanted as you use order() to
>>> achieve
>>> partly a solution but ordering on a single variable.
>>>
>>> HTH
>>>
>>> G
>>>
>>> On Tue, 2007-10-09 at 11:14 +0200, Birgit Lemcke wrote:
>>>> Hello Friedrich,
>>>>
>>>> thanks for your help and it is really not important that the
>>>> solution
>>>> is elegant. Important is only that there is a solution.
>>>>
>>>> But I still have some problems with this topic.
>>>>
>>>> #I tried as you suggested to order the vectors separately. My first
>>>> problem is that my data is a data.frame:
>>>>
>>>> data.frame': 348 obs. of 1 variable:
>>>> $ bracts.length.relative.to.flower...............Min: Factor w/ 4
>>>> levels "1","2","3","4": 2 3 3 3 3 2 1 4 3 2 ...
>>>>
>>>> bracts.length.relative.to.flower...............Min
>>>> 1 2
>>>> 2 3
>>>> 3 3
>>>> 4 3
>>>> 5 3
>>>> 6 2
>>>>
>>>>
>>>> #I tried to convert it to a vector using this:
>>>>
>>>> bract.awnMin<-as.vector(bract.awnMin)
>>>>
>>>> '#and then to use sort.list:
>>>>
>>>> sort.list(bract.awnMin)
>>>> Fehler in sort.list(bract.awnMin) :
>>>> 'x' must be atomic for 'sort.list'
>>>> Have you called 'sort' on a list?
>>>>
>>>> #if I try to use the following for the data.frame with two
>>>> variables,
>>>> it works well.
>>>>
>>>> bract.awn[order(bract.awn[,1]),]
>>>>
>>>> #but I have tu order both variables an therefore I should do that
>>>> separately and then use cbind.
>>>>
>>>> Can somebody help me with my problem please?
>>>>
>>>> Greetings
>>>>
>>>> Birgit
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> Am 08.10.2007 um 20:46 schrieb Friedrich Schuster:
>>>>
>>>>>
>>>>> Hello,
>>>>>
>>>>> (Warning. This might not be the most complete or elegant
>>>>> solution ...)
>>>>>
>>>>> If you want a sorted dataframe: look here for example
>>>>> http://tolstoy.newcastle.edu.au/R/help/05/02/12391.html
>>>>>
>>>>> To convert the factors from a data frame, you have to call
>>>>> as.ordered for
>>>>> each factor separately (not for the dataframe).
>>>>> To convert two factors a and b and merge them into a new
>>>>> dataframe:
>>>>> newFrame <- as.data.frame(cbind(as.ordered(a),as.ordered(b)))
>>>>> l
>>>>> For a larger number of factors this can be done with a loop or
>>>>> better one of
>>>>> the "apply"-functions.
>>>>>
>>>>> Hope this helps,
>>>>> Friedrich Schuster
>>>>>
>>>>>
>>>>> Birgit Lemcke wrote:
>>>>>>
>>>>>> Hello Members,
>>>>>>
>>>>>> I try to convert variables in a data.frame (bract.awn) in the
>>>>>> class
>>>>>> ordered.
>>>>>>
>>>>>> str(bract.awn)
>>>>>> 'data.frame': 348 obs. of 2 variables:
>>>>>> $ bracts.length.relative.to.flower...............Min: Factor w/ 4
>>>>>> levels "1","2","3","4": 2 3 3 3 3 2 1 4 3 2 ...
>>>>>> $ bract.awn.relative.to.body.................Max : Factor w/ 4
>>>>>> levels "1","2","3","4": 1 3 2 1 4 1 1 1 1 1
>>>>>>
>>>>>>
>>>>>> I tried this:
>>>>>>
>>>>>> bract.awn<-as.ordered(bract.awn)
>>>>>>
>>>>>> Fehler in sort.list(unique.default(x), na.last = TRUE) :
>>>>>> 'x' must be atomic for 'sort.list'
>>>>>> Have you called 'sort' on a list?
>>>>>>
>>>>>> What am I doing wrong?
>>>>>>
>>>>>> Thanks a lot in advance.
>>>>>>
>>>>>> Regards
>>>>>>
>>>>>> Birgit
>>>>>>
>>>>>>
>>>>>>
>>>>>> Birgit Lemcke
>>>>>> Institut für Systematische Botanik
>>>>>> Zollikerstrasse 107
>>>>>> CH-8008 Zürich
>>>>>> Switzerland
>>>>>> Ph: +41 (0)44 634 8351
>>>>>> birgit.lemcke at systbot.uzh.ch
>>>>>>
>>>>>> ______________________________________________
>>>>>> 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.
>>>>>>
>>>>>>
>>>>>
>>>>> --
>>>>> View this message in context: http://www.nabble.com/as.ordered-
>>>>> tf4589454.html#a13102513
>>>>> 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.
>>>>
>>>> Birgit Lemcke
>>>> Institut für Systematische Botanik
>>>> Zollikerstrasse 107
>>>> CH-8008 Zürich
>>>> Switzerland
>>>> Ph: +41 (0)44 634 8351
>>>> birgit.lemcke at systbot.uzh.ch
>>>>
>>>> ______________________________________________
>>>> 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.
>>> --
>>> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~
>>> %~%
>>> Gavin Simpson [t] +44 (0)20 7679 0522
>>> ECRC, UCL Geography, [f] +44 (0)20 7679 0565
>>> Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
>>> Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/
>>> UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
>>> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~
>>> %~%
>>>
>>
>> Birgit Lemcke
>> Institut für Systematische Botanik
>> Zollikerstrasse 107
>> CH-8008 Zürich
>> Switzerland
>> Ph: +41 (0)44 634 8351
>> birgit.lemcke at systbot.uzh.ch
>>
>>
>>
>>
>>
> --
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> Gavin Simpson [t] +44 (0)20 7679 0522
> ECRC, UCL Geography, [f] +44 (0)20 7679 0565
> Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
> Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/
> UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
>
Birgit Lemcke
Institut für Systematische Botanik
Zollikerstrasse 107
CH-8008 Zürich
Switzerland
Ph: +41 (0)44 634 8351
birgit.lemcke at systbot.uzh.ch
More information about the R-help
mailing list