How to remove columns from a data.frame by data type?

r

Solution

Assuming a generic `data.frame` this will remove columns of type `factor`

df[,-which(sapply(df, class) == "factor")]

EDIT

As per @Roland's suggestion, you can also just keep those which are not `factor`. Whichever you prefer.

df[, sapply(df, class) != "factor"]

EDIT 2

As you are concerned with the `cor` function, @Ista also points out that it would be safer in that particular instance to filter on `is.numeric`. The above are only to remove `factor` types.

df[,sapply(df, is.numeric)]

Problem

I have a data.frame with almost 200 variables (columns) and different type of data (num, int, logi, factor). Now, I would like to remove all the variables of the type "factor" to run the function cor() When I use the function str() I can see which variables are of the type "factor", but I don't know how to select and remove all these variables, because removing one by one is time consuming. To select these variables I have tried attr(), and typeof() without results. Some direction?

Original source