如何使lapply/sapply在R中输出一个列表?

huangapple go评论74阅读模式
英文:

How to make lapply/sapply output a list in R?

问题

我正在尝试弄清楚如何使用lapply/sapply创建一个列表。根据文档,我了解到可以使用sapplyUSE.NAMES = TRUE来实现,但我的示例没有起作用。

我需要将变量名中的空格替换为破折号,然后查找它们在输入列表中。我对这个列表没有直接的控制(它是R Shiny应用程序中的输入反应元素)。

input <- list("Subject-Identifier" = c("1022", "1023", "1024"),
              "Next-Screening" = c("A", "B", "C"))

variables <- c("Subject Identifier", "Next Screening")

### 我尝试了以下方法:
res <- lapply(variables, function(x){
  input[[stringr::str_replace_all(x, " ", "-")]]
})

[[1]]
[1] "1022" "1023" "1024"

[[2]]
[1] "A" "B" "C"

res <- sapply(variables, function(x){
  input[[stringr::str_replace_all(x, " ", "-")]]
}, USE.NAMES = TRUE)

     Subject Identifier Next Screening
[1,] "1022"             "A"           
[2,] "1023"             "B"           
[3,] "1024"             "C"           

### 我想要的是:

$`Subject Identifier`
[1] "1022" "1023" "1024"

$`Next Screening`
[1] "A" "B" "C"

如果您有任何其他问题,请告诉我。

英文:

I am trying to figure out how to create a list with lapply/sapply. From the documentation I gather this is possible using sapply with USE.NAMES = TRUE, but my example is not working.

I need to str-replace variable names to look them up in the input list where spaces are replaced with dashes. I have no direct control over this list (it is the input reactive element in an R shiny app)

input &lt;- list(&quot;Subject-Identifier&quot; = c(&quot;1022&quot;, &quot;1023&quot;, &quot;1024&quot;),
              &quot;Next-Screening&quot; = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;))

variables &lt;- c(&quot;Subject Identifier&quot;, &quot;Next Screening&quot;)


### I tried these:
res &lt;- lapply(variables, function(x){
  input[[stringr::str_replace_all(x, &quot; &quot;, &quot;-&quot;)]]
})

[[1]]
[1] &quot;1022&quot; &quot;1023&quot; &quot;1024&quot;

[[2]]
[1] &quot;A&quot; &quot;B&quot; &quot;C&quot;


res &lt;- sapply(variables, function(x){
  input[[stringr::str_replace_all(x, &quot; &quot;, &quot;-&quot;)]]
}, USE.NAMES = TRUE)

     Subject Identifier Next Screening
[1,] &quot;1022&quot;             &quot;A&quot;           
[2,] &quot;1023&quot;             &quot;B&quot;           
[3,] &quot;1024&quot;             &quot;C&quot;           


### I want this:

$`Subject Identifier`
[1] &quot;1022&quot; &quot;1023&quot; &quot;1024&quot;

$`Next Screening`
[1] &quot;A&quot; &quot;B&quot; &quot;C&quot;

答案1

得分: 2

以下是您要的翻译部分:

We can pass a named vector and lapply returns the named list (which can be done either before or afterwards)

lapply(setNames(variables, variables), function(x){
  input[[stringr::str_replace_all(x, " ", "-")]})

-output

$`Subject Identifier`
[1] "1022" "1023" "1024"

$`Next Screening`
[1] "A" "B" "C"

Regarding the second case, sapply by default use simplify = TRUE, which can be changed to FALSE and then it will return the expected

sapply(variables, function(x){
  input[[stringr::str_replace_all(x, " ", "-")]]
}, USE.NAMES = TRUE, simplify = FALSE)

-output

$`Subject Identifier`
[1] "1022" "1023" "1024"

$`Next Screening`
[1] "A" "B" "C"
英文:

We can pass a named vector and lapply returns the named list (which can be done either before or afterwards)

lapply(setNames(variables, variables), function(x){
  input[[stringr::str_replace_all(x, &quot; &quot;, &quot;-&quot;)]]})

-output

$`Subject Identifier`
[1] &quot;1022&quot; &quot;1023&quot; &quot;1024&quot;

$`Next Screening`
[1] &quot;A&quot; &quot;B&quot; &quot;C&quot;

Regarding the second case, sapply by default use simplify = TRUE, which can be changed to FALSE and then it will return the expected

sapply(variables, function(x){
  input[[stringr::str_replace_all(x, &quot; &quot;, &quot;-&quot;)]]
}, USE.NAMES = TRUE, simplify = FALSE)

-output

$`Subject Identifier`
[1] &quot;1022&quot; &quot;1023&quot; &quot;1024&quot;

$`Next Screening`
[1] &quot;A&quot; &quot;B&quot; &quot;C&quot;

答案2

得分: 1

只需对数据进行子集处理并设置名称:

例如

setNames(input[gsub(" ", "-", variables)], variables)

$`Subject Identifier`
[1] "1022" "1023" "1024"

$`Next Screening`
[1] "A" "B" "C"
英文:

You do not need to loop over the data. Just subset and set the names:

ie

setNames(input[gsub(&quot; &quot;, &quot;-&quot;, variables)], variables)

$`Subject Identifier`
[1] &quot;1022&quot; &quot;1023&quot; &quot;1024&quot;

$`Next Screening`
[1] &quot;A&quot; &quot;B&quot; &quot;C&quot;

huangapple
  • 本文由 发表于 2023年3月31日 23:56:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900522.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定