按组

时间:2017-12-21 20:24:48

标签: r dplyr

我有几十年的每日时间序列的降水数据,包括年,月和季的列(1 = 1月 - 3月,2 = 4月 - 6月等)。我在每个季度为每日降水量设定了不同的最低阈值,并计算了每个月超过季度阈值的天数百分比。我使用dplyr计算了这个百分比高于阈值。现在,我想计算每个月左右百分比的标准差(百分比),但这样做的简明方法就是我。这是我到目前为止所做的事情。

数据:

weath_k <- structure(list(Year = c(2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L), Qu = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), 
    Day = c("01", "02", "03", "04", "05", "06", "07", "08", "09", 
    "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 
    "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", 
    "30", "31", "01", "02", "03", "04", "05", "06", "07", "08", 
    "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", 
    "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", 
    "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", 
    "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", 
    "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", 
    "31", "01", "02", "03", "04", "05", "06", "07", "08", "09", 
    "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 
    "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", 
    "30", "01", "02", "03", "04", "05", "06", "07", "08", "09", 
    "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 
    "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", 
    "30", "31", "01", "02", "03", "04", "05", "06", "07", "08", 
    "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", 
    "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", 
    "29", "30", "01", "02", "03", "04", "05", "06", "07", "08", 
    "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", 
    "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", 
    "29", "30", "31", "01", "02", "03", "04", "05", "06", "07", 
    "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", 
    "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", 
    "28", "29", "30", "31", "01", "02", "03", "04", "05", "06", 
    "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", 
    "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", 
    "27", "28", "29", "30", "01", "02", "03", "04", "05", "06", 
    "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", 
    "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", 
    "27", "28", "29", "30", "31", "01", "02", "03", "04", "05", 
    "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", 
    "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", 
    "26", "27", "28", "29", "30", "01", "02", "03", "04", "05", 
    "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", 
    "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", 
    "26", "27", "28", "29", "30", "31", "01", "02", "03", "04", 
    "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", 
    "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
    "25", "26", "27", "28", "29", "30", "31", "01", "02", "03", 
    "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", 
    "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", 
    "24", "25", "26", "27", "28", "01", "02", "03", "04", "05", 
    "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", 
    "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", 
    "26", "27", "28", "29", "30", "31", "01", "02", "03", "04", 
    "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", 
    "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
    "25", "26", "27", "28", "29", "30", "01", "02", "03", "04", 
    "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", 
    "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
    "25", "26", "27", "28", "29", "30", "31", "01", "02", "03", 
    "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", 
    "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", 
    "24", "25", "26", "27", "28", "29", "30", "01", "02", "03", 
    "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", 
    "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", 
    "24", "25", "26", "27", "28", "29", "30", "31", "01", "02", 
    "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", 
    "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", 
    "23", "24", "25", "26", "27", "28", "29", "30", "31", "01", 
    "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", 
    "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", 
    "22", "23", "24", "25", "26", "27", "28", "29", "30", "01", 
    "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", 
    "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", 
    "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", 
    "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", 
    "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", 
    "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", 
    "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", 
    "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", 
    "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", 
    "31"), PRCP = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    4, 0, 0, 4, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 
    0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 34, 18, 0, 0, 0, 
    0, 0, 0, 7, 1, 16, 8, 0, 0, 0, 13, 0, 0, 3, 11, 0, 0, 0, 
    0, 1, 5, 4, 0, 0, 0, 0, 0, 0, 0, 0, 6, 2, 21, 0, 1, 0, 0, 
    0, 3, 46, 4, 38, 30, 0, 0, 0, 4, 16, 36, 2, 3, 0, 0, 7, 10, 
    5, 0, 0, 3, 1, 20, 1, 0, 5, 7, 18, 19, 18, 0, 0, 12, 0, 5, 
    0, 0, 3, 0, 0, 0, 6, 4, 4, 1, 12, 2, 20, 7, 1, 6, 5, 23, 
    8, 20, 53, 10, 95, 117, 1, 1, 5, 0, 1, 0, 13, 32, 15, 0, 
    0, 0, 0, 0, 3, 3, 11, 29, 5, 25, 2, 28, 6, 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, 8, 1, 13, 14, 3, 1, 3, 
    15, 9, 1, 3, 1, 7, 3, 1, 2, 13, 8, 4, 0, 8, 0, 1, 20, 2, 
    0, 1, 1, 0, 2, 0, 2, 6, 12, 39, 1, 2, 1, 11, 0, 0, 0, 14, 
    0, 32, 1, 6, 18, 4, 2, 0, 0, 1, 4, 12, 29, 9, 24, 16, 4, 
    1, 2, 18, 0, 0, 0, 80, 3, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 
    6, 2, 51, 15, 3, 5, 3, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 24, 
    78, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 10, 16, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("Year", 
"Qu", "Mo", "Day", "PRCP"), class = "data.frame", row.names = 10586:11315)

代码:

library(dplyr)

q1_t <- 2.5
q2_t <- 10
q3_t <- 20
q4_t <- 15

perc_test <- weath_k %>%
                filter(!is.na(PRCP)) %>%
                group_by(Qu, Mo) %>%
                summarize(n_days = n(),
                          n_q1t = sum(PRCP > q1_t),
                          p_q1t = round((n_q1t / n_days)*100, 2),
                          n_q2t = sum(PRCP > q2_t),
                          p_q2t = round((n_q2t / n_days)*100, 2),
                          n_q3t = sum(PRCP > q3_t),
                          p_q3t = round((n_q3t / n_days)*100, 2),
                          n_q4t = sum(PRCP > q4_t),
                          p_q4t = round((n_q4t / n_days)*100, 2))
perc_test$p_qs <- c(perc_test$p_q1t[1:3], perc_test$p_q2t[4:6], 
                    perc_test$p_q3t[7:9], perc_test$p_q4t[10:12])

专栏&#34; p_qs&#34;是感兴趣的列,因为它包含每个季度阈值以上的百分比。理想情况下,我会为&#34; sd_qs&#34;表示每个月和季度阈值之上的百分比变化。最终目标是在下图中放置置信区间: enter image description here

我已经阅读了许多函数来计算高于阈值的百分比以及平均值和标准差等,但没有一个已经证明了这些的组合。是否有任何帮助计算SD高于阈值的百分比?

2 个答案:

答案 0 :(得分:0)

我使用中心极限定理来计算下限和上限。如果你想要的只是标准错误,你可以修改我的功能。

我没有试图找到一种方法来避免每季度重复一次计算。

q_se <- function(q,n,p){
  # Return th quartile for the standard error of a probability assumining the Central Limit Theorem.
  qnorm(q,p,sqrt((p/100)*(1-p/100)/n))

}

library(dplyr)

q1_t <- 2.5
q2_t <- 10
q3_t <- 20
q4_t <- 15

perc_test <- weath_k %>%
                filter(!is.na(PRCP)) %>%
                group_by(Qu, Mo) %>%
                summarize(n_days = n(),
                          n_q1t = sum(PRCP > q1_t),
                          p_q1t = round((n_q1t / n_days)*100, 2),
                          p_q1t_L = q_se(0.05,n_q1t,p_q1t),
                          p_q1t_U = q_se(0.95,n_q1t,p_q1t),
                          n_q2t = sum(PRCP > q2_t),
                          p_q2t = round((n_q2t / n_days)*100, 2),
                          p_q2t_L = q_se(0.05,n_q2t,p_q2t),
                          p_q2t_U = q_se(0.95,n_q2t,p_q2t),
                          n_q3t = sum(PRCP > q3_t),
                          p_q3t = round((n_q3t / n_days)*100, 2),
                          p_q3t_L = q_se(0.05,n_q3t,p_q3t),
                          p_q3t_U = q_se(0.95,n_q3t,p_q3t),
                          n_q4t = sum(PRCP > q4_t),
                          p_q4t = round((n_q4t / n_days)*100, 2) ,
                          p_q4t_L = q_se(0.05,n_q4t,p_q4t),
                          p_q4t_U = q_se(0.95,n_q4t,p_q4t)
                )

perc_test$p_qs <- c(perc_test$p_q1t[1:3], perc_test$p_q2t[4:6], 
                    perc_test$p_q3t[7:9], perc_test$p_q4t[10:12])

答案 1 :(得分:0)

经过大量的试验和错误后,我相信我已经达成了自己的答案,尽管这些代码可以肯定会得到改进。

q1_t <- 2.5
q2_t <- 10
q3_t <- 20
q4_t <- 10

year_test <- weath_k %>%
                filter(!is.na(PRCP)) %>%
                group_by(Year, Qu, Mo) %>%
                summarize(n_days = n(),
                          n_q1t = sum(PRCP > q1_t),
                          p_q1t = round((n_q1t / n_days)*100, 2),
                          n_q2t = sum(PRCP > q2_t),
                          p_q2t = round((n_q2t / n_days)*100, 2),
                          n_q3t = sum(PRCP > q3_t),
                          p_q3t = round((n_q3t / n_days)*100, 2),
                          n_q4t = sum(PRCP > q4_t),
                          p_q4t = round((n_q4t / n_days)*100, 2))

year_test$p_qs <- ifelse(year_test$Qu==1, year_test$p_q1t, 
                         ifelse(year_test$Qu==2, year_test$p_q2t,
                                ifelse(year_test$Qu==3, year_test$p_q3t,
                                       year_test$p_q4t)))

p_test <- year_test %>%
                group_by(Qu, Mo) %>%
                summarize(m.qs = mean(p_qs),
                          sd.qs = sd(p_qs))
p_test
# A tibble: 12 x 4
# Groups:   Qu [?]
      Qu    Mo       m.qs      sd.qs
   <dbl> <int>      <dbl>      <dbl>
 1     1     1  4.0327273  4.8255089
 2     1     2  8.4911111  7.9238986
 3     1     3  8.8893333  7.6625199
 4     2     4  7.4636957  5.8455865
 5     2     5 18.1893478  7.1200800
 6     2     6 27.4613333  8.8789374
 7     3     7 17.1231818 10.1258237
 8     3     8 16.7724444  8.2073313
 9     3     9 13.1262222  8.0349977
10     4    10 12.5718605  8.2473945
11     4    11  2.3306818  3.3184428
12     4    12  0.2153333  0.8148078