Brandon Bertelsen Brandon Bertelsen - 1 year ago 96
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?

Answer Source

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

nums <- sapply(x, is.numeric)

Then standard subsetting

x[ , nums]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download