R - 从环境中的多个变量中获取值

R - get values from multiple variables in the environment

我当前的 R 环境中有一些变量:

ls()
 [1] "clt.list"      "commands.list" "dirs.list"     "eq"            "hurs.list"     "mlist"         "prec.list"     "temp.list"     "vars"         
[10] "vars.list"     "wind.list"    

其中每个变量 "clt.list"、"hurs.list"、"prec.list"、"temp.list" 和 "wind.list" 都是一个(巨大的)字符串列表。

例如:

clt.list[1:20]
 [1] "clt_Amon_ACCESS1-0_historical_r1i1p1_185001-200512.nc"        "clt_Amon_ACCESS1-3_historical_r1i1p1_185001-200512.nc"       
 [3] "clt_Amon_bcc-csm1-1_historical_r1i1p1_185001-201212.nc"       "clt_Amon_bcc-csm1-1-m_historical_r1i1p1_185001-201212.nc"    
 [5] "clt_Amon_BNU-ESM_historical_r1i1p1_185001-200512.nc"          "clt_Amon_CanESM2_historical_r1i1p1_185001-200512.nc"         
 [7] "clt_Amon_CCSM4_historical_r1i1p1_185001-200512.nc"            "clt_Amon_CESM1-BGC_historical_r1i1p1_185001-200512.nc"       
 [9] "clt_Amon_CESM1-CAM5_historical_r1i1p1_185001-200512.nc"       "clt_Amon_CESM1-CAM5-1-FV2_historical_r1i1p1_185001-200512.nc"
[11] "clt_Amon_CESM1-FASTCHEM_historical_r1i1p1_185001-200512.nc"   "clt_Amon_CESM1-WACCM_historical_r1i1p1_185001-200512.nc"     
[13] "clt_Amon_CMCC-CESM_historical_r1i1p1_190001-190412.nc"        "clt_Amon_CMCC-CESM_historical_r1i1p1_190001-200512.nc"       
[15] "clt_Amon_CMCC-CESM_historical_r1i1p1_190501-190912.nc"        "clt_Amon_CMCC-CESM_historical_r1i1p1_191001-191412.nc"       
[17] "clt_Amon_CMCC-CESM_historical_r1i1p1_191501-191912.nc"        "clt_Amon_CMCC-CESM_historical_r1i1p1_192001-192412.nc"       
[19] "clt_Amon_CMCC-CESM_historical_r1i1p1_192501-192912.nc"        "clt_Amon_CMCC-CESM_historical_r1i1p1_193001-193412.nc" 

我需要做的是提取 "Amon_" 和“_historical”之间的字符串子集。

我可以对单个变量执行此操作,如下所示:

levels(as.factor(sub(".*?Amon_(.*?)_historical.*", "\1", clt.list[1:20])))
 [1] "ACCESS1-0"        "ACCESS1-3"        "bcc-csm1-1"       "bcc-csm1-1-m"     "BNU-ESM"          "CanESM2"          "CCSM4"           
 [8] "CESM1-BGC"        "CESM1-CAM5"       "CESM1-CAM5-1-FV2" "CESM1-FASTCHEM"   "CESM1-WACCM"      "CMCC-CESM"

但是,我想做的是 运行 一次对所有五个变量执行上面的命令。我不想在上面的命令中仅使用 "ctl.list" 作为参数,而是想使用所有变量 "clt.list"、"hurs.list"、"prec.list"、"temp.list" 和 "wind.list"一次。

我该怎么做?

非常感谢!

一个解决方案是创建一个向量,其中包含您要从中提取数据的变量名称,例如:

var.names <- c("clt.list", "commands.list", "dirs.list")

然后从名称访问每个变量的值:

for (var.name in var.names) {
  var.value <- as.list(environment())[[var.name]]
  # Do something with var.value
}

您可以将您的操作放入一个函数中,然后对其进行迭代:

get_my_substr <- function(vecname) 
  levels(as.factor(sub(".*?Amon_(.*?)_historical.*", "\1", get(vecname))))

lapply(my_vecnames,get_my_substr)

lapply 就像一个循环。您可以使用

创建矢量名称列表
my_vecnames <- ls(pattern=".list$")


在您的问题中 post 一个可重现的例子通常是个好习惯。由于此处提供了 none,因此我使用...

测试了此方法
# example-maker
prestr <- "grr_Amon_"
posstr <- "_historical_zzz"
make_ex <- function() 
  replicate(
    sample(10,1),
    paste0(prestr,paste0(sample(LETTERS,sample(5,1)),collapse=""),posstr)
  )

# make a couple examples
set.seed(1)
m01 <- make_ex()
m02 <- make_ex()

# test result
lapply(ls(pattern="^m[0-9][0-9]$"),get_my_substr)