Re: [R] list() assigning the same value to two items
One approach is to use the within function: > within(list(), {a<-1; b<-a}) $b [1] 1 $a [1] 1 Now, whether that is simpler or more compact than other options is for you (or other users) to decide. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- > project.org] On Behalf Of Thaler, Thorn, LAUSANNE, Applied Mathematics > Sent: Monday, June 21, 2010 3:30 AM > To: r-help@r-project.org > Subject: [R] list() assigning the same value to two items > > 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) > > 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@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. __ R-help@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.
Re: [R] list() assigning the same value to two items
On Mon, Jun 21, 2010 at 4:28 PM, Thaler, Thorn, LAUSANNE, Applied Mathematics wrote: > Thanks, works as expected. But maybe I've to be a bit more clear about the > reason why I'd like to have such a construct. > > In lattice you can define some parameters by passing a named list to > par.settings. Suppose I want to superpose two lines and use colors different > from the default: > > xyplot(1:10~c(1:5,1:5), type="o", groups=rep(1:2,each=5), > par.settings=list(superpose.line=list(col=1:2), > superpose.symbol=list(col=1:2))) Have you considered xyplot(1:10~c(1:5,1:5), type="o", groups=rep(1:2,each=5), par.settings= simpleTheme(col=1:2)) ? -Deepayan > I have to specify the same setting for superpose.line and superpose.symbol. > If I'd use your proposition, I've to hardcode the respective item names. It > is still an academic question, for there are thousands of ways to solve this > issue, but I was just curious whether it is possible to find an one-liner > without the need of specifying any temp variable. > > BR, Thorn [...] __ R-help@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.
Re: [R] list() assigning the same value to two items
> Try this variation of my.transform that I had posted here: > http://tolstoy.newcastle.edu.au/R/e2/help/07/09/24707.html > > List <- function(..., L = list()) { >f <- function(){} >formals(f) <- eval(substitute(as.pairlist(c(alist(...), L >body(f) <- substitute(modifyList(L, list(...))) >f() > } Wow, this does the trick. Thanks a lot. Is this function part of any package? Since I'd like to use it on a regular basis, it would be great to have it packaged somewhere. If not, I could (provided you agree) include it in a package I maintain. Thanks again. __ R-help@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.
Re: [R] list() assigning the same value to two items
On Mon, Jun 21, 2010 at 6:58 AM, Thaler, Thorn, LAUSANNE, Applied Mathematics wrote: > Thanks, works as expected. But maybe I've to be a bit more clear about the > reason why I'd like to have such a construct. > > In lattice you can define some parameters by passing a named list to > par.settings. Suppose I want to superpose two lines and use colors different > from the default: > > xyplot(1:10~c(1:5,1:5), type="o", groups=rep(1:2,each=5), > par.settings=list(superpose.line=list(col=1:2), > superpose.symbol=list(col=1:2))) > > I have to specify the same setting for superpose.line and superpose.symbol. > If I'd use your proposition, I've to hardcode the respective item names. It > is still an academic question, for there are thousands of ways to solve this > issue, but I was just curious whether it is possible to find an one-liner > without the need of specifying any temp variable. > Try this variation of my.transform that I had posted here: http://tolstoy.newcastle.edu.au/R/e2/help/07/09/24707.html List <- function(..., L = list()) { f <- function(){} formals(f) <- eval(substitute(as.pairlist(c(alist(...), L body(f) <- substitute(modifyList(L, list(...))) f() } xyplot(1:10~c(1:5,1:5), type = "o", groups = rep(1:2,each=5), par.settings = List( superpose.line = list(col = 1:2), superpose.symbol = superpose.line)) __ R-help@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.
Re: [R] list() assigning the same value to two items
Thanks, works as expected. But maybe I've to be a bit more clear about the reason why I'd like to have such a construct. In lattice you can define some parameters by passing a named list to par.settings. Suppose I want to superpose two lines and use colors different from the default: xyplot(1:10~c(1:5,1:5), type="o", groups=rep(1:2,each=5), par.settings=list(superpose.line=list(col=1:2), superpose.symbol=list(col=1:2))) I have to specify the same setting for superpose.line and superpose.symbol. If I'd use your proposition, I've to hardcode the respective item names. It is still an academic question, for there are thousands of ways to solve this issue, but I was just curious whether it is possible to find an one-liner without the need of specifying any temp variable. BR, Thorn -Original Message- From: Peter Ehlers [mailto:ehl...@ucalgary.ca] Sent: lundi 21 juin 2010 11:47 To: Thaler,Thorn,LAUSANNE,Applied Mathematics Cc: r-help@r-project.org Subject: Re: [R] list() assigning the same value to two items 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@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.
Re: [R] list() assigning the same value to two items
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@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.
[R] list() assigning the same value to two items
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) 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@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.