[R] selecting columns from a data frame or data table by type, ie, numeric, integer

Carl Sutton suttoncarl at ymail.com
Fri Apr 29 22:07:21 CEST 2016


Thank you Bill Dunlap.  So simple I never tried that approach. Tried dozens of others though, read manuals till I was getting headaches, and of course the answer was simple when one is competent.   Learning, its a struggle, but slowly getting there.
Thanks again
 Carl Sutton CPA
 

    On Friday, April 29, 2016 10:50 AM, William Dunlap <wdunlap at tibco.com> wrote:
 
 

 > dt1[ vapply(dt1, FUN=is.numeric, FUN.VALUE=NA) ]    a   c1   1 1.12   2 1.0...10 10 0.2


Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Fri, Apr 29, 2016 at 9:19 AM, Carl Sutton via R-help <r-help at r-project.org> wrote:

Good morning RGuru's
I have a data frame of 575 columns.  I want to extract only those columns that are numeric(double) or integer to do some machine learning with.  I have searched the web for a couple of days (off and on) and have not found anything that shows how to do this.   Lots of ways to extract rows, but not columns.  I have attempted to use "(x == y)" indices extraction method but that threw error that == was for atomic vectors and lists, and I was doing this on a data frame.

My test code is below

#  a technique to get column classes
library(data.table)
a <- 1:10
b <- c("a","b","c","d","e","f","g","h","i","j")
c <- seq(1.1, .2, length = 10)
dt1 <- data.table(a,b,c)
str(dt1)
col.classes <- sapply(dt1, class)
head(col.classes)
dt2 <- subset(dt1, typeof = "double" | "numeric")
str(dt2)
dt2   #  not subset
dt2 <- dt1[, list(typeof = "double")]
str(dt2)
class_data <- dt1[,sapply(dt1,is.integer) | sapply(dt1, is.numeric)]
class_data
sum(class_data)
typeof(class_data)
names(class_data)
str(class_data)
 Any help is appreciated
Carl Sutton CPA

        [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.




  
	[[alternative HTML version deleted]]



More information about the R-help mailing list