r - 如何修改R中一系列列表的元素?
问题描述
这是示例:
R<-vector("list",10) #a list of 10 elements
r<-rep(0,5) # each list includes this vector
#we add the vector to all lists
for(i in 1:10)
{
R[[i]]$r=r
}
index<-c(2,3,6)
现在我想要做的是r[1]
在2
所有R
由index
. 如果我们有一个大小为 1 的索引,那么解决方案将是R[[index]]$r[1]=2
. 但是当我们有一系列索引时呢?实现这一目标的最有效方法是什么?
解决方案
感谢@rawr 关于使用的建议replace()
library(purrr)
index<-c(2,3,6)
modify_at(R, index,~list(r=replace(.$r, 1, 2)))
作为对以下评论的回应,您可以将第一个元素替换为第一个元素加 2,如下所示:
R <- modify_at(R, index,~list(r=replace(.$r, 1, .$r[1]+2)))
推荐阅读
- c++ - 如何禁用 QTabWidget 中的更改选项卡?
- laravel - Laravel 队列和作业 - 如何稍后重新运行失败的作业
- asp.net - 主页上的 ASP.NET VB.NET 搜索按钮未导航到正确的目录
- python - 如何通过 Python Kubernetes 客户端连接到我的 k8 应用服务?
- linux - 如何将文件中的 bi-us-rds-15september 文本值更改为 bi-us-rds- + 当前日期,以 september 作为月份名称?
- android - Android中的单元测试isNetworkAvailable()
- java - Hibernate 过滤器不适用于连接实体
- javascript - 角度过滤器数组
- php - 如何在codeigniter中设置查询超时
- ruby-on-rails - codebuild / rails / rspec 失败未发送错误信号