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

Giorgio Garziano giorgio.garziano at ericsson.com
Fri Apr 29 20:39:19 CEST 2016


Hi,

I was able to replicate the solution as suggested by William in case of
data.frame class, not in case of data.table class.
In case of data.table, I had to do some minor changes as shown below.


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)

# in case of data frame
dt1 <- data.frame(a,b,c)
dt1[vapply(dt1, FUN=is.numeric, FUN.VALUE=NA)]

    a   c
1   1 1.1
2   2 1.0
3   3 0.9
4   4 0.8
5   5 0.7
6   6 0.6
7   7 0.5
8   8 0.4
9   9 0.3
10 10 0.2

# in case of data table
dt1 <- data.table(a,b,c)
dt1[, vapply(dt1, FUN=is.numeric, FUN.VALUE=NA), with=FALSE]

    a   c
1   1 1.1
2   2 1.0
3   3 0.9
4   4 0.8
5   5 0.7
6   6 0.6
7   7 0.5
8   8 0.4
9   9 0.3
10 10 0.2


--

Best,

GG




	[[alternative HTML version deleted]]



More information about the R-help mailing list