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

Carl Sutton suttoncarl at ymail.com
Fri Apr 29 18:19:02 CEST 2016


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]]



More information about the R-help mailing list