引用r中字符串的值

时间:2018-04-06 20:28:13

标签: r list

我正在尝试从R中的列表中打印值。

当我使用

cat("Relevant GPD Info for ", endowmentAssetClassVariableNames.v[i],"\n")

我收到了正确的列表项,例如"bbergComm"

但如果我尝试引用该列表项中的项目,例如

cat("Upper Thershold Return: ", paste0(endowmentAssetClassVariableNames.v[i], ".gpdFit$upper.thresh"), "\n")

我才回来

Upper Thershold Return:  bbergComm.gpdFit$upper.thresh, and not the value associated with the list item "bbergComm.gpdFit$upper.thresh.

我在循环中运行它,但无法让我的循环给我我引用的值。

如何在循环中获取与列表项关联的值?

for(i in 1:length(endowmentAssetClassVariableNames.v)) {
+   cat("Relevant GPD Info for ",     endowmentAssetClassVariableNames.v[i],"\n")
+   t<-(paste0(endowmentAssetClassVariableNames.v[i],     ".gpdFit$upper.thresh"))
+   cat("Upper Thershold Return: ",     paste0(endowmentAssetClassVariableNames.v[i], ".gpdFit$upper.thresh"), "\n")
+ }

Relevant GPD Info for  peReturn 
Upper Thershold Return:  peReturn.gpdFit$upper.thresh 
Relevant GPD Info for  spReturn 
Upper Thershold Return:  spReturn.gpdFit$upper.thresh 
Relevant GPD Info for  hfReturn 
Upper Thershold Return:  hfReturn.gpdFit$upper.thresh 
Relevant GPD Info for  wilshireReturn 
Upper Thershold Return:  wilshireReturn.gpdFit$upper.thresh 
Relevant GPD Info for  bbergComm 
Upper Thershold Return:  bbergComm.gpdFit$upper.thresh 
Relevant GPD Info for  tripleABond 
Upper Thershold Return:  tripleABond.gpdFit$upper.thresh 

所有gpd对象都具有以下结构

> str(peReturn.gpdFit)
List of 22
 $ n                    : int 3439
 $ data                 : num [1:3439] -0.1582 -0.0892 -0.0873 -0.0762 -0.0662 ...
 $ upper.exceed         : num [1:344] 0.0298 0.0721 0.0558 0.0201 0.0402 ...
 $ lower.exceed         : num [1:344] -0.0513 -0.0358 -0.0428 -0.0205 -0.0332 ...
 $ upper.thresh         : Named num 0.0165
  ..- attr(*, "names")= chr "90%"
 $ lower.thresh         : Named num -0.0161
  ..- attr(*, "names")= chr "10%"
 $ p.less.upper.thresh  : num 0.9
 $ p.larger.lower.thresh: num 0.9
 $ n.upper.exceed       : int 344
 $ n.lower.exceed       : int 344
 $ upper.method         : chr "ml"
 $ lower.method         : chr "ml"
 $ upper.par.ests       : Named num [1:2] 0.0108 0.1451
  ..- attr(*, "names")= chr [1:2] "lambda" "xi"
 $ lower.par.ests       : Named num [1:2] 0.0119 0.07
  ..- attr(*, "names")= chr [1:2] "lambda" "xi"
 $ upper.par.ses        : logi NA
 $ lower.par.ses        : logi NA
 $ upper.varcov         : logi NA
 $ lower.varcov         : logi NA
 $ upper.info           : logi NA
 $ lower.info           : logi NA
 $ upper.converged      : logi TRUE
 $ lower.converged      : logi TRUE
 - attr(*, "class")= chr "gpd"

我希望在一个循环中提取一些gpd对象值,我在其中指定与该对象关联的名称。

1 个答案:

答案 0 :(得分:0)

不评估字符串内的代码。想象一下,当你跑步

时会有多糟糕

cat(“相关的GPD信息为”,endowmentAssetClassVariableNames.v [i],“\ n”)

R认为forfor循环的开头,如果您碰巧有一个名为elev = 176的高程变量,则将12替换为{{1} }}

您不能将R176ant GPD Info...与字符串一起使用,但可以使用$[

所以这样做:

[[