0

I would like to extract all columns for which the values are numeric from a dataframe, for a large dataset.

#generate mixed data
dat <- matrix(rnorm(100), nrow = 20)
df <- data.frame(letters[1 : 20], dat)

I was thinking of something along the lines of:

numdat <- df[,df == "numeric"]

That however leaves me without variables. The following gives an error.

dat <- df[,class == "numeric"]
Error in class == "numeric" : 
comparison (1) is possible only for atomic and list types

What should I do instead?

1
  • df[sapply(df, is.numeric)] Commented Jul 11, 2018 at 14:15

1 Answer 1

2

use sapply

numdat <- df[,sapply(df, function(x) {class(x)== "numeric"})]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.