例如,我有一个包含模型参数的 config.R
文件,如下所示:
var1 = "Fruit"
var2 = "Vegetables"
country = c("BRAZIL", "ECUADOR", "Georgia")
flag = NULL
peak = 30
years = c(2010, 2020)
remove1 = c("Bananas_ZZ_100s", "Apple_150-300_Pk", "Mango_mono")
remove2 = c("Tomato_ZR_400s", "cabbage300_Pk")
然后,我加载配置文件:
params = new.env()
source("config/config.R", params)
params = mget(ls(params), envir = params)
并获取带有参数的命名列表:
> print(params)
$var1
[1] "Fruit"
$var2
[1] "Vegetables"
$country
[1] "BRAZIL" "ECUADOR" "Georgia"
等等
我想将参数列表中的字符变量转换为小写,但两个变量除外:remove1 和 remove2
我能够以这种方式转换列表中的所有字符变量:
params = lapply(params, function(params) {
if (is.character(params)) return(tolower(params))
else return(params)
})
但我不知道如何将此函数(或者可能使用 for
循环)应用于所有字符 var。排除几个变量。
我将非常感谢任何帮助!
附注我想要得到什么:
> print(params)
$var1
[1] "fruit"
$var2
[1] "vegetables"
$country
[1] "brazil" "ecuador" "georgia"
$flag
NULL
$peak
[1] 30
$years
[1] 2010 2020
$remove1
[1] "Bananas_ZZ_100s" "Apple_150-300_Pk" "Mango_mono"
$remove2
[1] "Tomato_ZR_400s" "cabbage300_Pk"
最佳答案
由于您需要列表中的名称和值,因此我们可以使用Map
Map(function(x, y) if(is.character(x) & !y %in% c('remove1', 'remove2'))
tolower(x) else x, params, names(params))
#country
#[1] "brazil" "ecuador" "georgia"
#$flag
#NULL
#$peak
#[1] 30
#$remove1
#[1] "Bananas_ZZ_100s" "Apple_150-300_Pk" "Mango_mono"
#$remove2
#[1] "Tomato_ZR_400s" "cabbage300_Pk"
#$var1
#[1] "fruit"
#$var2
#[1] "vegetables"
#$years
#[1] 2010 2020
使用 purrr::imap
可能更容易
purrr::imap(params, ~if(is.character(.x) & !.y %in% c('remove1', 'remove2'))
tolower(.x) else .x)
您也可以先找出我们需要更改的元素,然后对其应用tolower
。
inds <- !names(params) %in% c('remove1', 'remove2') & sapply(params, is.character)
params[inds] <- lapply(params[inds], tolower)
关于R:for 循环或 lapply 到命名列表的某些元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59605651/