[R] how to assign a value?

Jinsong Zhao jszhao at yeah.net
Mon Dec 12 03:25:36 CET 2011


On 2011-12-12 1:16, Patrick Burns wrote:
> You are basically in R Inferno Circle 8.1.40.
>
> http://www.burns-stat.com/pages/Tutor/R_inferno.pdf
>

Thanks.

R_inferno is a good material for me. In this document, there is several 
sections titled "string not the name". I try try to change

get(paste("a.", i, sep = ""))[i] <- i+50

to

assign(get(paste("a.", i, sep = ""))[i], i+50)

however, error message:

Error in assign(get(paste("a.", i, sep = ""))[i], i + 50) :
   invalid first argument

I don't know why...

Regards,
Jinsong


> On 11/12/2011 15:27, Jinsong Zhao wrote:
>> Hi there,
>>
>> I hope to modify values in a vector or matrix in the following code:
>>
>> for (i in 1:9) {
>> assign(paste("a.", i, sep = ""), 1:i)
>> get(paste("a.", i, sep = ""))[i] <- i+50
>> }
>>
>> I get the following error message:
>>
>> Error in get(paste("a.", i, sep = ""))[i] <- i + 50 :
>> target of assignment expands to non-language object
>>
>> I have read the FAQ "How can I turn a string into a variable?", however,
>> I don't find a way to deal with:
>>
>> get(paste("a.", i, sep = ""))[i] <- i+50
>>
>> Any suggestions? Thanks in advance!
>>
>> Regards,
>> Jinsong
>>



More information about the R-help mailing list