Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201945

Data grouping comparable to Pandas group-by with grouper

$
0
0

I have dataset of hierarchical events where there is one row for one event.

TIME               level1   level2  Occurrence
29/11/2019 00:05    A       a       1
29/11/2019 00:05    B       a       1
29/11/2019 00:07    B       b       1
29/11/2019 00:20    B       b       1
29/11/2019 00:05    B       c       1
29/11/2019 01:20    A       a       1
29/11/2019 01:25    A       a       1
29/11/2019 02:00    A       a       2
29/11/2019 02:00    B       a       1
29/11/2019 02:00    B       b       1
29/11/2019 02:35    B       b       1
29/11/2019 02:49    B       c       1

I am aggregating it with Pandas groupby and grouper to get an output as below

df_agg = df.groupby([pd.Grouper(freq='H'), 'level1', pd.Grouper('level2')])
df_agg.count()
TIME               level1   level2  Count
29/11/2019 00:00    A       a       1
                    B       a       1
                    B       b       2
                    B       c       1
29/11/2019 01:00    A       a       2
29/11/2019 02:00    A       a       2
                    B       a       1
                    B       b       2
                    B       c       1

Can I achieve something similar in R?

I am attaching a commands to create the dataset similar to what i am working

dict = {"TIME" : ['29/11/2019  00:05:00', '29/11/2019  00:05:00', '29/11/2019  00:07:00', '29/11/2019  00:20:00',
                 '29/11/2019  00:05:00', '29/11/2019  01:20:00', '29/11/2019  01:25:00', '29/11/2019  02:00:00',
                 '29/11/2019  02:00:00', '29/11/2019  02:00:00', '29/11/2019  02:35:00', '29/11/2019  02:49:00'],
        "level1" : ["A", "B", "B", "B", "B", "A", "A", "A", "B","B", "B", "B"],
        "level2" : ["a", "a", "b", "b", "c", "a", "a", "a", "a", "b", "b","c"]}

tmp_df = pd.DataFrame(dict)
tmp_df = tmp_df.set_index('TIME')
tmp_df.index = pd.to_datetime(tmp_df.index)

Viewing all articles
Browse latest Browse all 201945

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>