So I’ve looked at this and this, and can’t seem to find a good example of a way to loop or iterate over a factor (or are their other / better ways to do this?). I have a data frame for which I have:
> head(frame)
x1 x2 DateTime
1 100 5 2010-06-01 05:32:46
2 105 3 2010-06-01 05:32:23
3 47 20 2010-06-01 05:32:34
4 56 6 2010-06-01 05:33:16
5 98 11 2010-06-01 05:54:12
6 84 9 2010-06-01 05:54:05
and I can create a factor based on time like so: fact <- cut(frame$DateTime, "1 hour") from there, how would I go about extracting the first and last elements of frame$x2 given the factor I’ve created? (or for that matter, the nth element of the cut).
would it be something like:
test <- split(frame$x2, fact)
I’m not sure what you mean by “extract” the nth element (i.e. do you want it in the same object, a new object, do you want something the same length as the original object, etc.).
I would use
avebecause it operates on factor groups. Note that this assumes that your data.frame is sorted byframe$DateTime.