当前时间的最大函数
max function for the current time
如何在时间之后创建一个包含当日最高价的列?我使用了 pmax 但它没有帮助。
> df
date_time Price
1 2018-02-01 18:13:17 3178.5
2 2018-02-01 18:13:13 3179.0
3 2018-02-01 18:13:13 3178.5
4 2018-02-01 18:13:13 3178.5
5 2018-02-01 18:13:09 3178.5
6 2018-02-01 18:13:00 3178.5
7 2018-02-01 18:12:54 3179.5
8 2018-02-01 18:12:43 3179.5
我正在尝试这样做。
> mutate(df, high_day = ?)
date_time Price high_day
1 2018-02-01 18:13:17 3179.5 3179.5
2 2018-02-01 18:13:13 3179.0 3179.0
3 2018-02-01 18:13:13 3178.5 3179.0
4 2018-02-01 18:13:13 3179.0 3179.0
5 2018-02-01 18:13:09 3178.5 3178.5
6 2018-02-01 18:13:00 3178.5 3178.5
7 2018-02-01 18:12:54 3178.5 3178.5
8 2018-02-01 18:12:43 3178.5 3178.5
复制:
structure(list(data_hora = structure(c(1517508797, 1517508793,
1517508793, 1517508793, 1517508789, 1517508780, 1517508774, 1517508763,
1517508763, 1517508763, 1517508763, 1517508757, 1517508756), class = c("POSIXct",
"POSIXt"), tzone = ""), Preco = c(3178.5, 3179, 3178.5, 3178.5,
3178.5, 3178.5, 3179.5, 3179.5, 3179.5, 3179.5, 3179, 3179, 3179.5
)), row.names = 38:50, class = "data.frame")
怎么样:
library(lubridate)
df %>%
arrange(date_time) %>%
group_by(date = as.Date(date_time)) %>%
mutate(day_high = cummax(Price))
如何在时间之后创建一个包含当日最高价的列?我使用了 pmax 但它没有帮助。
> df
date_time Price
1 2018-02-01 18:13:17 3178.5
2 2018-02-01 18:13:13 3179.0
3 2018-02-01 18:13:13 3178.5
4 2018-02-01 18:13:13 3178.5
5 2018-02-01 18:13:09 3178.5
6 2018-02-01 18:13:00 3178.5
7 2018-02-01 18:12:54 3179.5
8 2018-02-01 18:12:43 3179.5
我正在尝试这样做。
> mutate(df, high_day = ?)
date_time Price high_day
1 2018-02-01 18:13:17 3179.5 3179.5
2 2018-02-01 18:13:13 3179.0 3179.0
3 2018-02-01 18:13:13 3178.5 3179.0
4 2018-02-01 18:13:13 3179.0 3179.0
5 2018-02-01 18:13:09 3178.5 3178.5
6 2018-02-01 18:13:00 3178.5 3178.5
7 2018-02-01 18:12:54 3178.5 3178.5
8 2018-02-01 18:12:43 3178.5 3178.5
复制:
structure(list(data_hora = structure(c(1517508797, 1517508793,
1517508793, 1517508793, 1517508789, 1517508780, 1517508774, 1517508763,
1517508763, 1517508763, 1517508763, 1517508757, 1517508756), class = c("POSIXct",
"POSIXt"), tzone = ""), Preco = c(3178.5, 3179, 3178.5, 3178.5,
3178.5, 3178.5, 3179.5, 3179.5, 3179.5, 3179.5, 3179, 3179, 3179.5
)), row.names = 38:50, class = "data.frame")
怎么样:
library(lubridate)
df %>%
arrange(date_time) %>%
group_by(date = as.Date(date_time)) %>%
mutate(day_high = cummax(Price))