我一直在开发一个数据集,该数据集将在多个气候站上通用,用于分析温度和降水。我遇到了设计“气候指标”的困难,我成功地计算出了日平均气温TAVG,月平均气温AVG_TAVG,并将PRCP和SNOW相加为月总数。
目前,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日至10月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谢谢你的建议。
发布于 2020-08-16 09:34:28
所以我试过你的建议:
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(),下面是结果。这是我的困惑,mso_light数据库的每个实例都存在TAVG。
> 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
> https://stackoverflow.com/questions/63418424
复制相似问题