Re: [R] Remove non-numerical columns from data frame

From: Henrik Bengtsson <hb_at_stat.berkeley.edu>
Date: Sat, 26 Jul 2008 10:54:24 -0700

On Sat, Jul 26, 2008 at 9:35 AM, Peter Dalgaard <p.dalgaard_at_biostat.ku.dk> wrote:
> Henrique Dallazuanna wrote:
>>
>> Try this:
>>
>> x <- data.frame(A=c(10,20), B = c("a", "b"), C = c(20,30))
>> x[sapply(x, is.numeric)]
>>
>>
>
> Yes, and read ?Extract to clear up the confusion between data$vol and
> data[257] and data[[257]] (Section on "Recursive (list-like) objects:").
> Also, ?Extract.data.frame.

...and read up how you can use argument 'colClasses' in read.table() to read/skip selected columns from a tabular data file. Is that how you get your data.frame in the first place?!

/Henrik

>
>> On Sat, Jul 26, 2008 at 11:19 AM, Markus Mühlbacher <muehliman_at_yahoo.com>
>> wrote:
>>
>>>
>>> Hello,
>>>
>>> My question sounds simple, but as I am desperatly searchin for a solution
>>> I am
>>> asking you all. :)
>>>
>>> I try to filter out all non-numeric columns of a data frame using a for
>>> loop
>>> to go through all columns. My if clause looks like this:
>>>
>>> for(j in 1:length(data)) {
>>>
>>> ...
>>> if(!is.numeric(data[j])) skip <- "not numeric")
>>> ...
>>> }
>>>
>>> When I do: is.numeric(data$vol) i get TRUE as result:
>>> unfortunately this is not applicable in a for loop, because i need to go
>>> through all of the columns using j.
>>> I also tried to paste data$ and the name of the column , but this is
>>> always a
>>> character.
>>>
>>> When I do: is.numeric(data[257]) i get FALSE as result
>>> (257 is the corresponding column index to "vol")
>>>
>>
>
>
> --
> O__ ---- Peter Dalgaard Ĝster Farimagsgade 5, Entr.B
> c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
> (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
> ~~~~~~~~~~ - (p.dalgaard_at_biostat.ku.dk) FAX: (+45) 35327907
>
> ______________________________________________
> 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.
>



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. Received on Sat 26 Jul 2008 - 17:59:17 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 26 Jul 2008 - 19:32:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive