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: daniel.brewer at icr.ac.uk ************************************************************** 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}}
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
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 >
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
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]]
On Thu, Aug 26, 2010 at 4:31 PM, Daniel Brewer <daniel.brewer at icr.ac.uk> 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
On Thu, Aug 26, 2010 at 11:31 AM, Daniel Brewer <daniel.brewer at icr.ac.uk> 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
try lapply(df, class) Steve E On Thu, Aug 26, 2010 at 4:31 PM, Daniel Brewer <daniel.brewer at icr.ac.uk> 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 ______________________________________________ R-help at r-project.org 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}}