Skip to content

how to assign a value?

8 messages · Patrick Burns, William Dunlap, David Winsemius +1 more

#
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
#
On Dec 11, 2011, at 10:27 AM, Jinsong Zhao wrote:

            
Just one matrix? Then you seem to have inappropriately borrowed using  
"." as an indexing operation. In R that is just another character when  
used as an object name. "a.1" is notgoing to evaulate to  a[1].  Look  
at what you would have had after

 > for (i in 1:9) {
+   assign(paste("a.", i, sep = ""), 1:i)
+   }
 > ls()
  [1] "a"                    "a.1"                  "a.2"
  [4] "a.3"                  "a.4"                  "a.5"
  [7] "a.6"                  "a.7"                  "a.8"
[10] "a.9"

 > a.1
[1] 1
 > a.2
[1] 1 2

Each of those assign() operations created a single vector of length i.  
I doubt that was what you intended,

Better would be to describe your objects and your intentions, rather  
than expecting us to understand your goals by just looking at code  
that doesn't achieve thos goals. (There is no `get<-` function which  
was the source of the  error.)
David Winsemius, MD
West Hartford, CT
#
You are basically in R Inferno Circle 8.1.40.

http://www.burns-stat.com/pages/Tutor/R_inferno.pdf
On 11/12/2011 15:27, Jinsong Zhao wrote:

  
    
#
I find that get() and assign() are awkward to use
and that the syntax is easier if you put your objects into
a list or environment.  To me, it also makes it
clearer what the code is doing and keeps the output
of objects() shorter and easier to manage.  E.g.,

nResults <- 9
results <- vector("list", nResults) # or results <- new.env()
for(i in 1:nResults) {
   resultName <- paste("a.", i, sep="")
   results[[resultName]] <- 1:i
   results[[resultName]][i] <- i+50
}

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
#
On 2011-12-12 0:00, David Winsemius wrote:
yes, it was what I intended.
The question is why

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

give the following error message:

Error in get(paste("a.", i, sep = ""))[i] <- i + 50 :
   target of assignment expands to non-language object

The a.1 to  a.9 was created in the previous step.

if only

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

can give correct output. Why I cannot assign values to it?


Regards,
Jinsong
#
On 2011-12-12 1:16, Patrick Burns wrote:
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 Dec 11, 2011, at 9:07 PM, Jinsong Zhao wrote:

            
Then you are free to continue banging your head against a wall.
What part of THERE IS NO "get<-" function (much less a `get[<-`  
function)  don't you understand?
Right. They are there and can even be indexed:

 > get(paste("a", 9, sep="."))[9]
[1] 9

You could assign the value of get(paste("a", 9, sep=".")) to an  
intermediate object, which you could then reference using "[" and then  
use `assign` to push that object's value back to an object named "a. 
1", , "a.2", etc. Very clumsy and not an idiom that people want to  
promote.

 > x <- get(paste("a", 9, sep="."))
 > x[9] <- x[9]+50
 > assign(paste("a", 9, sep="."), x)
 > a.9
[1]  1  2  3  4  5  6  7  8 59
Using get, you mean? Because that is not the way R is designed. get()  
returns a value. `assign` is used... wait for it ... assignment.

 > get(paste("a", 1, sep="."))
[1] 1

Not a.1 but rather a.1's value. You cannot assign something else to  
the number 1. You are free to complain about the fact that R is is not  
languageX as much as you like, but it won't create new capabilities  
for functions. You've been given advice about how to get to the goal  
you desire by both Dunlap and Burns. The counter-question is why you  
have such trouble accepting advice.
David Winsemius, MD
West Hartford, CT
#
On 2011-12-12 10:58, David Winsemius wrote:
Sorry, I didn't understand it in the previous post. Now, it seems clear...
Yes, the intermediate object could be used to archive my goal:

for (i in 1:9) {
    assign(paste("a", i, sep = "."), 1:i)
    x <- get(paste("a", i, sep = "."))
    x[[i]] <- x[[i]] + 50
    assign(paste("a", i, sep = "."), x)
}
I don't have trouble accepting advice. I am just curious about the 
error. Thank you very much for your patience.
Regards,
Jinsong