user1742188
user1742188

Reputation: 5083

Extract columns of specific type in data frame

I have a data frame that has a bunch of numeric and factor columns. I want all the non factor columns. Is there a way to extract them? I tried something like

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

But no luck.

Upvotes: 2

Views: 1134

Answers (2)

Marat Talipov
Marat Talipov

Reputation: 13304

Technically, data frame is a list, so you might want to apply class() to every element of that list (i.e. to each column):

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

Upvotes: 2

LyzandeR
LyzandeR

Reputation: 37879

An example with Filter:

df <- data.frame(as.factor(letters), a = runif(26), b=runif(26) )
Filter(Negate(is.factor), df)

Output:

> Filter(Negate(is.factor), df)
            a          b
1  0.04942200 0.85160817
2  0.96370499 0.77911640
3  0.66545208 0.33037229
4  0.19736620 0.54797165
5  0.15264687 0.72744035
6  0.86426399 0.88491690
...
...

Upvotes: 2

Related Questions