Hi all, i'm sure there is an easy way to do this, but i'm stumped, so any help would be appreciated. i have a single column of data for precipitation every 15 minutes over a year. i want to sum the precip to daily data. so the first 96 records = the first day, the second 96 records = the second day, and so on.... is there a way to write a for loop that would sum the data to daily, and write each value to a second object so i end up with a file of daily precip? thanx, Janet [[alternative HTML version deleted]]
Hi Janet, One relatively simple way would be to transofrm the data into a 96 x Ndays matrix and use colSums(). Of course, lets say on one day, the measurement tool had technical difficulties and missed two observations, then you only have 94 observations for that day, you will need a fancier solution that deals with time not number of observations. Below is an example. Cheers, Josh ## Imaginary precipitation data for 7 days set.seed(10) x <- rnorm(96*7, 1, .1) ## An alternate way you may have the data stored xalt <- data.frame(precip = x) ## Assuming _no_ missing observations colSums(matrix(x, nrow = 96)) ## alternate version colSums(matrix(xalt$precip, nrow = 96)) On Thu, Feb 17, 2011 at 11:56 AM, Janet Choate <jsc.eco at gmail.com> wrote:> Hi all, > i'm sure there is an easy way to do this, but i'm stumped, so any help would > be appreciated. > > i have a single column of data for precipitation every 15 minutes over a > year. ?i want to sum the precip to daily data. > so the first 96 records = the first day, the second 96 records = the second > day, and so on.... > is there a way to write a for loop that would sum the data to daily, and > write each value to a second object so i end up with a file of daily precip? > > thanx, > Janet > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Janet: The zoo package and aggregate.zoo should do the trick. I have done this many times with these tools. HTH, Stephen On Thu, 2011-02-17 at 11:56 -0800, Janet Choate wrote:> Hi all, > i'm sure there is an easy way to do this, but i'm stumped, so any help would > be appreciated. > > i have a single column of data for precipitation every 15 minutes over a > year. i want to sum the precip to daily data. > so the first 96 records = the first day, the second 96 records = the second > day, and so on.... > is there a way to write a for loop that would sum the data to daily, and > write each value to a second object so i end up with a file of daily precip? > > thanx, > Janet > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On Thu, Feb 17, 2011 at 3:39 PM, Stephen Sefick <ssefick at gmail.com> wrote:> Janet: > > The zoo package and aggregate.zoo should do the trick. ?I have done this > many times with these tools.Here is some code. Suppose precip is the data. We convert it to a zoo object with date/times assuming the first one is at the start of 2000-01-01. Then we aggregate them by day:> library(zoo) > library(chron) > precip <- 1:200 # sample data > z <- zooreg(precip, start = as.chron("2000-01-01 00:00:00"), freq = 96) > z.ag <- aggregate(z, as.Date) > z.ag2000-01-01 2000-01-02 2000-01-03 4656 13872 1572 -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Possibly Parallel Threads
- Function that is giving me a headache- any help appreciated (automatic read )
- Problems with a specific calculate.
- Arranging two different types of ggplot2 plots with axes lined up
- R combining vectors into a data frame but without a continuous common variable
- overlay lattice histograms with goodness-of-fit pdfs