Blind0ne
Blind0ne

Reputation: 1037

Group items based on timestamp

I have a dataframe with unique rows like this:

df <- unique(data.frame(Timestamp=as.POSIXct(strptime(c('2018-01-08 13:26:53', 
                                                 '2018-01-08 13:33:33', 
                                                 '2018-01-08 13:45:12', 
                                                 '2018-01-08 13:45:12', 
                                                 '2018-01-08 14:28:34', 
                                                 '2018-01-08 14:31:32',
                                                 '2018-01-08 14:31:32',
                                                 '2018-01-08 15:13:16',
                                                 '2018-01-08 15:25:19',
                                                 '2018-01-08 15:25:19',
                                                 '2018-01-08 15:25:19',
                                                 '2018-01-08 15:25:19'), "%Y-%m-%d %H:%M:%OS")),
                 Text=c('A', 'B', 'C', 'A', 'B', 'A', 'C', 'D', 'A', 'A', 'B', 'C')))

Output:

             Timestamp Text
1  2018-01-08 13:26:53    A
2  2018-01-08 13:33:33    B
3  2018-01-08 13:45:12    C
4  2018-01-08 13:45:12    A
5  2018-01-08 14:28:34    B
6  2018-01-08 14:31:32    A
7  2018-01-08 14:31:32    C
8  2018-01-08 15:13:16    D
9  2018-01-08 15:25:19    A
11 2018-01-08 15:25:19    B
12 2018-01-08 15:25:19    C

I would like to group Items which have the same timestamp and always keep the same order of grouped items if it appeared once. So my result should look like this:

            Timestamp Text
1 2018-01-08 13:26:53    A
2 2018-01-08 13:33:33    B
3 2018-01-08 13:45:12    C | A
5 2018-01-08 14:28:34    B
7 2018-01-08 14:31:32    C | A
8 2018-01-08 15:13:16    D
9 2018-01-08 15:25:19    C | A | B

Any efficient suggestions? Thanks in advance!

Upvotes: 0

Views: 59

Answers (1)

Hunaidkhan
Hunaidkhan

Reputation: 1418

you can do it using base R

df <- unique(data.frame(Timestamp=as.POSIXct(strptime(c('2018-01-08 13:26:53', 
                                                        '2018-01-08 13:33:33', 
                                                        '2018-01-08 13:45:12', 
                                                        '2018-01-08 13:45:12', 
                                                        '2018-01-08 14:28:34', 
                                                        '2018-01-08 14:31:32',
                                                        '2018-01-08 14:31:32',
                                                        '2018-01-08 15:13:16',
                                                        '2018-01-08 15:25:19',
                                                        '2018-01-08 15:25:19',
                                                        '2018-01-08 15:25:19',
                                                        '2018-01-08 15:25:19'), "%Y-%m-%d %H:%M:%OS")),
                        Text=c('A', 'B', 'C', 'A', 'B', 'A', 'C', 'D', 'A', 'A', 'B', 'C')))



agg<- aggregate(df[,2], list(df[,1]), function(x) paste0(unique(x)))

Upvotes: 1

Related Questions