

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 email message is confidential and for use by the a...{{dropped:2}}
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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
MartinLutherKingPlatz 3
D20146 Hamburg, GERMANY
+49(0)40 42838 6231
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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: 1877GKXGROUP
email: ggrothendieck at gmail.com
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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/rhelp
PLEASE do read the posting guide
http://www.Rproject.org/postingguide.html
and provide commented, minimal, selfcontained, reproducible code.
*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.

