Rush
Rush

Reputation: 1

Groupby timestamp data in r

I am new to R and need help in the following. I have this data:

  Time Temperature
  4.460672    96.32332
 15.542545    96.32332
 16.750386    96.32332
 38.709795    96.32332
 39.952442    96.32332
 41.320898    96.32332
 55.375259    96.32332
 61.855321   100.47791
 66.464590   100.47791
 66.966965   100.47791
 76.100513   100.47791
 76.617365   100.47791
 77.186545   100.47791
 83.032157   100.47791
 87.772441   100.47791
 91.786988   100.47791
 98.161933   100.47791
101.258411   100.47791
105.186097   100.47791
105.928643   100.47791
111.476967   100.47791
119.426046   100.47791
124.406232    92.70218
135.352858    92.70218
136.545958    92.70218

I need to create a dataframe ‘minute_data’. Each row corresponds to one minute of observations, it contains count of events during that minute and the temperature.

Some minutes may not contain any events. Rows corresponding to such minutes should be excluded from the dataframe.

The dataframe should look like (first 2 rows):

minute count temperature
  1     7    96.32332
  2    15   100.47791

Upvotes: 0

Views: 92

Answers (2)

Wimpel
Wimpel

Reputation: 27732

And (of course) data.table can also perform this operation

sample data

library( data.table )
DT <- fread("Time Temperature
4.460672    96.32332
15.542545    96.32332
16.750386    96.32332
38.709795    96.32332
39.952442    96.32332
41.320898    96.32332
55.375259    96.32332
61.855321   100.47791
66.464590   100.47791
66.966965   100.47791
76.100513   100.47791
76.617365   100.47791
77.186545   100.47791
83.032157   100.47791
87.772441   100.47791
91.786988   100.47791
98.161933   100.47791
101.258411   100.47791
105.186097   100.47791
105.928643   100.47791
111.476967   100.47791
119.426046   100.47791
124.406232    92.70218
135.352858    92.70218
136.545958    92.70218")

code

It is actually a one-liner, but for readability I added some linebreaks.

DT[, list( count = .N, 
           temperature = mean( Temperature ) ), 
   by = .( minute = floor( Time / 60 ) + 1 )]

output

#    minute count temperature
# 1:      1     7    96.32332
# 2:      2    15   100.47791
# 3:      3     3    92.70218

Upvotes: 1

Sonny
Sonny

Reputation: 3183

You can use dplyr for this:

library(dplyr)
df %>%
  mutate(minute = Time %/% 60 + 1) %>%
  group_by(minute) %>%
  summarise(count = n(),
            temperature = first(temperature)) %>%
  select(-Time)

Note: Change first to mean if you want mean

Upvotes: 1

Related Questions