2

我一直在研究一个数据集,该数据集将在多个气候站上通用,用于分析温度和降水。我遇到了设计“气候规范”的砖墙,我已经成功计算了每日平均温度TAVG、每月平均温度,并AVG_TAVG总结了每月的总数。PRCPSNOW

我处于停滞状态的是计算偏离正常值,目前,1981 年至 2010 年的数据被认为是气候规范。

这是我的数据集目前的样子:

mso_light
    
    year month day  date       PRCP SNOW  SNWD TMAX TMIN  TAVG
1   1948    1   1   1948-01-01    0    0   102   44 -122 -39.0
2   1948    1   2   1948-01-02    3    0    51   44    6  25.0
3   1948    1   3   1948-01-03    0    0    25   44  -39   2.5
4   1948    1   4   1948-01-04   38   64    76   33  -56 -11.5
5   1948    1   5   1948-01-05    0    0    76   -6  -83 -44.5
6   1948    1   6   1948-01-06  107    0    51   22  -61 -19.5
7   1948    1   7   1948-01-07  147    0    25   28  -17   5.5
8   1948    1   8   1948-01-08    8   13    25   39  -83 -22.0
9   1948    1   9   1948-01-09    0    0    25   -6 -117 -61.5
10  1948    1   10  1948-01-10    8   10    25  -11 -156 -83.5

所以我最初觉得我需要date用于排序目的,如果将来不需要我会删除它。

接下来,我想为 增加一列DepNormT,该列的计算方法是从 1981 年到 2010 年每年 1 月 1 日到 12 月 31 日取平均值TAVG,以找到正常的平均温度。然后将是其自身与整个数据集DepNormT之间的差异。TAVG

我尝试了多种方法来实现这一点,这里有两个版本:

mso_DeptT <- mso_light %>%
  group_by(month, day) %>%
  mean(mso_light$TAVG[1981:2010], na.rm = T) %>%
  ungroup()

这给了我以下错误:

  no applicable method for 'ungroup' applied to an object of class "c('double', 'numeric')"
In addition: Warning message:
In mean.default(., mso_light$TAVG[1981:2010], na.rm = T) :
  argument is not numeric or logical: returning NA

这是另一个版本:

##mso_DeptT <- filter(mso_light, year >= "1981", year <= "2010") %>%
##  group_by(day, month) %>%
##  mutate(daily_DeptT = mean(TAVG, na.rm = T)) %>%
##  ungroup()

mso_sum <- mso_light %>%
  group_by(month, year) %>% 
  summarize(AVG_TAVG=mean(TAVG, na.rm = TRUE),
          T_PRCP=sum(PRCP, na.rm=TRUE),
          T_SNOW=sum(SNOW, na.rm=TRUE)) %>% 
  ungroup()

## To find monthly normal precipitation and snowfall - using dataset mso_sum

cli_Avg <- filter(mso_sum, year >= "1981", year <= "2010") %>%
  group_by(month) %>%
  summarize(Mon_Precip = mean(T_PRCP, na.rm = T),
            Mon_Snow = mean(T_SNOW, na.rm = T))

这给了我一个 30 年的平均值,等于每一天的平均值TAVG。例如:

    year month day  date       PRCP SNOW  SNWD TMAX TMIN  TAVG  DepNormT
1   1948    1   1   1948-01-01    0    0   102   44 -122 -39.0      -39.0
2   1948    1   2   1948-01-02    3    0    51   44    6  25.0       25.0
3   1948    1   3   1948-01-03    0    0    25   44  -39   2.5        2.5
4   1948    1   4   1948-01-04   38   64    76   33  -56 -11.5        ect
5   1948    1   5   1948-01-05    0    0    76   -6  -83 -44.5          .
6   1948    1   6   1948-01-06  107    0    51   22  -61 -19.5          .
7   1948    1   7   1948-01-07  147    0    25   28  -17   5.5          .
8   1948    1   8   1948-01-08    8   13    25   39  -83 -22.0
9   1948    1   9   1948-01-09    0    0    25   -6 -117 -61.5
10  1948    1   10  1948-01-10    8   10    25  -11 -156 -83.5

感谢您的建议。

4

1 回答 1

0

所以我尝试了你的建议:

mso_light %>%
  group_by(month, day) %>%
  summarise(CliAvgT = mean(TAVG[1981:2010], na.rm = T)) %>%
  mutate(Avg_DepT = CliAvgT - TAVG) %>%
  ungroup()

我收到此错误:

`summarise()` regrouping output by 'month' (override with `.groups` argument)
Error: Problem with `mutate()` input `Avg_DepT`.
x object 'TAVG' not found
i Input `Avg_DepT` is `CliAvgT - TAVG`.
i The error occured in group 1: month = 1.
Run `rlang::last_error()` to see where the error occurred.

我跑了rlang::last_trace(),下面是结果。这是我的困惑,TAVG存在于每个mso_light数据库实例中。

> rlang::last_trace()
<error/dplyr_error>
Problem with `mutate()` input `Avg_DepT`.
x object 'TAVG' not found
i Input `Avg_DepT` is `CliAvgT - TAVG`.
i The error occured in group 1: month = 1.
Backtrace:
     x
  1. \-`%>%`(...)
  2.   +-base::withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
  3.   \-base::eval(quote(`_fseq`(`_lhs`)), env, env)
  4.     \-base::eval(quote(`_fseq`(`_lhs`)), env, env)
  5.       \-`_fseq`(`_lhs`)
  6.         \-magrittr::freduce(value, `_function_list`)
  7.           \-function_list[[i]](value)
  8.             +-dplyr::mutate(., Avg_DepT = CliAvgT - TAVG)
  9.             \-dplyr:::mutate.data.frame(., Avg_DepT = CliAvgT - TAVG)
 10.               \-dplyr:::mutate_cols(.data, ...)
<parent: error/simpleError>
object 'TAVG' not found
> 
于 2020-08-16T01:34:27.620 回答