[R] An Apply function question about changing type of variable

R. Michael Weylandt <michael.weylandt@gmail.com> michael.weylandt at gmail.com
Sat Sep 28 00:19:01 CEST 2013



On Sep 27, 2013, at 11:27, Vincent Guyader <vincent.guyader at allstat.fr> wrote:

> Hi everyone,
> 
> plese can you look at this few lines :
> 
> data(iris)
> res<-apply(iris,MARGIN=2,is)
> res[1,]
> 
> the result is :
> Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species
> "character"  "character"  "character"  "character"  "character"
> 
> How can I conserve the type off each colum? apply seems to convert it into
> "character" adn it could be a problem for me.
> 

lapply(iris, is)

apply() coerces to a matrix before doing its thing, here the lowest common denominator was character so that's what you got. 

M

> 
> Regards
> 
>    [[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.



More information about the R-help mailing list