Table: group by datetime
Mostra commenti meno recenti
Hi all,
I have defined a table and I now want to group by a column of type 'datetime'. I tried varfun, but this function doesn't work for this kind. Can someone help? Thanks a lot! So table looks like:
[TYPE Datetime VALUE]
And the result should be the value per datetime, summed by TYPE.
4 Commenti
Guillaume
il 11 Giu 2015
varfun does not care about the type of the column, it just passes it to whatever function you give it. It works fine with datetime. In any case, varfun does not sound like the function you need
Peter Perkins
il 11 Giu 2015
Arjan, this
[TYPE Datetime VALUE]
is (probably) not a table in the sense of the MATLAB table datatype, which is what varfun expects. This
t = table(TYPE,Datetime,VALUE)
would be. Can you clarify exactly what you have?
Arjan
il 12 Giu 2015
Peter Perkins
il 16 Lug 2015
Arjan, I don't exactly understand your table, but I think your question boils down to, "I want to use varfun, with a grouping variable that's a datetime."
In R2014b and R2015a, varfun does not allow that. One work-around would be to temporarily convert the datetime to a numeric representation (for example, the number of seconds since some reference time). Then do the grouped calculation, and convert the grouping variable's unique values back to datetime:
>> d = datetime(2015,1,1) + caldays(randi(2,5,1));
>> x = randn(size(d));
>> t = table(d,x)
t =
d x
___________ ________
02-Jan-2015 0.30352
03-Jan-2015 -0.60033
02-Jan-2015 0.48997
03-Jan-2015 0.73936
02-Jan-2015 1.7119
>> d0 = datetime(2015,1,1);
>> t.d = seconds(t.d - d0)
t =
d x
_________ ________
86400 0.30352
1.728e+05 -0.60033
86400 0.48997
1.728e+05 0.73936
86400 1.7119
>> gt = varfun(@mean,t,'groupingVariable','d')
gt =
d GroupCount mean_x
_________ __________ ________
86400 86400 3 0.83512
172800 1.728e+05 2 0.069518
>> gt.d = d0 + seconds(gt.d)
gt =
d GroupCount mean_x
____________________ __________ ________
86400 02-Jan-2015 00:00:00 3 0.83512
172800 03-Jan-2015 00:00:00 2 0.069518
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Tables in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!