Skip to content

Instantly share code, notes, and snippets.

@clarkfitzg
Created September 13, 2017 22:41
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 clarkfitzg/572037415c5150da143ae6bd0220c799 to your computer and use it in GitHub Desktop.
Save clarkfitzg/572037415c5150da143ae6bd0220c799 to your computer and use it in GitHub Desktop.
#' Split And Append Results To CSV File
#'
#' x will be split by f and each group will be appended to a directory of
#' csv files named according to f
#'
#' @param x data frame to split
#' @param f factor defining splits
#' @param ... further arguments to split
#' @param dirname character directory, will be created if doesn't exist
#' @return NULL
#' @export
split_write = function(x, f, dirname, ...)
{
sp = split(x, f, ...)
yesdir = dir.exists(dirname)
if(!yesdir) dir.create(dirname)
allfiles = paste0(dirname, "/", names(sp), ".csv")
mapply(write.table, sp, allfiles, MoreArgs = list(append = yesdir, col.names = !yesdir, sep = ","))
NULL
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment