R:for 循环或 lapply 到命名列表的某些元素

标签 r list for-loop apply lapply

例如,我有一个包含模型参数的 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/

相关文章:

r - 对矩阵的所有列组合进行计算

r - 训练数据中不存在的新因子水平

r - 计算 R 中的 R 平方内、R 平方之间或整体 R 平方

python - 将 pandas 对象提取到列表列表中并提取唯一值

c - 使用函数打印二维数组

python - 将字典从特定索引号附加到 pandas 数据帧

r - 在 R 中的文本文件中输出 J48 树

r - ggplot2 热图缩放行

c# - 从两个列表中生成一个列表

python - python中的对象引用列表