Last active
December 17, 2015 07:48
-
-
Save jlehtoma/5575352 to your computer and use it in GitHub Desktop.
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
read.weather.data <- function(folder, hilas, jdate.start, jdate.end, years='All') { | |
# List all the csv files in the folder | |
csv.files <- list.files(folder, "\\.csv$", full.names = TRUE) | |
# Create an empty list to hold the content of each csv file | |
csv.files.data <- list() | |
col.names = c("hila", "year", "x1", "x2", "temp", "rain", "jdate") | |
# Loop through all the csv files | |
for (csv.file in csv.files) { | |
# Read the content | |
dat <- read.csv(csv.file,col.names=col.names) | |
#subset only years that we need | |
#dat<-subset(dat,year >= year.start & jdate <= year.end) | |
# Subset only particular hila elements | |
dat <- subset(dat, hila %in% hilas) | |
# Subset only a particular range of Julian days | |
dat <- subset(dat, jdate >= jdate.start & jdate <= jdate.end) | |
# Add the subsetted data to the list | |
csv.files.data[[csv.file]] <- dat | |
} | |
# Row bind all the subsetted data frames (read from the csv files) into a one | |
# big data frame. This is all the data for the particular hila elements over | |
# all years. | |
all.data <- do.call("rbind", csv.files.data) | |
return(all.data) | |
} | |
# Define the folder from which to read | |
csv.folder <- "c:/temp" | |
dat <- read.weather.data(csv.folder, 5, 100, 110) | |
# Summarise for particular hila elements | |
day.summary <- ddply(dat, .(hila), summarise, | |
mean.temp=mean(temp), | |
mean.perc=mean(rain)) | |
write.table(day.summary, file.path(csv.folder, "results.csv")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment