找出一天中最多24小时并乘以当天的数据

时间:2019-06-01 12:44:18

标签: r

我想找到L1的24h最大值,然后将24的最大值乘以带有数据的公式。

我尝试过的事情:

require(dplyr)

    max24h_L1 <-L1 %>%
      group_by(Date = cut(Date, breaks="24 h")) %>% 
      summarize(`L1` = max(`L1`, na.rm =T))

    max24h_L1 <- ((0.714) * ((max24h_L1/L1)-1)^1.231)

数据:

structure(list(Date = structure(1:10, .Label = c("08.04.2019 14:45", 
"08.04.2019 14:50", "08.04.2019 14:55", "08.04.2019 15:00", "08.04.2019 15:05", 
"08.04.2019 15:10", "08.04.2019 15:15", "08.04.2019 15:20", "08.04.2019 15:25", 
"08.04.2019 15:30", "08.04.2019 15:35", "08.04.2019 15:40", "08.04.2019 15:45", 
"08.04.2019 15:50", "08.04.2019 15:55", "08.04.2019 16:00", "08.04.2019 16:05", 
"08.04.2019 16:10", "08.04.2019 16:15", "08.04.2019 16:20", "08.04.2019 16:25", 
"08.04.2019 16:30", "08.04.2019 16:35", "08.04.2019 16:40", "08.04.2019 16:45", 
"08.04.2019 16:50", "08.04.2019 16:55", "08.04.2019 17:00", "08.04.2019 17:05", 
"08.04.2019 17:10", "08.04.2019 17:15", "08.04.2019 17:20", "08.04.2019 17:25", 
"08.04.2019 17:30", "08.04.2019 17:35", "08.04.2019 17:40", "08.04.2019 17:45", 
"08.04.2019 17:50", "08.04.2019 17:55", "08.04.2019 18:00", "08.04.2019 18:05", 
"08.04.2019 18:10", "08.04.2019 18:15", "08.04.2019 18:20", "08.04.2019 18:25", 
"08.04.2019 18:30", "08.04.2019 18:35", "08.04.2019 18:40", "08.04.2019 18:45", 
"08.04.2019 18:50", "08.04.2019 18:55", "08.04.2019 19:00", "08.04.2019 19:05", 
"08.04.2019 19:10", "08.04.2019 19:15", "08.04.2019 19:20", "08.04.2019 19:25", 
"08.04.2019 19:30", "08.04.2019 19:35", "08.04.2019 19:40", "08.04.2019 19:45", 
"08.04.2019 19:50", "08.04.2019 19:55", "08.04.2019 20:00", "08.04.2019 20:05", 
"08.04.2019 20:10", "08.04.2019 20:15", "08.04.2019 20:20", "08.04.2019 20:25", 
"08.04.2019 20:30", "08.04.2019 20:35", "08.04.2019 20:40", "08.04.2019 20:45", 
"08.04.2019 20:50", "08.04.2019 20:55", "08.04.2019 21:00", "08.04.2019 21:05", 
"08.04.2019 21:10", "08.04.2019 21:15", "08.04.2019 21:20", "08.04.2019 21:25", 
"08.04.2019 21:30", "08.04.2019 21:35", "08.04.2019 21:40", "08.04.2019 21:45", 
"08.04.2019 21:50", "08.04.2019 21:55", "08.04.2019 22:00", "08.04.2019 22:05", 
"08.04.2019 22:10", "08.04.2019 22:15", "08.04.2019 22:20", "08.04.2019 22:25", 
"08.04.2019 22:30", "08.04.2019 22:35", "08.04.2019 22:40", "08.04.2019 22:45", 
"08.04.2019 22:50", "08.04.2019 22:55", "08.04.2019 23:00", "08.04.2019 23:05", 
"08.04.2019 23:10", "08.04.2019 23:15", "08.04.2019 23:20", "08.04.2019 23:25", 
"08.04.2019 23:30", "08.04.2019 23:35", "08.04.2019 23:40", "08.04.2019 23:45", 
"08.04.2019 23:50", "08.04.2019 23:55", "09.04.2019 00:00", "09.04.2019 00:05", 
"09.04.2019 00:10", "09.04.2019 00:15", "09.04.2019 00:20", "09.04.2019 00:25", 
"09.04.2019 00:30", "09.04.2019 00:35", "09.04.2019 00:40", "09.04.2019 00:45", 
"09.04.2019 00:50", "09.04.2019 00:55", "09.04.2019 01:00", "09.04.2019 01:05", 
"09.04.2019 01:10", "09.04.2019 01:15", "09.04.2019 01:20", "09.04.2019 01:25", 
"09.04.2019 01:30", "09.04.2019 01:35", "09.04.2019 01:40", "09.04.2019 01:45", 
"09.04.2019 01:50", "09.04.2019 01:55", "09.04.2019 02:00", "09.04.2019 02:05", 
"09.04.2019 02:10", "09.04.2019 02:15", "09.04.2019 02:20", "09.04.2019 02:25", 
"09.04.2019 02:30", "09.04.2019 02:35", "09.04.2019 02:40", "09.04.2019 02:45", 
"09.04.2019 02:50", "09.04.2019 02:55", "09.04.2019 03:00", "09.04.2019 03:05", 
"09.04.2019 03:10", "09.04.2019 03:15", "09.04.2019 03:20", "09.04.2019 03:25", 
"09.04.2019 03:30", "09.04.2019 03:35", "09.04.2019 03:40", "09.04.2019 03:45", 
"09.04.2019 03:50", "09.04.2019 03:55", "09.04.2019 04:00", "09.04.2019 04:05", 
"09.04.2019 04:10", "09.04.2019 04:15", "09.04.2019 04:20", "09.04.2019 04:25", 
"09.04.2019 04:30", "09.04.2019 04:35", "09.04.2019 04:40", "09.04.2019 04:45", 
"09.04.2019 04:50", "09.04.2019 04:55", "09.04.2019 05:00", "09.04.2019 05:05", 
"09.04.2019 05:10", "09.04.2019 05:15", "09.04.2019 05:20", "09.04.2019 05:25", 
"09.04.2019 05:30", "09.04.2019 05:35", "09.04.2019 05:40", "09.04.2019 05:45", 
"09.04.2019 05:50", "09.04.2019 05:55", "09.04.2019 06:00", "09.04.2019 06:05", 
"09.04.2019 06:10", "09.04.2019 06:15", "09.04.2019 06:20", "09.04.2019 06:25", 
"09.04.2019 06:30", "09.04.2019 06:35", "09.04.2019 06:40", "09.04.2019 06:45", 
"09.04.2019 06:50", "09.04.2019 06:55", "09.04.2019 07:00", "09.04.2019 07:05", 
"09.04.2019 07:10", "09.04.2019 07:15", "09.04.2019 07:20", "09.04.2019 07:25", 
"09.04.2019 07:30", "09.04.2019 07:35", "09.04.2019 07:40", "09.04.2019 07:45", 
"09.04.2019 07:50", "09.04.2019 07:55", "09.04.2019 08:00", "09.04.2019 08:05", 
"09.04.2019 08:10", "09.04.2019 08:15", "09.04.2019 08:20", "09.04.2019 08:25", 
"09.04.2019 08:30", "09.04.2019 08:35", "09.04.2019 08:40", "09.04.2019 08:45", 
"09.04.2019 08:50", "09.04.2019 08:55", "09.04.2019 09:00", "09.04.2019 09:05", 
"09.04.2019 09:10", "09.04.2019 09:15", "09.04.2019 09:20", "09.04.2019 09:25", 
"09.04.2019 09:30", "09.04.2019 09:35", "09.04.2019 09:40", "09.04.2019 09:45", 
"09.04.2019 09:50", "09.04.2019 09:55", "09.04.2019 10:00", "09.04.2019 10:05"
), class = "factor"), L1 = c(0.654, 0.656, 0.653, 0.652, 0.657, 
0.649, 0.665, 0.647, 0.66, 0.666)), row.names = c(NA, 10L), class = "data.frame")
> 

2 个答案:

答案 0 :(得分:0)

我不确定以下是OP想要的,但是它对应于问题描述。

首先,将列Date强制为类"POSIXct",以在以后保存一些步骤。
然后,每天计算max(L1),重新组合数据并应用给定的公式。

library(dplyr)

L1 <- L1 %>% mutate(Date = as.POSIXct(Date, format = "%d.%m.%Y %H:%M"))

max24h_L1 <- L1 %>%
  mutate(Date  = cut(Date, breaks = "day")) %>%
  group_by(Date) %>% 
  summarize(maxL1 = max(L1, na.rm = TRUE)) %>%
  mutate(Date = as.Date(as.character(Date))) %>%
  inner_join(L1 %>% mutate(Date = as.Date(Date))) %>%
  mutate(NewL1 = 0.714 * (maxL1/L1 - 1)^1.231)

max24h_L1
#Joining, by = "Date"
## A tibble: 10 x 4
#   Date       maxL1    L1    NewL1
#   <date>     <dbl> <dbl>    <dbl>
# 1 2019-04-08 0.666 0.654 0.00520 
# 2 2019-04-08 0.666 0.656 0.00414 
# 3 2019-04-08 0.666 0.653 0.00575 
# 4 2019-04-08 0.666 0.652 0.00631 
# 5 2019-04-08 0.666 0.657 0.00363 
# 6 2019-04-08 0.666 0.649 0.00806 
# 7 2019-04-08 0.666 0.665 0.000239
# 8 2019-04-08 0.666 0.647 0.00928 
# 9 2019-04-08 0.666 0.66  0.00219 
#10 2019-04-08 0.666 0.666 0 

答案 1 :(得分:0)

作为Rui回答的一种变体,我们可以通过按Date = as.Date(Date)分组来组合一些步骤(这将保留基础Date列的POSIXct datetime格式,但仅按日期分组),并使用{{1} },而不是mutate。这样会将当天的最大L1值带入每一行,从而节省了我们重新连接到原始数据的步骤。

summarize