user3619169
user3619169

Reputation: 59

In a large data set, identify which variables are categorical and which are numeric

I have a list of 65 variables, and I want to separate out the lists of numerical and categorical variable.

What can be command for this task.

Upvotes: 4

Views: 10637

Answers (2)

Victorp
Victorp

Reputation: 13856

You can do this (imagine your data.frame is named df) :

sapply(df, class)

Indeed, the output with time variable is less pretty :

library(lubridate)
df <- data.frame(V1 = character(10),
                 V2 = numeric(10),
                 V3 = ymd(paste("2014-05", 21:30, sep="-")))
sapply(df, class)
##$V1
##[1] "factor"
##
##$V2
##[1] "numeric"
##
##$V3
##[1] "POSIXct" "POSIXt" 

But it still work to identify numeric or factor variables like if there was no time variable :

names(df)[sapply(df, class) == "factor"]
##[1] "V1"

# for time variable it's less obvious indeed...
names(df)[grepl("POSIXct", sapply(df, class))]
##[1] "V3"

Upvotes: 5

James
James

Reputation: 66844

You can use split with sapply to group the variables together:

split(names(iris),sapply(iris, function(x) paste(class(x), collapse=" ")))
$factor
[1] "Species"

$numeric
[1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"

Note the use of paste to collapse together any multi-class object's class names.

Upvotes: 9

Related Questions