Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据R中的上一个值和下一个值设置值的条件_R - Fatal编程技术网

根据R中的上一个值和下一个值设置值的条件

根据R中的上一个值和下一个值设置值的条件,r,R,我试图评估给定数据集中的干旱期数量 这是一个数据示例 (precip指日总降水量) 创建列索引是为了将干燥日与潮湿日分开。干日是指总降水量不超过0.2mm的天数,指数为1。超过0.2mm的天数视为潮湿,指数为0 然而,我想补充另一个条件,如果一天相对干燥(降水量不超过1mm),并且介于干燥的两天之间(使用dplyr的滞后和超前函数,您可以: require(dplyr) mutate(df, index2 = (index | (precip <= 1 & lag(index) &

我试图评估给定数据集中的干旱期数量

这是一个数据示例 (precip指日总降水量)

创建列索引是为了将干燥日与潮湿日分开。干日是指总降水量不超过0.2mm的天数,指数为1。超过0.2mm的天数视为潮湿,指数为0


然而,我想补充另一个条件,如果一天相对干燥(降水量不超过1mm),并且介于干燥的两天之间(使用dplyr的滞后和超前函数,您可以:

require(dplyr)
mutate(df, index2 = (index | (precip <= 1 & lag(index) & lead(index))) + 0L)
#         date precip index index2
#1  1976-01-15   11.4     0      0
#2  1976-01-16   10.3     0      0
#3  1976-01-17    3.2     0      0
#4  1976-01-18    0.0     1      1
#5  1976-01-19    1.2     0      0
#6  1976-01-20    1.7     0      0
#7  1976-01-21    3.1     0      0
#8  1976-01-22    9.2     0      0
#9  1976-01-23    4.6     0      0
#10 1976-01-24    1.9     0      0
#11 1976-01-25    0.0     1      1
#12 1976-01-26    0.1     1      1
#13 1976-01-27    0.2     0      1
#14 1976-01-28    0.0     1      1
#15 1976-01-29    0.0     1      1
#16 1976-01-30    0.0     1      1
require(dplyr)

mutate(df,index2=(index |)(非常感谢!这正是我想要的!
        date precip index
 1976-01-15   11.4     0
 1976-01-16   10.3     0
 1976-01-17    3.2     0
 1976-01-18    0.0     1
 1976-01-19    1.2     0
 1976-01-20    1.7     0
 1976-01-21    3.1     0
 1976-01-22    9.2     0
 1976-01-23    4.6     0
 1976-01-24    1.9     0
 1976-01-25    0.0     1
 1976-01-26    0.1     1
 1976-01-27    0.2     1
 1976-01-28    0.0     1
 1976-01-29    0.0     1
 1976-01-30    0.0     1
require(dplyr)
mutate(df, index2 = (index | (precip <= 1 & lag(index) & lead(index))) + 0L)
#         date precip index index2
#1  1976-01-15   11.4     0      0
#2  1976-01-16   10.3     0      0
#3  1976-01-17    3.2     0      0
#4  1976-01-18    0.0     1      1
#5  1976-01-19    1.2     0      0
#6  1976-01-20    1.7     0      0
#7  1976-01-21    3.1     0      0
#8  1976-01-22    9.2     0      0
#9  1976-01-23    4.6     0      0
#10 1976-01-24    1.9     0      0
#11 1976-01-25    0.0     1      1
#12 1976-01-26    0.1     1      1
#13 1976-01-27    0.2     0      1
#14 1976-01-28    0.0     1      1
#15 1976-01-29    0.0     1      1
#16 1976-01-30    0.0     1      1