dMh
dMh

Reputation: 27

R - Creating a subset by removing data that are factors

I have a dataset with missing values that contains both numeric and categorical variables. I plan on imputing the missing values by creating a subset for the numeric variables, creating a subset for the categorical variables and then applying the relevant mice methods on each.

I am currently trying to create the subset for the numeric variables but I am having trouble doing this. I am trying to create this subset by removing columns that are factors (i.e. categorical).

dataCont <- subset(data, select = -c(data %>% Filter(f = is.factor) %>% names))

However, I get the following error:

Error in -c(data %>% Filter(f = is.factor) %>% names) : 
  invalid argument to unary operator
In addition: Warning messages:
1: In doTryCatch(return(expr), name, parentenv, handler) :
  display list redraw incomplete
2: In doTryCatch(return(expr), name, parentenv, handler) :
  invalid graphics state
3: In doTryCatch(return(expr), name, parentenv, handler) :
  invalid graphics state

Would appreciate any guidance if possible. Thanks in advance!

Upvotes: 1

Views: 66

Answers (1)

akrun
akrun

Reputation: 887048

We can use the Filter as

library(dplyr)
Filter(is.factor, data) %>%
   names

In tidyverse, we can also do

library(dplyr)
data %>%
   select_if(is.factor) %>%
   select(contVar)

Upvotes: 1

Related Questions