Data Science Desktop Survival Guide
by Graham Williams |
|||||
Identify Numeric and Categoric Variables |
20180726 Identifying numeric and categoric variables may be useful for example for cluster analysis algorithms that only deal with numeric variables. Here we identify them by name (a character string) and by index. When using the index we have to assume the variables remain in the same order within the dataset and all variables are present, otherwise the indicies will get out of sync.
# Identify the numeric variables by index.
ds %>% sapply(is.numeric) %>% which() %>% intersect(inputi) %T>% print() -> numi
# Identify the numeric variables by name.
ds %>% names() %>% extract(numi) %T>% print() -> numc
# Identify the categoric variables by index and then name.
ds %>% sapply(is.factor) %>% which() %>% intersect(inputi) %T>% print() -> cati
ds %>%
names() %>% extract(cati) %T>% print() -> catc
|