我有股票价格数据,我的数据框如下所示:
Time Price
2018-02-21 09:00:00am 122.12
2018-02-21 09:07:38am 122.43
2018-02-21 09:09:10am 122.44
2018-02-21 09:09:10am 122.45
2018-02-21 09:09:21am 122.26
2018-02-21 09:13:16am 122.37
....
为了对此时间序列数据进行汇总和滑动,我需要将每个丢失的分钟替换为price == 000.00,因此我想要这样的输出
Time Price
2018-02-21 09:00:00am 122.12
2018-02-21 09:01:00am 000.00
2018-02-21 09:02:00am 000.00
2018-02-21 09:03:00am 000.00
2018-02-21 09:04:00am 000.00
........
2018-02-21 09:07:38am 122.43
2018-02-21 09:08:00am 000.00
2018-02-21 09:09:10am 122.44
2018-02-21 09:09:10am 122.45
2018-02-21 09:09:21am 122.26
2018-02-21 09:13:16am 122.37
....
我尝试过,但是卡住了
答案 0 :(得分:0)
可能有更好的方法,但是可以使用library(shiny)
library(shinyWidgets)
ui <- fluidPage(
pickerInput(
inputId = "my_input",
label = "TEST",
choices = 1:10, # provide choices - integers from 1 to 10
selected = 1:10, # select - integers from 1 to 10
options = list(`actions-box` = TRUE), # build buttons for collective selection
multiple = T
)
)
server <- function(input, output) {}
shinyApp(ui = ui, server = server)
:
dplyr
假定原始数据帧称为library(dplyr)
,并确保日期时间列未存储为字符串或因子。还创建第二列将秒数舍入为零。这仅用于在下一步中与完整的时间序列数据帧合并。保留秒的原始时间:
df_missing
以分钟为单位,以完整的顺序创建一个数据帧,从开始时间到结束时间。在此示例中,我假设时间范围是原始数据中最早的时间到最近的时间,但也可以手动设置开始和结束时间:
df_missing <- df_missing %>%
mutate(Time = as.POSIXct(Time, format="%Y-%m-%d %H:%M:%S")) %>%
mutate(time_merge = as.POSIXct(round(Time, "mins")))
然后将缺少的数据加入完整的数据框,以确定哪些时间有价格,然后用零填充缺失的值:
df_complete <- data_frame(time_merge = seq.POSIXt(min(df_missing$Time), max(df_missing$Time), by="min"))