Skip to content

Instantly share code, notes, and snippets.

@jfaganUK
Created October 15, 2014 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfaganUK/0f0e26202aefc0da04f5 to your computer and use it in GitHub Desktop.
Save jfaganUK/0f0e26202aefc0da04f5 to your computer and use it in GitHub Desktop.
sqldf random filtering
library(sqldf)
# there is a default dataset called iris that is always loaded into the R scope
# let's say I want to break it into two randomly using sqldf
# assign id's to the rows
my.iris <- iris
my.iris$id <- 1:nrow(my.iris)
ran.split <- data.frame(rs = sample(1:max(my.iris$id), floor(max(my.iris$id) / 2), replace=F))
sqldf("select * from `my.iris` as x inner join `ran.split` as y on x.id = y.rs")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment