An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/bioc-devel/attachments/20120120/d9fdbdfd/attachment.pl>
[Bioc-devel] Applying "class" function to columns of a data.frame always return character
3 messages · João Daniel Nunes Duarte, Martin Morgan, Nicolas Delhomme
On 01/20/2012 07:07 AM, Jo?o Daniel Nunes Duarte wrote:
Hello, I need to identify which columns from a data.frame is a factor. Since I need to test "is.factor" for all columns, I tried the following code: apply(dataframe, 2, is.factor) But it always returned FALSE. Then I tried to check the classes of those columns using: apply(dataframe, 2, class) And it always returned "character". Is the related to the fact that apply function should be applied to arrays or matrixes, and not to data.frames? So I believe that apply function convert the data.frame into a matrix. And the class of the matrix will be character if any of its elements is not numeric. So, if I my conclusion about apply function is correct, this is not the way I should proceed. So, how can I get a vector of booleans as result of some function applied to columns of a data.frame, e.g., is.factor?
A data frame is a list of equal length vectors, so you can use
sapply(df, class)
class(df[["some_column"]]
class(df$some_column)
Martin
Thanks! Cheers, Jo?o Daniel UFMG - Brazil [[alternative HTML version deleted]]
_______________________________________________ Bioc-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/bioc-devel
Computational Biology Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: M1-B861 Telephone: 206 667-2793
This would work:
sapply(1:ncol(dataframe),function(i,df){
is.factor(df[,i])
},dataframe)
Cheers,
---------------------------------------------------------------
Nicolas Delhomme
Genome Biology Computational Support
European Molecular Biology Laboratory
Tel: +49 6221 387 8310
Email: nicolas.delhomme at embl.de
Meyerhofstrasse 1 - Postfach 10.2209
69102 Heidelberg, Germany
---------------------------------------------------------------
On 20 Jan 2012, at 16:07, Jo?o Daniel Nunes Duarte wrote:
Hello, I need to identify which columns from a data.frame is a factor. Since I need to test "is.factor" for all columns, I tried the following code: apply(dataframe, 2, is.factor) But it always returned FALSE. Then I tried to check the classes of those columns using: apply(dataframe, 2, class) And it always returned "character". Is the related to the fact that apply function should be applied to arrays or matrixes, and not to data.frames? So I believe that apply function convert the data.frame into a matrix. And the class of the matrix will be character if any of its elements is not numeric. So, if I my conclusion about apply function is correct, this is not the way I should proceed. So, how can I get a vector of booleans as result of some function applied to columns of a data.frame, e.g., is.factor? Thanks! Cheers, Jo?o Daniel UFMG - Brazil [[alternative HTML version deleted]]
_______________________________________________ Bioc-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/bioc-devel