[R] list assignment syntax?

Justin Haynes jtor14 at gmail.com
Sat Mar 31 02:08:23 CEST 2012


You can also take a look at

http://stackoverflow.com/questions/7519790/assign-multiple-new-variables-in-a-single-line-in-r

which has some additional solutions.



On Fri, Mar 30, 2012 at 4:49 PM, Peter Ehlers <ehlers at ucalgary.ca> wrote:
> On 2012-03-30 15:40, ivo welch wrote:
>>
>> Dear R wizards:  is there a clean way to assign to elements in a list?
>>  what I would like to do, in pseudo R+perl notation is
>>
>>  f<- function(a,b) list(a+b,a-b)
>>  (c,d)<- f(1,2)
>>
>> and have c be assigned 1+2 and d be assigned 1-2.  right now, I use the
>> clunky
>>
>>   x<- f(1,2
>>   c<- x[[1]]
>>   d<- x[[2]]
>>   rm(x)
>>
>> which seems awful.  is there a nicer syntax?
>>
>> regards, /iaw
>> ----
>> Ivo Welch (ivo.welch at brown.edu, ivo.welch at gmail.com)
>>
>
> I must be missing something. Why not just assign to a
> vector instead of a list?
>
>  f<- function(a,b) c(a+b,a-b)
>
> If it's imperative that f return a list, then you
> could use
>
>  (c, d) <- unlist(f(a, b))
>
> to get vector (c, d).
>
> Peter Ehlers
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list