Skip to content

Instantly share code, notes, and snippets.

@ChristinaLK
Last active July 11, 2017 11:37
Show Gist options
  • Save ChristinaLK/37ecc5168c77ee93ad9a00704428121c to your computer and use it in GitHub Desktop.
Save ChristinaLK/37ecc5168c77ee93ad9a00704428121c to your computer and use it in GitHub Desktop.
Looping over spreadsheets to combine into a single dataframe
library(dplyr)
# create empty dataframe
df <- data.frame(Domain=character(),
Total=integer(),
Date=character())
# loop thru data files + add to data frame
for (date in c('2012-2013','2014-2015','2016-2017')) {
# set data file path + read in data
data <- load_data(date=date, time=365)
# summarize pieces I need + add date identifier column
data_summary <- data %>%
select(Domain, CHTC_Total) %>%
group_by(Domain) %>%
summarize(Total = sum(CHTC_Total)) %>%
mutate(Date = date)
# add to master df
df <- rbind(df, data_summary)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment