【问题标题】:Rcpp: change a list item in a list of a listrcpp:更改列表中的列表项
【发布时间】: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 this outerList[[i]]`?请提供一个可重现的最小示例。
  • 您是否还期望环境能够反映您对列表所做的更改?你是在复制它们,而不是修改原来的。
  • “CharacterVector”而不是“Character Vecotr”。 - 对不起。
  • 能否请edit您的问题至少删除琐碎的拼写错误?

标签: c++ r list rcpp


【解决方案1】:

我发现您发布的一些代码很难遵循,但这是一项相当简单的任务,无论是在您第一次尝试时以硬编码方式从 R 中的全局环境访问列表,还是将列表作为范围;使用 C++ 代码

#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
void test(List x) {
    CharacterVector tmp = x[0];
    tmp[0] = "Z";
    x[0] = tmp;
}

// [[Rcpp::export]]
void test2() {
    Environment env = Environment::global_env();
    List x = env["ListR"];
    CharacterVector tmp = x[0];
    tmp[0] = "Y";
    x[0] = tmp;
}

/*** R
ListR <- list(a = LETTERS[1:3])
ListR
test(ListR)
ListR
test2()
ListR
*/

我进入R

> Rcpp::sourceCpp("modify-list.cpp")

> ListR <- list(a = LETTERS[1:3])

> ListR
$a
[1] "A" "B" "C"


> test(ListR)

> ListR
$a
[1] "Z" "B" "C"


> test2()

> ListR
$a
[1] "Y" "B" "C"

更新

这也很容易扩展到列表中的列表;使用 C++ 代码

#include <Rcpp.h>

using namespace Rcpp;

// For when you need to modify an element of a list within a list
// [[Rcpp::export]]
void test3() {
    Environment env = Environment::global_env();
    List y = env["listR"];
    List x = y[0];
    CharacterVector tmp = x[0];
    tmp[0] = "Z";
    x[0] = tmp;
}

我在 R 中得到以下内容

Rcpp::sourceCpp("modify-list.cpp")
listR = list(list())
listR[[1]] = list(LETTERS[1:3])
listR
# [[1]]
# [[1]][[1]]
# [1] "A" "B" "C"
test3()
listR
# [[1]]
# [[1]][[1]]
# [1] "Z" "B" "C"

【讨论】:

  • 非常感谢duckmayr!你是对的,我应该写得更清楚。硬编码的方式是我想要的。您是否有 R 中包含列表的列表示例。比如:listR = list(list()); listR[[1]] = c("A","B","C");
  • @Till 很容易扩展到列表中的列表;查看答案的更新
  • @Till 请随时接受并支持答案。 StackOverflow 就是这样工作的,有一个tour for new users
  • @DirkEddelbuettel 当然! :)
猜你喜欢
  • 1970-01-01
  • 2014-11-10
  • 2016-06-09
  • 1970-01-01
  • 1970-01-01
  • 2017-09-27
  • 1970-01-01
  • 1970-01-01
  • 2012-12-30
相关资源
最近更新 更多