Thanks in advance for your help.
I am using R and let’s say that I have a datatable (or eventually a timeseries with zoo) with the following format:
Col1: time Values
Day1 H1 Value
Day1 H2 Value
Day1 H3 Value
Day1 H4 Value
Day2 H1 Value
Day2 H2 Value
Day2 H3 Value
Day2 H4 Value
Day3 H1 Value
...
Let’s say that I would like to construct a matrix with the following format:
Rows:Days
H1 H2 H3 H4
D1 Values Values Values Values
D2 Values Values Values Values
D3 Values Values Values Values
and also:
average(H1,H2) average(H3,H4)
D1 Values Vales
D2 Values Vales
D3 Values Vales
In some languages such as C++ we would probably proceed with a double ‘for’ but I am not sure it is the best way to proceed here.
Thanks a lot, I am new to R and I am quite lost with the different logic (but very interesting one).
I have seen other questions on the topic but I am not clear at all.
This can be done with some basic
reshapework, andaggregate()orwithin()for the means:First, some sample data is very helpful:
Second, use
reshapeto go from long to wide formatThird, use
rowMeanson the desired subset of your columns. You can also useaggregateif you prefer, but this is a convenient way to transform your originaldata.frame.