我有一个数据集,其中包含一年中每一天的每小时空气质量数据。我正在尝试扩大范围,但当 NULLCODE 列中存在空代码时遇到问题。这些特定日期会更广泛地分为同一天的多个条目(例如,下面的一小部分数据分为 2016 年 2 月 2 日的 3 个条目),但我每天只需要一个条目。
我想更广泛地旋转,以便每个小时都有一个包含 PM25 值的列和一个包含 NULLCODE 值的列,以防止每天分成多行,但我不确定如何做到这一点。
复制我的数据:
library(tidyr)
data <- data.frame(DATE = c("2019-02-09"),
TIME = c("00:00", "01:00", "02:00","03:00", "04:00","05:00","06:00","07:00","08:00","09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00"),
PM25 = c("2","1","0","-1","-4","NA","NA","NA","-4","1","6","28","23","3","3","5","NA","9","24","24","17","16","9","15"),
NULLCODE = c("","","","","","DA","DA","DA","","","","","","","","","AM","","","","","","",""))
我的原始数据框是什么样的
> print(data)
DATE TIME PM25 NULLCODE
1 2019-02-09 00:00 2
2 2019-02-09 01:00 1
3 2019-02-09 02:00 0
4 2019-02-09 03:00 -1
5 2019-02-09 04:00 -4
6 2019-02-09 05:00 NA DA
7 2019-02-09 06:00 NA DA
8 2019-02-09 07:00 NA DA
9 2019-02-09 08:00 -4
10 2019-02-09 09:00 1
11 2019-02-09 10:00 6
12 2019-02-09 11:00 28
13 2019-02-09 12:00 23
14 2019-02-09 13:00 3
15 2019-02-09 14:00 3
16 2019-02-09 15:00 5
17 2019-02-09 16:00 NA AM
18 2019-02-09 17:00 9
19 2019-02-09 18:00 24
20 2019-02-09 19:00 24
21 2019-02-09 20:00 17
22 2019-02-09 21:00 16
23 2019-02-09 22:00 9
24 2019-02-09 23:00 1
这是我用来旋转更宽的代码,为 2016-02-09 创建三行:
data2 <- data %>% pivot_wider(
names_from = TIME,
names_prefix = "Time_",
values_from = PM25
)
旋转后我的数据框是什么样子
> print(data2)
DATE NULLCODE Time_00:00 Time_01:00 Time_02:00 Time_03:00 Time_04:00
1 2019-02-09 2 1 0 -1 -4
2 2019-02-09 DA <NA> <NA> <NA> <NA> <NA>
3 2019-02-09 AM <NA> <NA> <NA> <NA> <NA>
Time_05:00 Time_06:00 Time_07:00 Time_08:00 Time_09:00 Time_10:00 Time_11:00
1 <NA> <NA> <NA> -4 1 6 28
2 NA NA NA <NA> <NA> <NA> <NA>
3 <NA> <NA> <NA> <NA> <NA> <NA> <NA>
Time_12:00 Time_13:00 Time_14:00 Time_15:00 Time_16:00 Time_17:00 Time_18:00
1 23 3 3 5 <NA> 9 24
2 <NA> <NA> <NA> <NA> <NA> <NA> <NA>
3 <NA> <NA> <NA> <NA> NA <NA> <NA>
Time_19:00 Time_20:00 Time_21:00 Time_22:00 Time_23:00
1 24 17 16 9 15
2 <NA> <NA> <NA> <NA> <NA>
3 <NA> <NA> <NA> <NA> <NA>
能够通过以下方式获得正确的旋转:
data2 <- data %>% pivot_wider(
names_from = TIME,
names_prefix = "Time_",
values_from = c(PM25, NULLCODE)
)