Skip to content

Instantly share code, notes, and snippets.

@jlehtoma
Created May 13, 2013 13:58
Show Gist options
  • Save jlehtoma/5568494 to your computer and use it in GitHub Desktop.
Save jlehtoma/5568494 to your computer and use it in GitHub Desktop.
Reading and subsetting data from csv files
library(plyr)
# Generate some data ------------------------------------------------------
set.seed(66)
# Define the folder from which to read
csv.folder <- "PATH_TO_CSV_FOLDER"
# Number of hila elements
n.hila <- 10
# Number of Julian days in a year
jdays <- 365
# How many elements over all?
n <- n.hila * jdays
# Template:
# col.names = c("hila", "year", "temp", "rain", "jdate"))
y2001 <- data.frame(hila = rep(1:n.hila, jdays), year = 2001, temp = rnorm(n),
rain = runif(n, 0, 1000), jdate=rep(1:jdays, each=n.hila))
y2002 <- data.frame(hila = rep(1:n.hila, jdays), year = 2002, temp = rnorm(n),
rain = runif(n, 0, 1000), jdate=rep(1:jdays, each=n.hila))
# Save the data for later reading
write.csv(y2001, file.path(csv.folder, "y2001.csv"))
write.csv(y2002, file.path(csv.folder, "y2002.csv"))
# Read and process data --------------------------------------------------
# List all the csv files in the folder
csv.files <- list.files(csv.folder, "\\.csv$")
# Create an empty list to hold the content of each csv file
csv.files.data <- list()
# Loop through all the csv files
for (csv.file in csv.files) {
# Read the content
dat <- read.csv(csv.file)
# Subset only particular hila elements
dat <- subset(dat, hila == 5 | hila == 7)
# Subset only a particular range of Julian days
dat <- subset(dat, jdate >= 163 & jdate <= 181)
# 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)
# Summarise for particular hila elements
day.summary <- ddply(all.data, .(hila), summarise,
mean.temp=mean(temp),
mean.perc=mean(rain))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment