[R] subset(..., drop=TRUE) doesn't seem to work.

Paul Lemmens P.Lemmens at nici.kun.nl
Thu Jun 17 10:03:24 CEST 2004


Hoi Peter,

--On woensdag 16 juni 2004 17:35 +0200 Peter Dalgaard 
<p.dalgaard at biostat.ku.dk> wrote:
> Anyways, the way out is
>
>  d2 <- subset(dd,c==1)
>  ifac <- sapply(dd,is.factor)
>  d2[ifac] <- lapply(d2[ifac],factor)
>
> or
>
>  d2 <- subset(dd,c==1)
>  d2[] <- lapply(d2, function(x) if (is.factor(x)) factor(x) else x)
>
My toolbox.r now contains:

my.subset <- function(x, drop.unused.levels=FALSE, ...) {
  subsetted <- subset(x, ...)

  if (drop.unused.levels) {
    subsetted[] <- lapply(subsetted,
                      function(x) if (is.factor(x)) factor(x) else x)
  }

  subsetted
}


Thnx for all the help!



-- 
Paul Lemmens
NICI, University of Nijmegen              ASCII Ribbon Campaign /"\
Montessorilaan 3 (B.01.05)                    Against HTML Mail \ /
NL-6525 HR Nijmegen                                              X
The Netherlands                                                 / \
Phonenumber    +31-24-3612648
Fax            +31-24-3616066




More information about the R-help mailing list