Skip to content

Instantly share code, notes, and snippets.

@gmbecker
Created March 30, 2015 19:26
Show Gist options
  • Save gmbecker/aed2e98baed0991a9f5c to your computer and use it in GitHub Desktop.
Save gmbecker/aed2e98baed0991a9f5c to your computer and use it in GitHub Desktop.
"Fast" binary category column generation using only R code. Response to http://rstatistics.net/strategies-to-speed-up-r-code/
col1 <- runif (12^6, 0, 2)
col2 <- rnorm (12^6, 0, 2)
col3 <- rpois (12^6, 3)
col4 <- rchisq (12^6, 2)
df <- data.frame (col1, col2, col3, col4)
## process the rows
## slightly less than 3 times slower than Rcpp example here http://rstatistics.net/strategies-to-speed-up-r-code/ (on my machine)
## but written only in R
system.time({
want = which(rowSums(df) > 4)
output = rep("less than 4", times = nrow(df))
output[want] = "greater than 4"
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment