[Bioc-devel] Applying "class" function to columns of a data.frame always return character

Nicolas Delhomme delhomme at embl.de
Fri Jan 20 16:17:39 CET 2012


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



More information about the Bioc-devel mailing list