Skip to content

Instantly share code, notes, and snippets.

@djhocking
Last active November 9, 2018 04:01
Show Gist options
  • Save djhocking/f743532d2d7bbd8d3ca745676cd80126 to your computer and use it in GitHub Desktop.
Save djhocking/f743532d2d7bbd8d3ca745676cd80126 to your computer and use it in GitHub Desktop.
Subset and Save
# make fake dataset
df <- data.frame(x = runif(100, 0, 1), y = rnorm(100, 10, 3), z = rpois(100, 10))
# subset dataframe
df_sub <- df[which(df$x >= 0.75), ]
# subset using dplyr
library(dplyr)
df_sub2 <- df %>%
filter(x >= 0.75)
df_list <- list(sub1 = df_sub, sub2 = df_sub2)
# Create a new directory for output if it doesn't exist
if(!dir.exists("Output/Large")) dir.create("Output/Large", recursive = TRUE)
if(!dir.exists("Output/Small")) dir.create("Output/Small", recursive = TRUE)
# Save subset of data as csv to location based on size of subset
for(i in 1:length(df_list)) {
# currently will throw an error I think if the list is empty or has a blank space
if(ncol(df_list[[i]] >= 20) {
write.csv(df_sub, file = paste0("Output/Large/", names(df_list)[i], ".csv"), row.names = FALSE)
} else {
write.csv(df_sub, file = paste0("Output/Small/", names(df_list)[i], ".csv"), row.names = FALSE)
}
# save subset as RData
save(df_sub, file = "Output/subset1.RData")
# save subset 2 as RDS
saveRDS(df_sub2, file = "Output/subset2.rds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment