Johannes
Johannes

Reputation: 69

How to access data saved in an assign construct?

I made a list, read the list into a for loop, do some calculations with it and export a modified dataframe to [1] "IAEA_C2_NoStdConditionResiduals1" [2] "IAEA_C2_EAstdResiduals2" ect. When I do View(IAEA_C2_NoStdConditionResiduals1) after the for loop then I get the following error message in the console: Error in print(IAEA_C2_NoStdConditionResiduals1) : object 'IAEA_C2_NoStdConditionResiduals1' not found, but I know it is there because RStudio tells me in its Environment view. So the question is: How can I access the saved data (in this assign construct) for further usage?

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) 

Upvotes: 0

Views: 27

Answers (1)

IRTFM
IRTFM

Reputation: 263301

Not really a problem with assign and more with behavior of the paste function. This will build a variable name with a space in it:

assign(paste(SubNameString, j), LoopVar)

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

,,,, so you need to get its value by putting backticks around its name so the space is not misinterpreted as a parse-able delimiter. See what happens when you type:

`IAEA_C2_NoStdCondition 1`

... and from here forward, use paste0 to avoid this problem.

Upvotes: 1

Related Questions