Find classes for each column of a data.frame

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Find classes for each column of a data.frame

Daniel Brewer-3
Hello,

Is there a simple way to get the class type for each column of a
data.frame?  I am in the situation where I would like to get all the
columns of a data.frame that are factors.

I have tried:
apply(df,2,class)
but all the columns come back as class "character".

Thanks

Dan

--
**************************************************************
Daniel Brewer, Ph.D.

Institute of Cancer Research
Molecular Carcinogenesis
Email: [hidden email]
**************************************************************

The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company Limited by Guarantee, Registered in England under Company No. 534147 with its Registered Office at 123 Old Brompton Road, London SW7 3RP.

This e-mail message is confidential and for use by the a...{{dropped:2}}

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Find classes for each column of a data.frame

David Winsemius

On Aug 26, 2010, at 11:31 AM, Daniel Brewer wrote:

> Hello,
>
> Is there a simple way to get the class type for each column of a
> data.frame?  I am in the situation where I would like to get all the
> columns of a data.frame that are factors.
>
> I have tried:
> apply(df,2,class)

lapply(df, class)

> but all the columns come back as class "character".

Agree that is not what I would have expected.

--
David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Find classes for each column of a data.frame

Erik Iverson-3
In reply to this post by Daniel Brewer-3
That's because apply works on arrays/matrices, not data.frames.

It therefore coerces your data.frame to a matrix of type
character, since you have factors, thus the result.

You want sapply or lapply, since a data.frame is actually a
list.

sapply(df, class)

and then to get what you want:

df[sapply(df, is.factor)]

Daniel Brewer wrote:

> Hello,
>
> Is there a simple way to get the class type for each column of a
> data.frame?  I am in the situation where I would like to get all the
> columns of a data.frame that are factors.
>
> I have tried:
> apply(df,2,class)
> but all the columns come back as class "character".
>
> Thanks
>
> Dan
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Find classes for each column of a data.frame

David Winsemius
In reply to this post by Daniel Brewer-3

On Aug 26, 2010, at 11:31 AM, Daniel Brewer wrote:

> Hello,
>
> Is there a simple way to get the class type for each column of a
> data.frame?  I am in the situation where I would like to get all the
> columns of a data.frame that are factors.
>
> I have tried:
> apply(df,2,class)
> but all the columns come back as class "character".

Just realized it happens because of matrix coercion.

--

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Find classes for each column of a data.frame

Ivan Calandra
In reply to this post by Daniel Brewer-3
  Since a data.frame is a list, you should use lapply() or sapply():

 > df <- data.frame(a=1:5, b=LETTERS[1:5])
 > lapply(df, class)
$a
[1] "integer"
$b
[1] "factor"
 > sapply(df, class)
         a         b
"integer"  "factor"

HTH,
Ivan

Le 8/26/2010 17:31, Daniel Brewer a écrit :

> Hello,
>
> Is there a simple way to get the class type for each column of a
> data.frame?  I am in the situation where I would like to get all the
> columns of a data.frame that are factors.
>
> I have tried:
> apply(df,2,class)
> but all the columns come back as class "character".
>
> Thanks
>
> Dan
>
--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231




        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Find classes for each column of a data.frame

barry rowlingson
In reply to this post by Daniel Brewer-3
On Thu, Aug 26, 2010 at 4:31 PM, Daniel Brewer <[hidden email]> wrote:
> Hello,
>
> Is there a simple way to get the class type for each column of a
> data.frame?  I am in the situation where I would like to get all the
> columns of a data.frame that are factors.
>
> I have tried:
> apply(df,2,class)
> but all the columns come back as class "character".

 apply is treating it as a matrix, and so converts it to the lowest common form.

 try lapply and use is.factor, with some unlist for good measure:

 z=data.frame(x=1:10,l=factor(1:10))
 unlist(lapply(z,is.factor))

   x     l
FALSE  TRUE


Barry

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Find classes for each column of a data.frame

Marc Schwartz-3
In reply to this post by David Winsemius

On Aug 26, 2010, at 10:36 AM, David Winsemius wrote:

>
> On Aug 26, 2010, at 11:31 AM, Daniel Brewer wrote:
>
>> Hello,
>>
>> Is there a simple way to get the class type for each column of a
>> data.frame?  I am in the situation where I would like to get all the
>> columns of a data.frame that are factors.
>>
>> I have tried:
>> apply(df,2,class)
>
> lapply(df, class)
>
>> but all the columns come back as class "character".
>
> Agree that is not what I would have expected.


> sapply(iris, class)
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species
   "numeric"    "numeric"    "numeric"    "numeric"     "factor"


> apply(iris, 2, class)
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species
 "character"  "character"  "character"  "character"  "character"


The second occurs because apply() will coerce the data frame to a matrix:


> str(as.matrix(iris))
 chr [1:150, 1:5] "5.1" "4.9" "4.7" "4.6" "5.0" "5.4" ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:5] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" ...

Since a matrix can only contain a single class of objects (recall that a matrix is a vector with dim attributes), 'iris' becomes a character matrix.

HTH,

Marc Schwartz

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Find classes for each column of a data.frame

Gabor Grothendieck
In reply to this post by Daniel Brewer-3
On Thu, Aug 26, 2010 at 11:31 AM, Daniel Brewer <[hidden email]> wrote:

> Hello,
>
> Is there a simple way to get the class type for each column of a
> data.frame?  I am in the situation where I would like to get all the
> columns of a data.frame that are factors.
>
> I have tried:
> apply(df,2,class)
> but all the columns come back as class "character".
>

Try this where iris is a builtin data frame with one several numeric
columns and one factor column, Species:

> onlyFactors <- Filter(is.factor, iris)
> str(onlyFactors)
'data.frame':   150 obs. of  1 variable:
 $ Species: Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Find classes for each column of a data.frame

barry rowlingson
In reply to this post by Marc Schwartz-3
On Thu, Aug 26, 2010 at 4:44 PM, Marc Schwartz <[hidden email]> wrote:

>> sapply(iris, class)
> Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species
>   "numeric"    "numeric"    "numeric"    "numeric"     "factor"

 Note that comparing the result of class(foo) is a bad way of telling
if something is of a particular class, because of inheritance -
class(foo) can be a vector if the object belongs to more than one
class.

 Always test using is.whatever(foo), which returns TRUE if foo is a whatever:

 > f=factor(letters)
 > class(f)
 [1] "factor"
 > class(f)=c("factor","alphabet")
 > f
  [1] a b c d e f g h i j k l m n o p q r s t u v w x y z
 Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z
 > is.factor(f)
 [1] TRUE

 but now:

 > class(f)=="factor"
 [1]  TRUE FALSE

Barry

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Find classes for each column of a data.frame

slre
In reply to this post by Daniel Brewer-3
try

lapply(df, class)

Steve E

On Thu, Aug 26, 2010 at 4:31 PM, Daniel Brewer
<[hidden email]> wrote:
> Hello,
>
> Is there a simple way to get the class type for each column of a
> data.frame?  I am in the situation where I would like to get all the
> columns of a data.frame that are factors.
>
> I have tried:
> apply(df,2,class)
> but all the columns come back as class "character".

 apply is treating it as a matrix, and so converts it to the lowest
common form.

 try lapply and use is.factor, with some unlist for good measure:

 z=data.frame(x=1:10,l=factor(1:10))
 unlist(lapply(z,is.factor))

   x     l
FALSE  TRUE


Barry

______________________________________________
[hidden email] 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.

*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}

______________________________________________
[hidden email] 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.