【发布时间】:2018-11-12 09:53:29
【问题描述】:
更改列表中的列表项。
很遗憾,我在论坛中找不到我的问题的答案。
使用 rcpp,我想直接更改列表中的列表项。我有以下方法:
// [[Rcpp::export]]
void test(){
Environment env = Environment::global_env();
List outerList = env["ListR"];
List innerList = polymeraseFlagList[0];
outerList((1)) ) "test"; // correctly changing inner list
CharacterVector innerStr = innerList[1]; // correctly access to inner list element
}
但是,我只能更改完整列表列表 [i] 而不能更改单个元素:list [[i]] or list [i][j]。
outerList[i][j] = "new inner list element"; // not working
outerList[[i]] = "new inner list"; // not working
我可以提取内部列表,但这里我只更改新创建的列表,而不是旧列表。我必须直接更改 R Workspace 中的列表。我当然可以更改新创建的列表,然后将其分配给旧列表。不过,我希望这里有更优雅的解决方案。
我还尝试在分配列表之前声明它,以便我已经有一个可以照常访问的嵌套列表。不幸的是,这不起作用。
List outerList = List::create(Named("lst")); // not working
最后,我希望以下内容成为可能(直接在 R 工作区中更改变量):
// [[Rcpp::export]]
void test(){
Environment env = Environment::global_env();
List outerList = env["ListR"];
CharacterVector innerStr = outerList[i][j];
CharacterVector innerList = outerList[[i]]
innerList[i][j] = "new String";
}
如果有人可以帮助我,那就太好了。
非常感谢:)
【问题讨论】:
-
"列表 innerList = polymeraseFlagList[0];"是“列表 innerList = outerList[0];”
-
这是什么:` outerList((1)) ) "test";
? And thisouterList[[i]]`?请提供一个可重现的最小示例。 -
您是否还期望环境能够反映您对列表所做的更改?你是在复制它们,而不是修改原来的。
-
“CharacterVector”而不是“Character Vecotr”。 - 对不起。
-
能否请edit您的问题至少删除琐碎的拼写错误?