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

Martin Morgan mtmorgan at fhcrc.org
Fri Jan 20 16:13:37 CET 2012


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



More information about the Bioc-devel mailing list