Reputation: 358
Open/Close Minute Price Data. Next Minute open is always equal to previous minute close. Exemplary data set:
dt open close
1998-01-02 09:30:00 100 101
1998-01-02 09:31:00 101 102
...
1998-01-02 15:59:00 105 106
After the last row I want to add another row like this:
dt open close
1998-01-02 09:30:00 100 101
1998-01-02 09:31:00 101 102
...
1998-01-02 15:59:00 105 106
1998-01-02 16:00:00 106 NA
That is, the timestamp is increased by one minute, open is equal to previous minute close and close is NA. My naive approach does not work:
library(lubridate)
library(dplyr)
data <- add_row(data, dt = max("dt") + minute(1), open = close[[n()]])
Any ideas?
Upvotes: 0
Views: 111
Reputation: 887148
We could also use bind_rows
library(tidyverse)
data %>%
summarise(dt = max(dt) + minutes(1), open = last(close)) %>%
bind_rows(data, .)
# A tibble: 4 x 3
# dt open close
# <dttm> <int> <int>
#1 1998-01-02 09:30:00 100 101
#2 1998-01-02 09:31:00 101 102
#3 1998-01-02 15:59:00 105 106
#4 1998-01-02 16:00:00 106 NA
Upvotes: 0
Reputation: 17299
First, you should use minutes
(create time periods) instead of minute
(used to get minute component of a date-time). Second, in add_row
you could not refer to columns in data
with string or column name as in other dplyr
functions.
One way to do it is:
> data <- data %>% add_row(dt = max(.$dt) + minutes(1), open = last(.$close))
> data
# A tibble: 4 x 3
dt open close
<dttm> <int> <int>
1 1998-01-02 09:30:00 100 101
2 1998-01-02 09:31:00 101 102
3 1998-01-02 15:59:00 105 106
4 1998-01-02 16:00:00 106 NA
Where the sample data
is:
> dput(data)
structure(list(dt = structure(c(883733400, 883733460, 883756740
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), open = c(100L,
101L, 105L), close = c(101L, 102L, 106L)), row.names = c(NA,
-3L), class = c("tbl_df", "tbl", "data.frame"))
Upvotes: 3