Skip to content

Instantly share code, notes, and snippets.

@bhaskarvk
Last active March 6, 2016 17:37
Show Gist options
  • Save bhaskarvk/88eb30990b1d77f63a5a to your computer and use it in GitHub Desktop.
Save bhaskarvk/88eb30990b1d77f63a5a to your computer and use it in GitHub Desktop.
Some code to test purrr::walk
# Sample df with missing values
df <- data.frame(col1=sample(c('','Y','N'),100,replace = TRUE),
col2=sample(c('', as.character(0:9)),100,replace = TRUE),
stringsAsFactors = FALSE)
cols <- colnames(df) # store colnames
lapply(df,table) # check levels
df.forFor <- df # to be fixed with a for loop
df.forPurrr <- df # to be fixed using purrr::walk
# Correct levels
levels <- list(
col1 = factor(c('N','Y')),
col2 = ordered(as.character(0:9))
)
# impute df.forFor and df.forPurr by
# changing '' to the first level of each column.
# i.e. in col1 '' becomes 'N' and in col2 '' becomes '0'
# impute df.forFor using for loop
for(col in cols) {
df.forFor[,col][which(df.forFor[,col]=='')] <- levels(levels[[col]])[1]
}
# impute df.forPurrr using purrr::walk
purrr::walk(cols,function(col){
df.forPurrr[,col][which(df.forPurrr[,col]=='')] <- levels(levels[[col]])[1]
})
lapply(df.forFor,table) # imputation succeeds
lapply(df.forPurrr,table) # imputation fails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment