R 中跨多个日期(POSIXt 格式)按特定时间删除行
Remove Rows By Specific Hours Across Multiple Dates (POSIXt format) in R
我有一个数据框,需要删除包含小时 0:00、1:00、2:00、3:00、4:00、[= 的行24=], 6:00 对于所有日期,每天只留下 17 个数字而不是 24 个。然后我需要根据这 17 个值计算每天的最大值,但我坚持要摆脱 7我不需要的时间。这是我已经尝试过的
library(dplyr)
library (lubridate)
df <- maximums %>% filter(!grepl("0:00", maximums$date))
关于如何摆脱 7 小时的任何想法?
> class(maximums$date)
[1] "POSIXct" "POSIXt"
> class(maximums$X8.hour.running.ozone)
[1] "numeric"
date X8.hour.running.ozone
1 2015-01-01 07:00:00 0.028938958
2 2015-01-01 08:00:00 0.028889583
3 2015-01-01 09:00:00 0.029101042
4 2015-01-01 10:00:00 0.029714583
5 2015-01-01 11:00:00 0.030435000
6 2015-01-01 12:00:00 0.031142500
7 2015-01-01 13:00:00 0.032040208
8 2015-01-01 14:00:00 0.032310625
9 2015-01-01 15:00:00 0.031541875
10 2015-01-01 16:00:00 0.030727708
11 2015-01-01 17:00:00 0.029294583
12 2015-01-01 18:00:00 0.029521497
13 2015-01-01 19:00:00 0.029260455
14 2015-01-01 20:00:00 0.028565455
15 2015-01-01 21:00:00 0.028091705
16 2015-01-01 22:00:00 0.027990038
17 2015-01-01 23:00:00 0.028562330
18 2015-01-02 00:00:00 0.029188788
19 2015-01-02 01:00:00 0.030586705
20 2015-01-02 02:00:00 0.030149167
21 2015-01-02 03:00:00 0.030019583
22 2015-01-02 04:00:00 0.030131667
23 2015-01-02 05:00:00 0.029914583
24 2015-01-02 06:00:00 0.029825208
25 2015-01-02 07:00:00 0.030125833
26 2015-01-02 08:00:00 0.030182083
27 2015-01-02 09:00:00 0.029968333
28 2015-01-02 10:00:00 0.029895833
29 2015-01-02 11:00:00 0.030017500
30 2015-01-02 12:00:00 0.030468125
31 2015-01-02 13:00:00 0.030865000
32 2015-01-02 14:00:00 0.030941458
33 2015-01-02 15:00:00 0.030991875
34 2015-01-02 16:00:00 0.031401875
35 2015-01-02 17:00:00 0.032022997
36 2015-01-02 18:00:00 0.032019372
37 2015-01-02 19:00:00 0.032063122
38 2015-01-02 20:00:00 0.031818747
39 2015-01-02 21:00:00 0.031460413
40 2015-01-02 22:00:00 0.031113955
41 2015-01-02 23:00:00 0.029953955
42 2015-01-03 00:00:00 0.028782497
43 2015-01-03 01:00:00 0.027288667
44 2015-01-03 02:00:00 0.026244375
45 2015-01-03 03:00:00 0.025112917
46 2015-01-03 04:00:00 0.024015417
47 2015-01-03 05:00:00 0.022908542
48 2015-01-03 06:00:00 0.022458958
49 2015-01-03 07:00:00 0.022889583
50 2015-01-03 08:00:00 0.022612500
51 2015-01-03 09:00:00 0.023327917
理想情况下我想 return:
date X8.hour.running.ozone
1 2015-01-01 07:00:00 0.028938958
2 2015-01-01 08:00:00 0.028889583
3 2015-01-01 09:00:00 0.029101042
4 2015-01-01 10:00:00 0.029714583
5 2015-01-01 11:00:00 0.030435000
6 2015-01-01 12:00:00 0.031142500
7 2015-01-01 13:00:00 0.032040208
8 2015-01-01 14:00:00 0.032310625
9 2015-01-01 15:00:00 0.031541875
10 2015-01-01 16:00:00 0.030727708
11 2015-01-01 17:00:00 0.029294583
12 2015-01-01 18:00:00 0.029521497
13 2015-01-01 19:00:00 0.029260455
14 2015-01-01 20:00:00 0.028565455
15 2015-01-01 21:00:00 0.028091705
16 2015-01-01 22:00:00 0.027990038
17 2015-01-01 23:00:00 0.028562330
18 2015-01-02 07:00:00 0.030125833
19 2015-01-02 08:00:00 0.030182083
20 2015-01-02 09:00:00 0.029968333
21 2015-01-02 10:00:00 0.029895833
22 2015-01-02 11:00:00 0.030017500
23 2015-01-02 12:00:00 0.030468125
24 2015-01-02 13:00:00 0.030865000
25 2015-01-02 14:00:00 0.030941458
26 2015-01-02 15:00:00 0.030991875
27 2015-01-02 16:00:00 0.031401875
28 2015-01-02 17:00:00 0.032022997
29 2015-01-02 18:00:00 0.032019372
30 2015-01-02 19:00:00 0.032063122
31 2015-01-02 20:00:00 0.031818747
32 2015-01-02 21:00:00 0.031460413
33 2015-01-02 22:00:00 0.031113955
34 2015-01-02 23:00:00 0.029953955
35 2015-01-03 07:00:00 0.022889583
36 2015-01-03 08:00:00 0.022612500
37 2015-01-03 09:00:00 0.023327917
如果您只想删除这些时间,您可以这样做:
library(lubridate)
library(dplyr)
library(readr)
df %>%
type_convert() %>%
as_tibble() %>%
filter(hour(date) > 6)
Returns:
# A tibble: 37 x 2
date running.ozone
<dttm> <dbl>
1 2015-01-01 07:00:00 0.0289
2 2015-01-01 08:00:00 0.0289
3 2015-01-01 09:00:00 0.0291
4 2015-01-01 10:00:00 0.0297
5 2015-01-01 11:00:00 0.0304
6 2015-01-01 12:00:00 0.0311
7 2015-01-01 13:00:00 0.0320
8 2015-01-01 14:00:00 0.0323
9 2015-01-01 15:00:00 0.0315
10 2015-01-01 16:00:00 0.0307
# ... with 27 more rows
数据:
df <- structure(list(date = c(
"2015-01-01 07:00:00", "2015-01-01 08:00:00",
"2015-01-01 09:00:00", "2015-01-01 10:00:00", "2015-01-01 11:00:00",
"2015-01-01 12:00:00", "2015-01-01 13:00:00", "2015-01-01 14:00:00",
"2015-01-01 15:00:00", "2015-01-01 16:00:00", "2015-01-01 17:00:00",
"2015-01-01 18:00:00", "2015-01-01 19:00:00", "2015-01-01 20:00:00",
"2015-01-01 21:00:00", "2015-01-01 22:00:00", "2015-01-01 23:00:00",
"2015-01-02 00:00:00", "2015-01-02 01:00:00", "2015-01-02 02:00:00",
"2015-01-02 03:00:00", "2015-01-02 04:00:00", "2015-01-02 05:00:00",
"2015-01-02 06:00:00", "2015-01-02 07:00:00", "2015-01-02 08:00:00",
"2015-01-02 09:00:00", "2015-01-02 10:00:00", "2015-01-02 11:00:00",
"2015-01-02 12:00:00", "2015-01-02 13:00:00", "2015-01-02 14:00:00",
"2015-01-02 15:00:00", "2015-01-02 16:00:00", "2015-01-02 17:00:00",
"2015-01-02 18:00:00", "2015-01-02 19:00:00", "2015-01-02 20:00:00",
"2015-01-02 21:00:00", "2015-01-02 22:00:00", "2015-01-02 23:00:00",
"2015-01-03 00:00:00", "2015-01-03 01:00:00", "2015-01-03 02:00:00",
"2015-01-03 03:00:00", "2015-01-03 04:00:00", "2015-01-03 05:00:00",
"2015-01-03 06:00:00", "2015-01-03 07:00:00", "2015-01-03 08:00:00",
"2015-01-03 09:00:00"
), running.ozone = c(
0.028938958, 0.028889583,
0.029101042, 0.029714583, 0.030435, 0.0311425, 0.032040208, 0.032310625,
0.031541875, 0.030727708, 0.029294583, 0.029521497, 0.029260455,
0.028565455, 0.028091705, 0.027990038, 0.02856233, 0.029188788,
0.030586705, 0.030149167, 0.030019583, 0.030131667, 0.029914583,
0.029825208, 0.030125833, 0.030182083, 0.029968333, 0.029895833,
0.0300175, 0.030468125, 0.030865, 0.030941458, 0.030991875, 0.031401875,
0.032022997, 0.032019372, 0.032063122, 0.031818747, 0.031460413,
0.031113955, 0.029953955, 0.028782497, 0.027288667, 0.026244375,
0.025112917, 0.024015417, 0.022908542, 0.022458958, 0.022889583,
0.0226125, 0.023327917
)), class = "data.frame", row.names = c(
NA,
-51L
), .Names = c("date", "running.ozone"))
我有一个数据框,需要删除包含小时 0:00、1:00、2:00、3:00、4:00、[= 的行24=], 6:00 对于所有日期,每天只留下 17 个数字而不是 24 个。然后我需要根据这 17 个值计算每天的最大值,但我坚持要摆脱 7我不需要的时间。这是我已经尝试过的
library(dplyr)
library (lubridate)
df <- maximums %>% filter(!grepl("0:00", maximums$date))
关于如何摆脱 7 小时的任何想法?
> class(maximums$date)
[1] "POSIXct" "POSIXt"
> class(maximums$X8.hour.running.ozone)
[1] "numeric"
date X8.hour.running.ozone
1 2015-01-01 07:00:00 0.028938958
2 2015-01-01 08:00:00 0.028889583
3 2015-01-01 09:00:00 0.029101042
4 2015-01-01 10:00:00 0.029714583
5 2015-01-01 11:00:00 0.030435000
6 2015-01-01 12:00:00 0.031142500
7 2015-01-01 13:00:00 0.032040208
8 2015-01-01 14:00:00 0.032310625
9 2015-01-01 15:00:00 0.031541875
10 2015-01-01 16:00:00 0.030727708
11 2015-01-01 17:00:00 0.029294583
12 2015-01-01 18:00:00 0.029521497
13 2015-01-01 19:00:00 0.029260455
14 2015-01-01 20:00:00 0.028565455
15 2015-01-01 21:00:00 0.028091705
16 2015-01-01 22:00:00 0.027990038
17 2015-01-01 23:00:00 0.028562330
18 2015-01-02 00:00:00 0.029188788
19 2015-01-02 01:00:00 0.030586705
20 2015-01-02 02:00:00 0.030149167
21 2015-01-02 03:00:00 0.030019583
22 2015-01-02 04:00:00 0.030131667
23 2015-01-02 05:00:00 0.029914583
24 2015-01-02 06:00:00 0.029825208
25 2015-01-02 07:00:00 0.030125833
26 2015-01-02 08:00:00 0.030182083
27 2015-01-02 09:00:00 0.029968333
28 2015-01-02 10:00:00 0.029895833
29 2015-01-02 11:00:00 0.030017500
30 2015-01-02 12:00:00 0.030468125
31 2015-01-02 13:00:00 0.030865000
32 2015-01-02 14:00:00 0.030941458
33 2015-01-02 15:00:00 0.030991875
34 2015-01-02 16:00:00 0.031401875
35 2015-01-02 17:00:00 0.032022997
36 2015-01-02 18:00:00 0.032019372
37 2015-01-02 19:00:00 0.032063122
38 2015-01-02 20:00:00 0.031818747
39 2015-01-02 21:00:00 0.031460413
40 2015-01-02 22:00:00 0.031113955
41 2015-01-02 23:00:00 0.029953955
42 2015-01-03 00:00:00 0.028782497
43 2015-01-03 01:00:00 0.027288667
44 2015-01-03 02:00:00 0.026244375
45 2015-01-03 03:00:00 0.025112917
46 2015-01-03 04:00:00 0.024015417
47 2015-01-03 05:00:00 0.022908542
48 2015-01-03 06:00:00 0.022458958
49 2015-01-03 07:00:00 0.022889583
50 2015-01-03 08:00:00 0.022612500
51 2015-01-03 09:00:00 0.023327917
理想情况下我想 return:
date X8.hour.running.ozone
1 2015-01-01 07:00:00 0.028938958
2 2015-01-01 08:00:00 0.028889583
3 2015-01-01 09:00:00 0.029101042
4 2015-01-01 10:00:00 0.029714583
5 2015-01-01 11:00:00 0.030435000
6 2015-01-01 12:00:00 0.031142500
7 2015-01-01 13:00:00 0.032040208
8 2015-01-01 14:00:00 0.032310625
9 2015-01-01 15:00:00 0.031541875
10 2015-01-01 16:00:00 0.030727708
11 2015-01-01 17:00:00 0.029294583
12 2015-01-01 18:00:00 0.029521497
13 2015-01-01 19:00:00 0.029260455
14 2015-01-01 20:00:00 0.028565455
15 2015-01-01 21:00:00 0.028091705
16 2015-01-01 22:00:00 0.027990038
17 2015-01-01 23:00:00 0.028562330
18 2015-01-02 07:00:00 0.030125833
19 2015-01-02 08:00:00 0.030182083
20 2015-01-02 09:00:00 0.029968333
21 2015-01-02 10:00:00 0.029895833
22 2015-01-02 11:00:00 0.030017500
23 2015-01-02 12:00:00 0.030468125
24 2015-01-02 13:00:00 0.030865000
25 2015-01-02 14:00:00 0.030941458
26 2015-01-02 15:00:00 0.030991875
27 2015-01-02 16:00:00 0.031401875
28 2015-01-02 17:00:00 0.032022997
29 2015-01-02 18:00:00 0.032019372
30 2015-01-02 19:00:00 0.032063122
31 2015-01-02 20:00:00 0.031818747
32 2015-01-02 21:00:00 0.031460413
33 2015-01-02 22:00:00 0.031113955
34 2015-01-02 23:00:00 0.029953955
35 2015-01-03 07:00:00 0.022889583
36 2015-01-03 08:00:00 0.022612500
37 2015-01-03 09:00:00 0.023327917
如果您只想删除这些时间,您可以这样做:
library(lubridate)
library(dplyr)
library(readr)
df %>%
type_convert() %>%
as_tibble() %>%
filter(hour(date) > 6)
Returns:
# A tibble: 37 x 2
date running.ozone
<dttm> <dbl>
1 2015-01-01 07:00:00 0.0289
2 2015-01-01 08:00:00 0.0289
3 2015-01-01 09:00:00 0.0291
4 2015-01-01 10:00:00 0.0297
5 2015-01-01 11:00:00 0.0304
6 2015-01-01 12:00:00 0.0311
7 2015-01-01 13:00:00 0.0320
8 2015-01-01 14:00:00 0.0323
9 2015-01-01 15:00:00 0.0315
10 2015-01-01 16:00:00 0.0307
# ... with 27 more rows
数据:
df <- structure(list(date = c(
"2015-01-01 07:00:00", "2015-01-01 08:00:00",
"2015-01-01 09:00:00", "2015-01-01 10:00:00", "2015-01-01 11:00:00",
"2015-01-01 12:00:00", "2015-01-01 13:00:00", "2015-01-01 14:00:00",
"2015-01-01 15:00:00", "2015-01-01 16:00:00", "2015-01-01 17:00:00",
"2015-01-01 18:00:00", "2015-01-01 19:00:00", "2015-01-01 20:00:00",
"2015-01-01 21:00:00", "2015-01-01 22:00:00", "2015-01-01 23:00:00",
"2015-01-02 00:00:00", "2015-01-02 01:00:00", "2015-01-02 02:00:00",
"2015-01-02 03:00:00", "2015-01-02 04:00:00", "2015-01-02 05:00:00",
"2015-01-02 06:00:00", "2015-01-02 07:00:00", "2015-01-02 08:00:00",
"2015-01-02 09:00:00", "2015-01-02 10:00:00", "2015-01-02 11:00:00",
"2015-01-02 12:00:00", "2015-01-02 13:00:00", "2015-01-02 14:00:00",
"2015-01-02 15:00:00", "2015-01-02 16:00:00", "2015-01-02 17:00:00",
"2015-01-02 18:00:00", "2015-01-02 19:00:00", "2015-01-02 20:00:00",
"2015-01-02 21:00:00", "2015-01-02 22:00:00", "2015-01-02 23:00:00",
"2015-01-03 00:00:00", "2015-01-03 01:00:00", "2015-01-03 02:00:00",
"2015-01-03 03:00:00", "2015-01-03 04:00:00", "2015-01-03 05:00:00",
"2015-01-03 06:00:00", "2015-01-03 07:00:00", "2015-01-03 08:00:00",
"2015-01-03 09:00:00"
), running.ozone = c(
0.028938958, 0.028889583,
0.029101042, 0.029714583, 0.030435, 0.0311425, 0.032040208, 0.032310625,
0.031541875, 0.030727708, 0.029294583, 0.029521497, 0.029260455,
0.028565455, 0.028091705, 0.027990038, 0.02856233, 0.029188788,
0.030586705, 0.030149167, 0.030019583, 0.030131667, 0.029914583,
0.029825208, 0.030125833, 0.030182083, 0.029968333, 0.029895833,
0.0300175, 0.030468125, 0.030865, 0.030941458, 0.030991875, 0.031401875,
0.032022997, 0.032019372, 0.032063122, 0.031818747, 0.031460413,
0.031113955, 0.029953955, 0.028782497, 0.027288667, 0.026244375,
0.025112917, 0.024015417, 0.022908542, 0.022458958, 0.022889583,
0.0226125, 0.023327917
)), class = "data.frame", row.names = c(
NA,
-51L
), .Names = c("date", "running.ozone"))