March 2019
Beginner to intermediate
250 pages
6h 6m
English
Now, we are going to calculate the number of unique values that a variable takes. Thus, if a variable only takes one single value, it can be directly removed from the dataset.
The sapply function allows the calculation of the n_distinct values of each variable. Create a new data frame with the name of the constant variables:
library(dplyr)unique_values<-as.data.frame(sapply(train, n_distinct))
Rename the name of the variable in this data frame:
colnames(unique_values)<-"Unique_values"
Add a column in the data frame containing the name of the variables:
unique_values$variable_name<-colnames(train)
Then create a list with the name of the constant variables:
variables_to_remove<-unique_values[unique_values$Unique_values==1,"variable_name"] ...
Read now
Unlock full access