Vincent Guyader
2013-Sep-27 15:27 UTC
[R] An Apply function question about changing type of variable
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. Regards [[alternative HTML version deleted]]
Ben Tupper
2013-Sep-27 17:42 UTC
[R] An Apply function question about changing type of variable
Hi, On Sep 27, 2013, at 11:27 AM, Vincent Guyader 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. >Did you intend to use lapply() instead of apply()?> res<-lapply(iris,is) > str(res)List of 5 $ Sepal.Length: chr [1:2] "numeric" "vector" $ Sepal.Width : chr [1:2] "numeric" "vector" $ Petal.Length: chr [1:2] "numeric" "vector" $ Petal.Width : chr [1:2] "numeric" "vector" $ Species : chr [1:5] "factor" "integer" "oldClass" "numeric" ... apply() expects an array input, so it will coerce non-array inputs to be so with as.matrix() or as.array(). That coercion will change the data type if needed, in your case changing all to character to accommodate the factor column.> iris2 <- as.matrix(iris) > str(iris2)chr [1:150, 1:5] "5.1" "4.9" "4.7" "4.6" "5.0" "5.4" "4.6" "5.0" "4.4" ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:5] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" ... Cheers, Ben> > 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.Ben Tupper Bigelow Laboratory for Ocean Sciences 60 Bigelow Drive, P.O. Box 380 East Boothbay, Maine 04544 http://www.bigelow.org
R. Michael Weylandt <michael.weylandt@gmail.com>
2013-Sep-27 22:19 UTC
[R] An Apply function question about changing type of variable
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.