如何访问保存在assign构造中的数据?

时间:2014-12-03 01:43:11

标签: r variable-assignment

我制作了一个列表,将列表读入for循环,用它做一些计算并将修改后的数据框导出到[1] "IAEA_C2_NoStdConditionResiduals1" [2] "IAEA_C2_EAstdResiduals2"等。当我在for循环后执行View(IAEA_C2_NoStdConditionResiduals1)时,我在控制台中收到以下错误消息:Error in print(IAEA_C2_NoStdConditionResiduals1) : object 'IAEA_C2_NoStdConditionResiduals1' not found,但我知道它在那里,因为RStudio在其环境视图中告诉我。所以问题是:如何访问已保存的数据(在此分配结构中)以供进一步使用?

ResidualList = 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,
                TIRI_I_NoStdCondition = TIRI_I_NoStdCondition,
                TIRI_I_EAstd = TIRI_I_EAstd,
                TIRI_I_STstd = TIRI_I_STstd,
                TIRI_I_Bothstd = TIRI_I_Bothstd
                )          

C = 8

for(j in 1:C) {

#convert list Variable to string for later usage as Variable Name as unique identifier!!    

SubNameString = names(ResidualList)[j]
SubNameString = paste0(SubNameString, "Residuals")

#print(SubNameString)
LoopVar = ResidualList[[j]]



LoopVar[ ,"F_corrected_normed"] = round(LoopVar[ ,"F_corrected_normed"] / mean(LoopVar[   ,"F_corrected_normed"]),
                                            digit = 5
                                            )

LoopVar[ ,"F_corrected_normed_error"] = round(LoopVar[ ,"F_corrected_normed_error"] / mean(LoopVar[ ,"F_corrected_normed_error"]),
                                                  digit = 5
                                                  )
assign(paste(SubNameString, j), LoopVar)

}

View(IAEA_C2_NoStdConditionResiduals1) 

1 个答案:

答案 0 :(得分:1)

assign并不是真正的问题,paste函数的行为更多。这将构建一个带有空格的变量名称:

assign(paste(SubNameString, j), LoopVar)

#simple example
> assign(paste("v", 1), "test")
> `v 1`
[1] "test"

,,,,因此您需要通过在其名称周围添加反引号来获取其值,以便空间不会被误解为可解析的分隔符。看看你输入时会发生什么:

`IAEA_C2_NoStdCondition 1`

...从此处开始,使用paste0来避免此问题。