Это легко объясняется:
Итак:
def x(a=0, b=[], c=[], d=0):
a = a + 1
b = b + [1]
c.append(1)
print a, b, c
a
не изменяется - каждый вызов назначения создает новый объект int - печатается новый объект b
не изменяется - новый массив создается из значения по умолчанию и печатается c
изменения - операция выполняется на одном и том же объекте - и печатается Я удалил свой комментарий, я понимаю, что вы собираетесь после второго прочтения.
Это довольно просто, используя rleid()
из data.table
library(data.table)
## Note: store times as POSIXct instead of POSIXlt for drastic performance improvement
time <- c(as.POSIXct("2017-02-03 09:00:01"),
as.POSIXct("2017-02-03 09:00:03"),
as.POSIXct("2017-02-03 09:00:06"),
as.POSIXct("2017-02-03 09:00:09"),
as.POSIXct("2017-02-03 09:00:10"),
as.POSIXct("2017-02-03 09:00:20"),
as.POSIXct("2017-02-03 09:00:23"),
as.POSIXct("2017-02-03 09:00:34"),
as.POSIXct("2017-02-03 09:00:44"),
as.POSIXct("2017-02-03 09:01:07"))
price <- c(100, 100, 100, 99, 98, 99, 100, 101, 101, 100)
data <- data.frame(time, price)
## Convert to a data.table
setDT(data)
## Create a summary using a generated counter on the fly with
## the `rleid` function from data.table to group consecutive
## sequences together and then operate by group. the `.N`
## operator is another special symbol in data.table
## that we can use to return the number of rows in each group
## here. See ?special-symbols to learn more
Summary <- data[, .(start = first(time),
end = last(time),
nbr_records = .N,
duration_sec = as.numeric(last(time)) - as.numeric(first(time))
), by = .(Counter = data.table::rleid(price))]
## Drop the Counter variable assuming you don't need it
Summary[,Counter := NULL]
## Results
print(Summary)
# start end nbr_records duration_sec
# 1: 2017-02-03 09:00:01 2017-02-03 09:00:06 3 5
# 2: 2017-02-03 09:00:09 2017-02-03 09:00:09 1 0
# 3: 2017-02-03 09:00:10 2017-02-03 09:00:10 1 0
# 4: 2017-02-03 09:00:20 2017-02-03 09:00:20 1 0
# 5: 2017-02-03 09:00:23 2017-02-03 09:00:23 1 0
# 6: 2017-02-03 09:00:34 2017-02-03 09:00:44 2 10
# 7: 2017-02-03 09:01:07 2017-02-03 09:01:07 1 0