Brandon Bertelsen Brandon Bertelsen - 5 months ago 45
R Question

Selecting only numeric columns from a data frame

Suppose, you have a data.frame like this:

x <- data.frame(v1=1:20,v2=1:20,v3=1:20,v4=letters[1:20])

How would you select only those columns in x that are numeric?


Since a data frame is a list we can use the list-apply functions:

nums <- sapply(x, is.numeric)

Then standard subsetting

x[ , nums]