[Rd] inconsistent behavior for logical vectors when using apply (" TRUE")

Greg Snow Greg.Snow at imail.org
Wed Nov 4 21:02:21 CET 2009


The apply function was meant to work on matrices and arrays, when you use it on a data frame, the frame is first converted to a matrix.  Since your data frame has columns of different modes, the logical column is converted to character and the matrix is of the single mode character.  That is what you are seeing.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111

> -----Original Message-----
> From: r-devel-bounces at r-project.org [mailto:r-devel-bounces at r-
> project.org] On Behalf Of Adrian Dragulescu
> Sent: Wednesday, November 04, 2009 12:45 PM
> To: r-devel
> Subject: [Rd] inconsistent behavior for logical vectors when using
> apply (" TRUE")
> 
> 
> Hello,
> 
> > X <- data.frame(letters=letters[1:3], flag=c(TRUE, FALSE, TRUE))
> > X
>    letters  flag
> 1       a  TRUE
> 2       b FALSE
> 3       c  TRUE
> > apply(X, 1, as.list)
> [[1]]
> [[1]]$letters
> [1] "a"
> 
> [[1]]$flag
> [1] " TRUE"
> 
> 
> [[2]]
> [[2]]$letters
> [1] "b"
> 
> [[2]]$flag
> [1] "FALSE"
> 
> 
> [[3]]
> [[3]]$letters
> [1] "c"
> 
> [[3]]$flag
> [1] " TRUE"
> 
> Notice how TRUE becomes " TRUE" and FALSE becomes "FALSE".  Not sure
> why
> TRUE gets an extra whitespace in front.
> 
> Checked with R-2.10.0, but can reproduce the behavior as far back as
> R-2.8.1.
> 
> Adrian Dragulescu
> 
> > sessionInfo()
> R version 2.10.0 (2009-10-26)
> i386-pc-mingw32
> 
> locale:
> [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United
> States.1252
> [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
> 
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
> 
> loaded via a namespace (and not attached):
> [1] tools_2.10.0
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list