I have a csv with four columns: site ID, latitude, longitude, and a date. I need to calculate the average daily temperature and total daily precipitation for each site at each date. I am trying to use the package “daymetr”.
site | lat | lon | date |
---|---|---|---|
d22002 | 35.1527 | -84.0396 | 11/8/2017 |
d22002 | 35.1527 | -84.0396 | 11/1/2017 |
d22066 | 36.4246 | -84.4666 | 11/15/2017 |
d22065 | 35.2741 | -81.5184 | 1/22/2017 |
All my dates fall during 2017, so I used the following code to extract data:
site_weather2017 <- download_daymet_batch(
file_location = "templateforweatherdata.csv",
start = 2017,
end = 2017,
internal = TRUE,
force = FALSE,
silent = FALSE,
path = tempdir(),
simplify = FALSE
)
Which gives me a daymetr object with 365 days of temp/precip data for each lat/lon location. How do I narrow it down to match just the date specified in each column?
I have tried this code:
finaldata <- list()
for( i in 1:length(site_weather2017)){
site <- site_weather2017[[i]]
site.dat <- site$data
#add a column of mean daily temperature
site.dat["tmean"] <- rowMeans(site.dat[,c("tmax..deg.c.","tmin..deg.c.")])
#choose timespan of interest
beginDate <- yday(as.Date(format="%m-%d-%Y"))
# s.Date(alldata$Date, format = "%Y-%m-%d")
#subset to only timespan of interest
subSite <- subset(site.dat, yday %in% beginDate:beginDate)
subSite['precip'] <- subSite$prcp..mm.day.
#calculate mean daily temperature, and sum of precipitation
finaldata[[i]] <- data.frame(site=site$site, meanTemp=mean(subSite$tmean),
sumPrecip=base::sum(subSite$prcp..mm.day.))
}
But temp and precip show up as NaN.