英文:
How to make lapply/sapply output a list in R?
问题
我正在尝试弄清楚如何使用lapply/sapply创建一个列表。根据文档,我了解到可以使用sapply
和USE.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 <- list("Subject-Identifier" = c("1022", "1023", "1024"),
"Next-Screening" = c("A", "B", "C"))
variables <- c("Subject Identifier", "Next Screening")
### I tried these:
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"
### I want this:
$`Subject Identifier`
[1] "1022" "1023" "1024"
$`Next Screening`
[1] "A" "B" "C"
答案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, " ", "-")]]})
-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"
答案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(" ", "-", variables)], variables)
$`Subject Identifier`
[1] "1022" "1023" "1024"
$`Next Screening`
[1] "A" "B" "C"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论