[Rd] 'ordered' destroyed to 'factor'

Joris Meys jorismeys at gmail.com
Fri Jun 16 16:00:59 CEST 2017


This can be traced back to the following line in unlist():

structure(res, levels = lv, names = nm, class = "factor")

The Details section of ?unlist states specifically how it treats factors,
so this is documented and expected behaviour.

This is also the appropriate behaviour. In your case one could argue that
unlist should maintain the order, as there's only a single factor. However,
the moment you have 2 ordered factors, there's no guarantee that the levels
are the same, or even in the same order. Hence it is impossible to
determine what should be the correct order. For this reason, the only
logical object to be returned in case of a list of factors, is an unordered
factor.

In your use case (so with a list of factors with identical ordered levels)
the solution is one extra step:

x <- list(
  factor(c("a","b"),
         levels = c("a","b","c"),
         ordered = TRUE),
  factor(c("b","c"),
         levels = c("a","b","c"),
         ordered = TRUE)
)
res <- sapply(x, min)
res <- ordered(res, levels = levels(res))
min(res)


I hope this explains

Cheers
Joris


On Fri, Jun 16, 2017 at 3:03 PM, "Jens Oehlschlägel" <
jens.oehlschlaegel at truecluster.com> wrote:

> Dear all,
>
> I don't know if you consider this a bug or feature, but it breaks
> reasonable code: 'unlist' and 'sapply' convert 'ordered' to 'factor' even
> if all levels are equal. Here is a simple example:
>
> o <- ordered(letters)
> o[[1]]
> lapply(o, min)[[1]]          # ordered factor
> unlist(lapply(o, min))[[1]]  # no longer ordered
> sapply(o, min)[[1]]          # no longer ordered
>
> Jens Oehlschlägel
>
>
> P.S: The above examples are silly for simple reproduction. The current
> behavior broke my use-case which had a structure like this
>
> # have some data
> x <- 1:20
> # apply some function to each element
> somefunc <- function(x){
>   # do something and return an ordinal level
>   sample(o, 1)
> }
> x <- sapply(x, somefunc)
> # get minimum result
> min(x)
> # Error in Summary.factor(c(2L, 26L), na.rm = FALSE) :
> #   ‘min’ not meaningful for factors
>
>
> > version
>                _
> platform       x86_64-pc-linux-gnu
> arch           x86_64
> os             linux-gnu
> system         x86_64, linux-gnu
> status
> major          3
> minor          4.0
> year           2017
> month          04
> day            21
> svn rev        72570
> language       R
> version.string R version 3.4.0 (2017-04-21)
> nickname       You Stupid Darkness
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel




-- 
Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Mathematical Modelling, Statistics and Bio-Informatics

tel :  +32 (0)9 264 61 79
Joris.Meys at Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

	[[alternative HTML version deleted]]



More information about the R-devel mailing list