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

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

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

问题

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

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

  1. input <- list("Subject-Identifier" = c("1022", "1023", "1024"),
  2. "Next-Screening" = c("A", "B", "C"))
  3. variables <- c("Subject Identifier", "Next Screening")
  4. ### 我尝试了以下方法:
  5. res <- lapply(variables, function(x){
  6. input[[stringr::str_replace_all(x, " ", "-")]]
  7. })
  8. [[1]]
  9. [1] "1022" "1023" "1024"
  10. [[2]]
  11. [1] "A" "B" "C"
  12. res <- sapply(variables, function(x){
  13. input[[stringr::str_replace_all(x, " ", "-")]]
  14. }, USE.NAMES = TRUE)
  15. Subject Identifier Next Screening
  16. [1,] "1022" "A"
  17. [2,] "1023" "B"
  18. [3,] "1024" "C"
  19. ### 我想要的是:
  20. $`Subject Identifier`
  21. [1] "1022" "1023" "1024"
  22. $`Next Screening`
  23. [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)

  1. input &lt;- list(&quot;Subject-Identifier&quot; = c(&quot;1022&quot;, &quot;1023&quot;, &quot;1024&quot;),
  2. &quot;Next-Screening&quot; = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;))
  3. variables &lt;- c(&quot;Subject Identifier&quot;, &quot;Next Screening&quot;)
  4. ### I tried these:
  5. res &lt;- lapply(variables, function(x){
  6. input[[stringr::str_replace_all(x, &quot; &quot;, &quot;-&quot;)]]
  7. })
  8. [[1]]
  9. [1] &quot;1022&quot; &quot;1023&quot; &quot;1024&quot;
  10. [[2]]
  11. [1] &quot;A&quot; &quot;B&quot; &quot;C&quot;
  12. res &lt;- sapply(variables, function(x){
  13. input[[stringr::str_replace_all(x, &quot; &quot;, &quot;-&quot;)]]
  14. }, USE.NAMES = TRUE)
  15. Subject Identifier Next Screening
  16. [1,] &quot;1022&quot; &quot;A&quot;
  17. [2,] &quot;1023&quot; &quot;B&quot;
  18. [3,] &quot;1024&quot; &quot;C&quot;
  19. ### I want this:
  20. $`Subject Identifier`
  21. [1] &quot;1022&quot; &quot;1023&quot; &quot;1024&quot;
  22. $`Next Screening`
  23. [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)

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

-output

  1. $`Subject Identifier`
  2. [1] "1022" "1023" "1024"
  3. $`Next Screening`
  4. [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

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

-output

  1. $`Subject Identifier`
  2. [1] "1022" "1023" "1024"
  3. $`Next Screening`
  4. [1] "A" "B" "C"
英文:

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

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

-output

  1. $`Subject Identifier`
  2. [1] &quot;1022&quot; &quot;1023&quot; &quot;1024&quot;
  3. $`Next Screening`
  4. [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

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

-output

  1. $`Subject Identifier`
  2. [1] &quot;1022&quot; &quot;1023&quot; &quot;1024&quot;
  3. $`Next Screening`
  4. [1] &quot;A&quot; &quot;B&quot; &quot;C&quot;

答案2

得分: 1

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

例如

  1. setNames(input[gsub(" ", "-", variables)], variables)
  2. $`Subject Identifier`
  3. [1] "1022" "1023" "1024"
  4. $`Next Screening`
  5. [1] "A" "B" "C"
英文:

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

ie

  1. setNames(input[gsub(&quot; &quot;, &quot;-&quot;, variables)], variables)
  2. $`Subject Identifier`
  3. [1] &quot;1022&quot; &quot;1023&quot; &quot;1024&quot;
  4. $`Next Screening`
  5. [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:

确定