Question

Suppose this is the dataframe (dt):

        datetime        price1    price2
2011-01-04 22:00:20       1         7
2011-01-04 22:01:37       2         8 
2011-01-04 22:01:57       3         9
2011-01-04 22:03:03       4        10
2011-01-04 22:03:32       5        11
2011-01-04 22:03:45       6        12

I want to split the data by datetime (1 min intervals) and find:

For price1: the last observation minus the first.

For price2: sum of all prices.

        time           difference    sum
2011-01-04 22:00:00      1-1=0           7
2011-01-04 22:01:00      3-2=1          17
2011-01-04 22:03:00      6-4=2          33


dt$time <- cut(dt$datetime, "1 min")
dt1min<-ddply(dt,.(time), function(x))

How should I define the function(x)? Thanks.

Was it helpful?

Solution

Here is my solution using the data.table package:

library(data.table)

dt <- data.table(datetime= c("2011-01-04 22:00:20",
                             "2011-01-04 22:01:37",
                             "2011-01-04 22:01:57",
                             "2011-01-04 22:03:03",
                             "2011-01-04 22:03:32",
                             "2011-01-04 22:03:45"),
                 price1 = c(1,2,3,4,5,6),
                 price2 = c(7,8,9,10,11,12))

dt[, datetime:= as.POSIXct(datetime)] # convert character to timestamp
dt[, time:= format(datetime, "%Y-%m-%d %H:%M")] # create time column

# now split apply,combine
dt[, list(difference = price1[datetime==max(datetime)] - price1[datetime==min(datetime)],
          sum        = sum(price2)),
   by = time]

the output is:

              time difference sum
1: 2011-01-04 22:00          0   7
2: 2011-01-04 22:01          1  17
3: 2011-01-04 22:03          2  33
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top