Data Cleaning in R - Part 2

Attributes with Zero Variance

Datasets can sometimes contain attributes (predictors) that have near-zero variance, or may have just one value. Such variables are considered to have less predictor power. Apart from being uninformative, these predictors may also sometimes break the model that you are trying to fit to your data. This can occur, for example, due to division by zero (if a standardization is performed in the data).

One quick solution is to remove all predictors that satisfy some threshold criterion related to their variance.

In our dataset, we will look for predictors that have zero variance and will remove them.

We will first define some generic functions that we will use later.

# Returns the Numeric columns from a dataset
getNumericColumns<-function(t){
    tn = sapply(t,function(x){is.numeric(x)})
    return(names(tn)[which(tn)])
}
# Returns the character columns from a dataset
getCharColumns<-function(t){
    tn = sapply(t,function(x){is.character(x)})
    return(names(tn)[which(tn)])
}
# Returns the factor columns in a dataset 
getFactorColumns<-function(t){
    tn = sapply(t,function(x){is.factor(x)})
    return(names(tn)[which(tn)])
}
# Returns index of columns along with the column names
getIndexsOfColumns <- function(t,column_names){
    return(match(column_names,colnames(t)))
}

Now we can find character columns with same value and numeric columns with zero-variance.

tmp = apply(data_train[getCharColumns(data_train)],2,function(x){length(unique(x))})
tmp = tmp[tmp==1]

tmp2 = apply(data_train[getNumericColumns(data_train)],2,function(x){(sd(x))})
tmp2 = tmp2[tmp2==0]

discard_column = c(names(tmp),names(tmp2))

> discard_column
[1] "policy_code"
>

There is only one predictor that meets this criteria. We then proceed to drop this zero variance feature.

data_train = (data_train[,!(names(data_train) %in% discard_column)])

Title, Desc, and Purpose

Let’s look at the attributes ’title’ and ‘purpose’.

> table(data_train$purpose)
               car        credit_card debt_consolidation   home_improvement              house 
               424               9163              24604               2785                197 
    major_purchase            medical             moving              other   renewable_energy 
               939                480                281               2340                 31 
    small_business           vacation 
               404                261 
> table(data_train$title)
                                       Business           Car financing Credit card refinancing 
                   3323                     372                     403                    8292 
     Debt consolidation              Green loan             Home buying        Home improvement 
                  22614                      27                     187                    2614 
         Major purchase        Medical expenses   Moving and relocation                   Other 
                    879                     453                     264                    2239 
               Vacation 
                    242 
>

The variable title and purpose have the same information. So, we can drop one of them. We will drop title.

> data_train$title = NULL

Let’s look at what we have in the desc column.

> str(data_train$desc)
 chr [1:41909] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" …

As you can see it looks mostly empty. We will drop this as well.

> data_train$desc = NULL

Related Downloads

Data Science in Finance: 9-Book Bundle

Data Science in Finance Book Bundle

Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.

What's Included:

  • Getting Started with R
  • R Programming for Data Science
  • Data Visualization with R
  • Financial Time Series Analysis with R
  • Quantitative Trading Strategies with R
  • Derivatives with R
  • Credit Risk Modelling With R
  • Python for Data Science
  • Machine Learning in Finance using Python

Each book includes PDFs, explanations, instructions, data files, and R code for all examples.

Get the Bundle for $39 (Regular $57)
JOIN 30,000 DATA PROFESSIONALS

Free Guides - Getting Started with R and Python

Enter your name and email address below and we will email you the guides for R programming and Python.

Data Science in Finance: 9-Book Bundle

Data Science in Finance Book Bundle

Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.

What's Included:

  • Getting Started with R
  • R Programming for Data Science
  • Data Visualization with R
  • Financial Time Series Analysis with R
  • Quantitative Trading Strategies with R
  • Derivatives with R
  • Credit Risk Modelling With R
  • Python for Data Science
  • Machine Learning in Finance using Python

Each book comes with PDFs, detailed explanations, step-by-step instructions, data files, and complete downloadable R code for all examples.