根据列中的条件向R中的数据框添加行

时间:2019-07-10 18:31:04

标签: r dataframe dplyr

我有一个数据帧,其中包含22个具有时间序列数据的唯一站点标识符的列。我想绘制一个时间序列条形图。我可以做的很好。但是,某些站点在某些月份没有数据。因此,有些图看起来不正确,因为它将仅显示两个月的数据而不是全部12个数据。我的问题是,如何为丢失的测站添加丢失的月份和零值。我想以一种实用的方式进行操作,因此无需查看每个工作站,也无需手动查看缺少的内容。

样本数据:

  structure(list(month = c("01", "01", "01", "01", "01", "01", 
"01", "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", 
"01", "01", "01", "01", "01", "01", "01", "02", "02", "02", "02", 
"02", "02", "02", "02", "02", "02", "02", "02", "02", "02", "02", 
"02", "02", "02", "02", "02", "02", "03", "03", "03", "03", "03", 
"03", "03", "03", "03", "03", "03", "03", "03", "03", "03", "03", 
"03", "04", "04", "04", "04", "04", "04", "05", "05", "06", "06", 
"06", "07", "07", "07", "07", "08", "08", "08", "08", "08", "08", 
"08", "08", "08", "09", "09", "09", "09", "09", "09", "09", "09", 
"10", "10", "10", "10", "10", "10", "11", "11", "11", "11", "12", 
"12", "12", "12", "12", "12", "12", "12", "12", "12", "12", "12", 
"12", "12", "12"), Station = c("01377358", "01378560", "01379200", 
"01379525", "01463740", "01463750", "01463810", "1390600", "1390700", 
"1390800", "1399565", "1399820", "1400775", "1401400", "1403075", 
"1408100", "AN0113", "BFBM000180", "BFBM000244", "BFBM000245", 
"BFBM000246", "BFBM000247", "BFBM000248", "FIBI041", "01377358", 
"01378560", "01379525", "01463740", "01463750", "01463810", "1390600", 
"1390700", "1390800", "1399565", "1399820", "1400775", "1401400", 
"1403075", "AN0113", "BFBM000180", "BFBM000245", "BFBM000246", 
"BFBM000247", "BFBM000248", "FIBI041", "01377358", "01378560", 
"01379525", "01463740", "01463750", "01463810", "1390600", "1390700", 
"1390800", "1401400", "1403075", "AN0113", "BFBM000245", "BFBM000246", 
"BFBM000247", "BFBM000248", "FIBI041", "01377358", "01378560", 
"01379525", "1390800", "AN0113", "BFBM000245", "01378560", "01379525", 
"01378560", "01379525", "01463740", "01377358", "01378560", "BFBM000244", 
"BFBM000245", "01377358", "01378560", "01379200", "01379525", 
"AN0113", "BFBM000244", "BFBM000245", "BFBM000246", "BFBM000247", 
"01377358", "01378560", "01379200", "01379525", "AN0113", "BFBM000244", 
"BFBM000245", "BFBM000246", "01377358", "01378560", "01379200", 
"01379525", "BFBM000244", "BFBM000245", "01378560", "01379525", 
"BFBM000244", "BFBM000245", "01377358", "01378560", "01379200", 
"01379525", "01463740", "01463750", "01463810", "1399820", "1403075", 
"AN0113", "BFBM000244", "BFBM000245", "BFBM000246", "BFBM000247", 
"FIBI041"), Huc14 = c("02030103170010", "02030103180010", "02030103010070", 
"02030103010140", "02040105240010", "02040105240010", "02040105240010", 
"2030103140010", "HUC02030100000000", "2030103140020", "HUC2030105050080", 
"2030105070010", "2030105100130", "2030105110010", "2030105120050", 
"HUC02040301030010", "02040105240010", "HUC02030100000000", "02040105240010", 
"02040105240010", "02040105240010", "02040105240010", "02040105240010", 
"02040105240010", "02030103170010", "02030103180010", "02030103010140", 
"02040105240010", "02040105240010", "02040105240010", "2030103140010", 
"HUC02030100000000", "2030103140020", "HUC2030105050080", "2030105070010", 
"2030105100130", "2030105110010", "2030105120050", "02040105240010", 
"HUC02030100000000", "02040105240010", "02040105240010", "02040105240010", 
"02040105240010", "02040105240010", "02030103170010", "02030103180010", 
"02030103010140", "02040105240010", "02040105240010", "02040105240010", 
"2030103140010", "HUC02030100000000", "2030103140020", "2030105110010", 
"2030105120050", "02040105240010", "02040105240010", "02040105240010", 
"02040105240010", "02040105240010", "02040105240010", "02030103170010", 
"02030103180010", "02030103010140", "2030103140020", "02040105240010", 
"02040105240010", "02030103180010", "02030103010140", "02030103180010", 
"02030103010140", "02040105240010", "02030103170010", "02030103180010", 
"02040105240010", "02040105240010", "02030103170010", "02030103180010", 
"02030103010070", "02030103010140", "02040105240010", "02040105240010", 
"02040105240010", "02040105240010", "02040105240010", "02030103170010", 
"02030103180010", "02030103010070", "02030103010140", "02040105240010", 
"02040105240010", "02040105240010", "02040105240010", "02030103170010", 
"02030103180010", "02030103010070", "02030103010140", "02040105240010", 
"02040105240010", "02030103180010", "02030103010140", "02040105240010", 
"02040105240010", "02030103170010", "02030103180010", "02030103010070", 
"02030103010140", "02040105240010", "02040105240010", "02040105240010", 
"2030105070010", "2030105120050", "02040105240010", "02040105240010", 
"02040105240010", "02040105240010", "02040105240010", "02040105240010"
), `Total Number of Exceedance` = c(1028, 1362, 870, 1294, 595, 
589, 484, 1419, 1417, 1416, 564, 535, 46, 411, 1463, 25, 827, 
1343, 476, 1455, 718, 603, 2, 295, 1344, 1298, 1343, 349, 567, 
500, 1344, 1344, 1344, 428, 685, 5, 661, 1344, 940, 886, 1173, 
272, 802, 304, 201, 1488, 1416, 1437, 504, 698, 568, 1322, 842, 
856, 86, 901, 1324, 791, 549, 794, 447, 602, 209, 1164, 625, 
68, 384, 483, 980, 68, 970, 189, 1, 307, 7, 114, 112, 1069, 52, 
253, 62, 4, 1472, 1478, 2, 93, 890, 488, 1039, 48, 19, 1194, 
1389, 12, 7, 618, 203, 578, 1483, 1466, 777, 644, 1439, 1396, 
495, 1244, 462, 1008, 125, 5, 86, 26, 17, 74, 1403, 1451, 89, 
263, 1), timehrs = c(514, 681, 435, 647, 297.5, 294.5, 242, 709.5, 
708.5, 708, 282, 267.5, 23, 205.5, 731.5, 12.5, 413.5, 671.5, 
238, 727.5, 359, 301.5, 1, 147.5, 672, 649, 671.5, 174.5, 283.5, 
250, 672, 672, 672, 214, 342.5, 2.5, 330.5, 672, 470, 443, 586.5, 
136, 401, 152, 100.5, 744, 708, 718.5, 252, 349, 284, 661, 421, 
428, 43, 450.5, 662, 395.5, 274.5, 397, 223.5, 301, 104.5, 582, 
312.5, 34, 192, 241.5, 490, 34, 485, 94.5, 0.5, 153.5, 3.5, 57, 
56, 534.5, 26, 126.5, 31, 2, 736, 739, 1, 46.5, 445, 244, 519.5, 
24, 9.5, 597, 694.5, 6, 3.5, 309, 101.5, 289, 741.5, 733, 388.5, 
322, 719.5, 698, 247.5, 622, 231, 504, 62.5, 2.5, 43, 13, 8.5, 
37, 701.5, 725.5, 44.5, 131.5, 0.5)), .Names = c("month", "Station", 
"Huc14", "Total Number of Exceedance", "timehrs"), row.names = c(NA, 
-119L), class = c("tbl_df", "tbl", "data.frame"))

我正在谈论的示例情节:

enter image description here

0 个答案:

没有答案