[R] Multiple assignments in one statement

Huntsinger, Reid reid_huntsinger at merck.com
Fri Jul 8 22:04:16 CEST 2005


Yes and no. Your function doesn't work because c() is a function and R is
call-by-value, so c(x,y,z) is just a value, like 7. 

You could use lists like this:

> l <- list(x=NULL,y=NULL,z=NULL)
> l[c("x","y","z")] <- 1:3
> l
$x
[1] 1

$y
[1] 2

$z
[1] 3

That, together with attaching lists (help(attach)), might give you the
effect you want.

Reid Huntsinger



-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Jeffrey Horner
Sent: Friday, July 08, 2005 3:45 PM
To: r-help at stat.math.ethz.ch
Subject: [R] Multiple assignments in one statement


Is this possible?

For instance, I have a function that returns a vector length 3. In one 
statement I'd like to assign each element of the vector to different 
variables. Syntactically, I hoped this would work:

c(x,y,z) <- myfun();

Thanks,
-- 
Jeffrey Horner       Computer Systems Analyst         School of Medicine
615-322-8606         Department of Biostatistics   Vanderbilt University

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html




More information about the R-help mailing list