Last active
January 15, 2021 12:47
-
-
Save simonthompson99/d3d9eeb5b7347b6b728756753e193f50 to your computer and use it in GitHub Desktop.
[Export tables per factor into a zip file] Export tsv table into a text file for each level in a factor, exports to a zip file #r
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-- function to export tables per factor in a zip | |
writeTablesPerCat <- function(df, cat, fn, zip_flags = "-j", na_string = "", replace = TRUE){ | |
stopifnot(cat %in% colnames(df)) | |
df_ls <- split(df, df[[cat]]) | |
names(df_ls) <- tolower(gsub(" ", "-", names(df_ls))) | |
files <- sapply(names(df_ls), function(x) paste0(tempdir(), "/", basename(fn), "-", x, ".txt")) | |
for(i in names(df_ls)){ | |
write_tsv(df_ls[[i]][,!colnames(df_ls[[i]]) %in% cat], files[i], na = na_string) | |
} | |
if(replace == TRUE){ | |
suppressWarnings(file.remove(paste0(fn, ".zip"))) | |
} | |
zip(paste0(fn, ".zip"), files, flags = zip_flags) | |
file.remove(files) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment