如何在具有多个条件的R中应用RLE函数?

如何解决如何在具有多个条件的R中应用RLE函数?

我有以下数据:

dat <- structure(list(year = c(1979L,1979L,1980L,1981L,1982L,1983L,1984L,1985L,1986L,1987L,1988L,1989L,1990L,1991L,1992L,1993L,1994L,1995L,1996L,1996L),mon = c(5L,5L,7L,6L,7L),day = c(16L,17L,18L,19L,20L,21L,22L,23L,24L,25L,26L,4L,8L,9L,10L,11L,12L,13L,14L,15L,16L,27L,28L,29L,30L,1L,2L,3L,31L,2L),phase = c(6L,1L),Rainfall = c(23.2,35.575,37.4,6.425,10.275,3.05,50.075,23.05,2,1.4,3.325,12.84,0.68,7.78,12.88,91.48,41.08,4.48,0.26,2.32,13.25,64.5,21.55,82.175,33.725,48.95,3.8,16.875,4.7,7.7,48.7,25.275,3.625,0.075,2.5,3.525,0.725,0.2,0.625,0.25,2.85,6.15,10.675,41.975,24.975,127.775,86.225,19.95,1.725,11.125,1.775,5.825,5.975,18.125,3.725,11.75,13.975,0.1,1,4.775,0.225,2.625,0.575,13.375,0.825,0.45,2.2,4.5,0.05,0.975,5.375,9.1,27.3,47.7,31.475,4.8,11.45,3.15,2.3,14.975,77.25,112.225,69.675,27.625,43.65,34.85,47.325,65.725,83.825,29.525,29.95,12.575,3.2,30.95,26.25,4.15,0.025,0.5,0.375,5.15,0.55,2.025,10.525,0.4,9.225,5.25,1.3,3.175,7.825,1.15,3.475,4.275,3.45,3.95,23.525,6.2,5.7,6.1,4.975,2.7,0.95,1.55,37.525,53.8,26.275,101.25,81.825,26.05,6.4,6.75,0.65,2.475,1.45,0.775,5.8,0.36,0.02,0.8,2.64,3.44,26.8,17.98,3.88,33.48,8.08,15.8,11.52,21.44,31.18,13.06,12.92,0.24,9.4,4.24,4.36,2.34,5.72,16.56,10.96,24.12,2.96,28.48,14.72,6.32,0.3,3.46,0.62,0.76,0.46,17.22,10.92,1.96,2.92,3.86,2.88,0.72,0.06,1.62,28.74,0.64,1.18,0.42,5.46,3.56,0.44,0.48,4.9,1.48,19.94,7.28,29.56,8.72,1.5,2.42,4.62,1.2,13.88,9.76,26.32,11,23.8,10.08,17.04,47.6,15.22,4.06,60.3,71.2,16.54,0.88,0.04,0.34,0.36)),row.names = c(NA,266L),class = "data.frame")

此数据中有四列:年,月,日,阶段,降雨。

我想:

(1)计算连续至少三天(长度> = 3)每天降雨量低于5毫米的情况

(2)和连续几天的开始(首次出现)应该具有等于1的阶段。

我有以下错误脚本:

 dat2<-subset(dat,phase == 1)
 countruns = function(x){
    rainfall_rle_df <- data.frame(unclass(rle(x$Rainfall < 5)))
    nrow(subset(rainfall_rle_df,lengths >= 3 & values == TRUE))
    }

 countruns(dat2)

此脚本给我的值为5,但这是不正确的。整个数据的正确答案应该只有4。

所以问题在于此脚本忽略了连续几天的开始阶段。

例如,上面数据中的1996年具有以下值:

 year   month day phase Rainfall
 1996   6  28     8    0.100
 1996   6  29     1    0.040
 1996   6  30     1    0.340
 1996   7   1     1    0.000
 1996   7   2     1    0.360

开始阶段是阶段8,因此不应计入该阶段。

满足上述条件的年份应该是1983、1984、1991和1993。

关于如何在R中执行此操作的任何建议?还是在R中有更好的方法?

我必须将脚本放入函数countruns()中。

我将不胜感激!

解决方法

在按“年”分组后,我们用rleid(来自data.table)创建分组列,然后保留{Rainfall”的all值小于5,{{ 1}}组,其中“ {phase}的filter值为1,并在first中进行频率计数(n()

summarise

如果我们要将其包装在函数中,library(dplyr) library(data.table) dat %>% # // create a Date column as it is easier to check for consecutive days mutate(Date = as.Date(paste(year,mon,day,sep="-"))) %>% # // create a group with year group_by(year) %>% # // add more groups with rleid on logical vector group_by(grp = rleid(Rainfall < 5),# // checks for difference between adjacent Date # // if the difference is greater than 1,cumsum increments by 1 grp2 = cumsum(c(0,abs(diff(Date))) > 1),.add = TRUE) %>% # // filter groups where all Rainfall is less than 5 and number of rows >= 3 filter(all(Rainfall < 5),n() >= 3) %>% # // filter the groups where the first value of phase is 1 filter(first(phase) == 1) %>% # // get the frequency count summarise(n = n(),.groups = 'drop') %>% # // remove the columns that are not needed select(-grp,-grp2) # A tibble: 4 x 2 # year n # <int> <int> #1 1983 8 #2 1984 11 #3 1991 6 #4 1993 5 会执行{{}} + enquo(当我们将未加引号的变量名作为参数传递给函数esp的列名时) / p>

!!

-应用功能

f1 <- function(data,year,month,rainfall) {
        data %>%
           mutate(Date = as.Date(str_c({{year}},{{month}},{{day}},sep="-"))) %>%
           group_by({{year}}) %>%
           group_by(grp = rleid({{rainfall}} < 5),grp2 = cumsum(c(0,.add = TRUE) %>%
           filter(all({{rainfall}} < 5),n() >=3 ) %>%
           filter(first(phase) == 1) %>%
           summarise(n = n(),.groups = 'drop') %>%
           select(-grp,-grp2)
     }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-