Creating the CRN Data Set

Now that we’ve got the data downloaded and a set of functions to process the files, let’s create the data frame for the data and a meta data frame for general site data (name, lat , long ect). I also wanted to keep track of how many years of data there were for each site and to order them by the longest number of years (the order column). This was so I could look at the largest data sets first.


library(zoo)
#directory where the function code was stored
dir <- "~/Documents/CRN-R code"
setwd(dir)
source("CRNFunctions.R")

#directory where the data is stored
dir <- "~/Documents/CRN"
setwd(dir)

# don't automatically make strings into factors
options( stringsAsFactors = F)

# extract a list of all the sites
crnsites <- getsites()

#make a list of the filepaths
crnfilepaths <- getfilepaths()

# for each site make a list of the files for that site
crnfiles <- lapply(crnsites ,FUN = getsitefilenames,filepaths = crnfilepaths)
#now build up a list of each sites data
crnlist <- lapply(crnfiles,getsitefiles)

site.years <- as.numeric(lapply(crnfiles, function(x) length(x)))

#create a data frame of the meta data for the sites
crnmeta <- NULL
crnmeta <- sapply(crnlist, function(x) x$meta)

crnmeta <- data.frame(t(crnmeta), site.years, row.names = NULL)

#get the order for largest number of years to smallest
crnorder <- order(site.years, decreasing = T)
crnmeta = cbind(crnmeta, crnorder)
names(crnmeta) <- c("name", "lat", "lon", "years", "order")
crnmeta$lat <- as.double(crnmeta$lat)
crnmeta$lon <- as.double(crnmeta$lon)
# order the meta data to get a list of the longest sites
crn.longest <- crnmeta[crnmeta$order,]
crn.top <- which(crnmeta$name %in% crn.longest$name[1:10])

crndata <- lapply(crnlist, function(x) x$data)

crndata.longest <- crndata[crn.top]

Advertisements

Leave a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s