【问题标题】:R - get values from multiple variables in the environmentR - 从环境中的多个变量中获取值
【发布时间】:2015-07-12 15:20:03
【问题描述】:

我当前的 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”。

我该怎么做?

非常感谢!

【问题讨论】:

    标签: r environment-variables


    【解决方案1】:

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

    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$")
    


    在您的问题中发布可重现的示例通常是一种很好的做法。由于此处未提供任何内容,因此我使用...测试了此方法。

    # 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)
    

    【讨论】:

    • 谢谢@Frank。该功能正是我想要的。另外,我更喜欢 lapply 命令,因为它避免了循环。
    【解决方案2】:

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

    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
    }
    

    【讨论】:

    • 感谢@Richard Ambler 的建议。不过,我宁愿避免循环。
    猜你喜欢
    • 2022-12-16
    • 2013-05-11
    • 2013-08-10
    • 2013-11-01
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    相关资源
    最近更新 更多