将列表变量转换为字符串

时间:2014-11-20 22:30:52

标签: r string list variables

我有以下代码:

     #subsets of different criteria

     IAEA_C2_NoStdCondition = subset(SelectedRNumberOnlyOutlierRejectionOrdered, 
                                     Job..R == "14047/2"
                                     & Weight_Initial > weight
                                     )

      IAEA_C2_EAstd = subset(SelectedRNumberOnlyOutlierRejectionOrdered, 
                             Job..R == "14047/2"
                             & Weight_Initial > weight
                             & AMS_Timetable_From_Results..Standard_Prep_Type == "EA"
                             )

      IAEA_C2_STstd = subset(SelectedRNumberOnlyOutlierRejectionOrdered, 
                             Job..R == "14047/2"
                             & Weight_Initial > weight
                             & AMS_Timetable_From_Results..Standard_Prep_Type == "ST"
                             )

      IAEA_C2_Bothstd = subset(SelectedRNumberOnlyOutlierRejectionOrdered, 
                               Job..R == "14047/2"
                               & Weight_Initial > weight
                               & AMS_Timetable_From_Results..Standard_Prep_Type == "Both"
                               )

R = 4        

for(i in 1:R) {
  RNumberList = NULL
  #make sure that names of the used subsets are listed in the 'list'
  RNumberList = list(IAEA_C2_NoStdCondition,
                     IAEA_C2_EAstd,
                     IAEA_C2_STstd,
                     IAEA_C2_Bothstd
                     )

  LoopVariable = RNumberList[[i]]
  #now I do different calculations with LoopVariable and so on

  #convert list Variable to string for later usage in filename as unique identifier!! 
  SubNameStr = deparse(substitute(RNumberList[[i]]))

  print(SubNameStr)
  # I get "RNumberList[[i]]" as string, but want for i=1 --> [1] "IAEA_C2_NoStdCondition" and for i=2 --> [1] "IAEA_C2_EAstd" and so on
  [1] "RNumberList[[i]]"

我首先进行一些子设置,然后从i到R开始for循环,其中R is是我放在list()中的子集数量。然后我调用RNumberList[[i]],将其传递给LoopVariable并进行一些计算(一切正常)然后我想将used subset Variable name in my list()转换为字符串,以便此字符串可用作唯一标识符在我的导出文件名中!

1 个答案:

答案 0 :(得分:0)

首先:

  RNumberList = NULL #you don't need this
  RNumberList = list(IAEA_C2_NoStdCondition, #this needs to be before the loop otherwise
                     IAEA_C2_EAstd,          #you just overwrite it again and again
                     IAEA_C2_STstd,          #which is inefficient
                     IAEA_C2_Bothstd
                     )

现在:

将上一个更改为:

RNumberList = list(IAEA_C2_NoStdCondition=IAEA_C2_NoStdCondition,
                     IAEA_C2_EAstd=IAEA_C2_EAstd,
                     IAEA_C2_STstd=IAEA_C2_STstd,
                     IAEA_C2_Bothstd=IAEA_C2_Bothstd
  )

SubNameStr = deparse(substitute(RNumberList[[i]]))

为:

SubNameStr = names(RNumberList)[i]

就是这样!

我将添加一个小例子:

IAEA_C2_NoStdCondition <- data.frame(a=runif(1:100))
IAEA_C2_EAstd <- data.frame(b=runif(1:100))
IAEA_C2_STstd <- data.frame(c=runif(1:100))
IAEA_C2_Bothstd <- data.frame(d=runif(1:100))

R=4
#make sure that names of the used subsets are listed in the 'list'
RNumberList = list(IAEA_C2_NoStdCondition=IAEA_C2_NoStdCondition,
                   IAEA_C2_EAstd=IAEA_C2_EAstd,
                   IAEA_C2_STstd=IAEA_C2_STstd,
                   IAEA_C2_Bothstd=IAEA_C2_Bothstd
)
for(i in 1:R) {

  LoopVariable = RNumberList[[i]]
  #now I do different calculations with LoopVariable and so on

  #convert list Variable to string for later usage in filename as unique identifier!! 
  SubNameStr = names(RNumberList)[i]

  print(SubNameStr)
  # I get "RNumberList[[i]]" as string, but want for i=1 --> [1] "IAEA_C2_NoStdCondition" and for i=2 --> [1] "IAEA_C2_EAstd" and so on
}

输出:

[1] "IAEA_C2_NoStdCondition"
[1] "IAEA_C2_EAstd"
[1] "IAEA_C2_STstd"
[1] "IAEA_C2_Bothstd"