[R] splitting time vector into days
jim holtman
jholtman at gmail.com
Tue Sep 9 23:04:20 CEST 2008
Here is one way of doing it:
> x <- data.frame(dates=seq(as.POSIXct('2008-09-08'), by='7 hours', length=10),
+ values=1:10)
> # split into days
> x.s <- split(x, format(x$dates, "%Y%m%d"))
> x.s
$`20080908`
dates values
1 2008-09-08 00:00:00 1
2 2008-09-08 07:00:00 2
3 2008-09-08 14:00:00 3
4 2008-09-08 21:00:00 4
$`20080909`
dates values
5 2008-09-09 04:00:00 5
6 2008-09-09 11:00:00 6
7 2008-09-09 18:00:00 7
$`20080910`
dates values
8 2008-09-10 01:00:00 8
9 2008-09-10 08:00:00 9
10 2008-09-10 15:00:00 10
> lapply(x.s, function(.df) mean(.df$values))
$`20080908`
[1] 2.5
$`20080909`
[1] 6
$`20080910`
[1] 9
>
On Tue, Sep 9, 2008 at 3:25 PM, Alexy Khrabrov <deliverable at gmail.com> wrote:
> Greetings -- I have a dataframe a with one element a vector, time, of
> POSIXct values. What's a good way to split the data frame into periods of
> a$time, e.g. days, and apply a function, e.g. mean, to some other column of
> the dataframe, e.g. a$value?
>
> Cheers,
> Alexy
>
> ______________________________________________
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list