将字符串迭代到公式中
Iterate a string into formula
我想遍历一个字符串列表,并将它们一个一个地附加到我的公式中,并在每次迭代后添加额外的名称。例如,我的代码产生以下输出:
require(faraway)
col_names <- names(teengamb)[-5]
form <-0
for(i in 1:length(col_names)){
form[i]=list(formula(paste('gamble ~', paste(col_names[i], collapse='+'))))
}
[[1]]
gamble ~ sex
[[2]]
gamble ~ status
[[3]]
gamble ~ income
[[4]]
gamble ~ verbal
预期输出:
[[1]]
gamble ~ sex
[[2]]
gamble ~ sex+status
[[3]]
gamble ~ sex+status+income
[[4]]
gamble ~ sex+status+income+verbal
一个可能的解决方案(我们需要将col_names[i]
替换为col_names[1:i]
):
require(faraway)
#> Loading required package: faraway
col_names <- names(teengamb)[-5]
form <-""
for(i in 1:length(col_names)){
form[i]=list(formula(paste('gamble ~', paste(col_names[1:i], collapse='+'))))
}
form
#> [[1]]
#> gamble ~ sex
#>
#> [[2]]
#> gamble ~ sex + status
#>
#> [[3]]
#> gamble ~ sex + status + income
#>
#> [[4]]
#> gamble ~ sex + status + income + verbal
虽然已经有一个可接受的答案,但这里有一个 reformulate
的方法。
data(teengamb, package = "faraway")
col_names <- names(teengamb)[-5]
form <- vector("list", length(col_names))
for(i in seq_along(col_names)){
form[[i]] <- reformulate(col_names[1:i], response = "gamb")
}
form
#> [[1]]
#> gamb ~ sex
#>
#> [[2]]
#> gamb ~ sex + status
#>
#> [[3]]
#> gamb ~ sex + status + income
#>
#> [[4]]
#> gamb ~ sex + status + income + verbal
由 reprex package (v2.0.1)
创建于 2022-06-04
这是另一个,这次是 purrr::map
。
form_function <- function(i, response = "gamb") {
reformulate(col_names[1:i], response = response)
}
col_names |>
seq_along() |>
purrr::map(form_function)
#> [[1]]
#> gamb ~ sex
#> <environment: 0x0000026801da3030>
#>
#> [[2]]
#> gamb ~ sex + status
#> <environment: 0x0000026801d924f8>
#>
#> [[3]]
#> gamb ~ sex + status + income
#> <environment: 0x0000026801c0c870>
#>
#> [[4]]
#> gamb ~ sex + status + income + verbal
#> <environment: 0x0000026801c13e80>
由 reprex package (v2.0.1)
创建于 2022-06-04
或者在 R 4.2.0 中引入的新管道运算符的基础 R 唯一方式,
col_names |>
seq_along() |>
lapply(form_function)
我想遍历一个字符串列表,并将它们一个一个地附加到我的公式中,并在每次迭代后添加额外的名称。例如,我的代码产生以下输出:
require(faraway)
col_names <- names(teengamb)[-5]
form <-0
for(i in 1:length(col_names)){
form[i]=list(formula(paste('gamble ~', paste(col_names[i], collapse='+'))))
}
[[1]]
gamble ~ sex
[[2]]
gamble ~ status
[[3]]
gamble ~ income
[[4]]
gamble ~ verbal
预期输出:
[[1]]
gamble ~ sex
[[2]]
gamble ~ sex+status
[[3]]
gamble ~ sex+status+income
[[4]]
gamble ~ sex+status+income+verbal
一个可能的解决方案(我们需要将col_names[i]
替换为col_names[1:i]
):
require(faraway)
#> Loading required package: faraway
col_names <- names(teengamb)[-5]
form <-""
for(i in 1:length(col_names)){
form[i]=list(formula(paste('gamble ~', paste(col_names[1:i], collapse='+'))))
}
form
#> [[1]]
#> gamble ~ sex
#>
#> [[2]]
#> gamble ~ sex + status
#>
#> [[3]]
#> gamble ~ sex + status + income
#>
#> [[4]]
#> gamble ~ sex + status + income + verbal
虽然已经有一个可接受的答案,但这里有一个 reformulate
的方法。
data(teengamb, package = "faraway")
col_names <- names(teengamb)[-5]
form <- vector("list", length(col_names))
for(i in seq_along(col_names)){
form[[i]] <- reformulate(col_names[1:i], response = "gamb")
}
form
#> [[1]]
#> gamb ~ sex
#>
#> [[2]]
#> gamb ~ sex + status
#>
#> [[3]]
#> gamb ~ sex + status + income
#>
#> [[4]]
#> gamb ~ sex + status + income + verbal
由 reprex package (v2.0.1)
创建于 2022-06-04这是另一个,这次是 purrr::map
。
form_function <- function(i, response = "gamb") {
reformulate(col_names[1:i], response = response)
}
col_names |>
seq_along() |>
purrr::map(form_function)
#> [[1]]
#> gamb ~ sex
#> <environment: 0x0000026801da3030>
#>
#> [[2]]
#> gamb ~ sex + status
#> <environment: 0x0000026801d924f8>
#>
#> [[3]]
#> gamb ~ sex + status + income
#> <environment: 0x0000026801c0c870>
#>
#> [[4]]
#> gamb ~ sex + status + income + verbal
#> <environment: 0x0000026801c13e80>
由 reprex package (v2.0.1)
创建于 2022-06-04或者在 R 4.2.0 中引入的新管道运算符的基础 R 唯一方式,
col_names |>
seq_along() |>
lapply(form_function)