Re: [R] list() assigning the same value to two items

From: Peter Ehlers <ehlers_at_ucalgary.ca>
Date: Mon, 21 Jun 2010 03:47:05 -0600

On 2010-06-21 3:30, Thaler, Thorn, LAUSANNE, Applied Mathematics wrote:
> Hi everybody,
>
> I'd like to have a list with two elements, where both elements have the same value:
>
> z<- list(a=1, b=1)
>
> If it happens, that I've to change the value, I've to assure that I change both. This is error prone. Hence, a better way to achieve this is to define:
>
> tmp<- 1
> z<- list(a=tmp, b=tmp)
>
> Now, I'm wondering if it is possible to make this more compact:
> z<- list(a=tmp<-1, b=tmp)
>
> Both approaches have the side effect that a variable "tmp" is created. So is there a way to achieve the same thing without having to define an additional variable? Something like
>
> z<- list(a=1, b=a)
>
> or even
>
> z<- list(a=b=1)?
>
> Both commands don't work of course, but I'm just curious whether this is possible in principle (a rather academic question I've to admit)
>

  f <- function(x){ list(a=x, b=x) }
  z <- f(1)
etc.

   -Peter Ehlers

> Thanks + BR,
>
> Thorn Thaler
> Applied Mathematics Group
> Nestlé Research Center
> PO Box 44
> CH-1000 Lausanne 26, Switzerland
> Phone: + 41 21 785 8220
> Fax: + 41 21 785 8556
>



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. Received on Mon 21 Jun 2010 - 09:50:30 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 21 Jun 2010 - 11:10:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive