我正在尝试编写一个基于降水类型(雪、雨、混合)的if else语句。我需要对降雨量为雪或混合时使用基于风速的校正(即,针对降雨量的校正系数),但如果是降雨,则不需要应用校正系数。
以下是我尝试为此编写的代码:
mutate(Precip_Corr = ifelse(
PrecipType == "Snow", exp(-0.04*Wind_ms^1.75),
PrecipType == "Mixed", 1.0104-(0.0562*Wind_ms)),
Precip_Type == "Rain", Precip_mm)下面是df:
date.time Air.Temp_F DewPtTemp_F RH_. WindDir_DegfromN WindSp_knots HourlyPrecip_in HourlyPrecipT.in Precip_mm precip_cum_mm
1 2019-09-01 00:55:00 55.9 52 86.71 0 0 0 0.000 0.000 0.000
2 2019-09-01 01:55:00 61.0 54 77.72 310 3 T 0.005 0.127 0.127
3 2019-09-01 02:55:00 60.1 54 80.25 0 0 0 0.000 0.000 0.127
4 2019-09-01 03:55:00 57.9 54 86.82 170 5 0 0.000 0.000 0.127
5 2019-09-01 04:55:00 57.0 53.1 86.77 0 0 0 0.000 0.000 0.127
6 2019-09-01 05:33:00 57.9 54 86.82 260 4 0 0.000 0.000 0.127
AirTemp_C PrecipType Wind_ms
1 13.27778 Rain 0.000000
2 16.11111 Rain 1.543210
3 15.61111 Rain 0.000000
4 14.38889 Rain 2.572016
5 13.88889 Rain 0.000000
6 14.38889 Rain 2.057613这是我得到的错误:
Error: Problem with `mutate()` input `Precip_Corr`.
x unused argument (1.0104 - (0.0562 * Wind_ms))
i Input `Precip_Corr` is `ifelse(...)`.发布于 2020-10-09 11:53:46
上面的代码中有两个错误:
case_when而不是the正如所写的那样,您的ifelse语句在错误的位置有一个括号:
mutate(Precip_Corr = ifelse(
PrecipType == "Snow", exp(-0.04*Wind_ms^1.75),
PrecipType == "Mixed", 1.0104-(0.0562*Wind_ms)), # this bracket closes the ifelse statement
Precip_Type == "Rain", Precip_mm) # these are in the mutate, not the ifelse将此更正为:
mutate(Precip_Corr = ifelse(
PrecipType == "Snow", exp(-0.04*Wind_ms^1.75),
PrecipType == "Mixed", 1.0104-(0.0562*Wind_ms), # remove excess bracket
Precip_Type == "Rain", Precip_mm)) # add moved bracket然而,在这次修正之后,关于ifelse的争论太多了
ifelse有3个参数,如下所示:ifelse(condition, value_if_yes, value_if_no)case_when case_when(condition1 ~ value1, condition2 ~ value2, ... )所以你可能想要的代码是:
mutate(Precip_Corr = case_when(
PrecipType == "Snow" ~ exp(-0.04*Wind_ms^1.75),
PrecipType == "Mixed" ~ 1.0104-(0.0562*Wind_ms),
Precip_Type == "Rain" ~ Precip_mm))请注意,在条件及其值之间使用~而不是逗号。
https://stackoverflow.com/questions/64266933
复制相似问题