zoo::rollapply 窗口超过列值而不是行

如何解决zoo::rollapply 窗口超过列值而不是行

dat = structure(list(index = c(10505L,10506L,10511L,10539L,10542L,10579L,10642L,11008L,11012L,13011L,13110L,13116L,13118L,13156L,13259L,13273L,13313L,13365L,13380L,13382L,13445L,13453L,13482L,13483L,13494L,13543L,13550L,14462L,14464L,14564L,14599L,14604L,14674L,14719L,14728L,14775L,14860L,14874L,14930L,14933L,14975L,15031L,15089L,15117L,15179L,15211L,15241L,15245L,15255L,15260L,15418L,15585L,15627L,15644L,15774L,15776L,15777L,15790L,15791L,15833L,15849L,15850L,15886L,16042L,16127L,16140L,16141L,16142L,16365L,16485L,16489L,16515L,16542L,16738L,16834L,16949L,17272L,17462L,17569L,17571L,17641L,17654L,17694L,17695L,17709L,17748L,17836L,17922L,18643L,20113L,20131L,28914L,29318L,30524L,30741L,30912L,30923L,30998L,46650L,46698L),V2 = c(3L,3L,2L,1L,0L,5L,1L)),row.names = c(NA,-100L),class = "data.frame")

假设我想在滚动窗口中跨 dat 计算一个函数。

n_sites = function(x) {
    return(sum(x > 1))
}
zoo::rollapply(dat$V2,FUN=n_sites,width=100)

但是,我不想使用行数作为窗口大小,而是使用 index 列中的实际数值。所以我想让每个窗口在索引列中包含大约 100 个单位。鉴于第 1 行和第 7 行之间大约有 100 个单位的 index,第一个窗口将包含这些行。这可能吗?

很高兴使用 zoodata.table 等解决方案。

解决方法

您也可以使用包 runner,其中参数 idx 正是您要查找的内容

dat$n_sites <- runner::runner(x = dat$V2,idx = dat$index,k = 100,f = n_sites)

head(dat,10)
   index V2 n_sites
1  10505  3       1
2  10506  3       2
3  10511  3       3
4  10539  2       4
5  10542  2       5
6  10579  2       6
7  10642  2       2
8  11008  1       0
9  11012  0       0
10 13011  3       1

,

您可以使用 slider::slide_index 代替 zoo::rollapply

library(slider)
dat$n_sites <- slider::slide_index(.x = dat$V2,.i = dat$index,.f = n_sites,.before = 100)

head(dat,10)
   index V2 n_sites
1  10505  3       1
2  10506  3       2
3  10511  3       3
4  10539  2       4
5  10542  2       5
6  10579  2       6
7  10642  2       3
8  11008  1       0
9  11012  0       0
10 13011  3       1
,

rollapply 中的宽度可以是一个向量,使得第 i 个元素是用于第 i 行的宽度。对这个问题有多种解释。我们可以使用最大宽度不超过 100 个索引单元,最小宽度至少 100 个索引单元或最接近 100 个索引单元的宽度。问题似乎要求第三个,但示例宽度 7 与此不一致,并表明可能需要第二种解释。我们在最后给出了所有三个宽度。选择你想要的。问题还说第一个窗口是 7 表示需要左对齐。

library(zoo)

w <- w2 # see calcs of w1,w2 and w3 at end.  Use whichever you want.
transform(dat,roll = rollapplyr(V2,w,n_sites,fill = NA,align = "left"))

如果 n_sites 只是实际功能的替代品,那么我们可以使用上面的,但如果它是实际功能,我们可以消除它并像这样写:

transform(dat,roll = rollapplyr(V2 > 1,sum,align = "left"))

宽度

这可能有很多变化,我们计算了这里提到的三个。

下面的代码使用基础 R 的 findInterval。回想一下 findInterval(x,vec),其中 x 和 vec 是向量,而 vec 是非递减的,返回一个与 x 长度相同的向量,使得结果的第 i 个分量是 sum(x[i] >= vec) 但是这样做更有效。也就是说,如果在 vec 中找到 x[i],那么它会在 vec 中找到等于 x[i] 的最后一个位置,如果 x[i] 不在 vec 中,那么它会在 vec 中找到小于 x[一世]。请注意,它返回位置,即索引,而不是 vec 的值。例如, findInterval(c(20,30),c(10,30,40)) 返回 c(1,4) 因为 1 是 vec 中小于 20 的最大值的位置,而 4 是vec 中最后一个值的位置等于 30。

n <- nrow(dat)
index <- dat$index

# i1 is row number of last index no more than current index + 100
i1 <- findInterval(index + 100,index)
w1 <- i1 - 1:n + 1

# i2 is row number of first index at least equal to index + 100
i2 <- pmin(findInterval(index + 100 - 1,index) + 1,n)
w2 <- i2 - 1:n + 1
w2[1]
## [1] 7

# i is row number of index closest to current index + 100
i <- ifelse(index + 100 - index[i1] <= index[i2] - (index + 100),i1,i2)
w3 <- i - 1:n + 1

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

相关推荐


使用本地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-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res