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)
You don't need temporary variable x c<-f(1,2)[[1]] d<-f(1,2)[[2]] Weidong Gu On Fri, Mar 30, 2012 at 6:40 PM, ivo welch <ivowel at gmail.com> 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) > > ______________________________________________ > 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.
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
On Fri, Mar 30, 2012 at 6:40 PM, ivo welch <ivowel at gmail.com> 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]]Suppose for concreteness that we want to add the first component of the result to twice the second component of the result. Also lets name the components c and d. Then here are 4 approaches: f <- function(a,b) list(c = a + b, d = a - b) # 1 x <- f(1, 2) x[[1]] + 2 * x[[2]] # 2 with( f(1, 2), c + 2 * d ) # 3 attach( f(1, 2)) c + 2*d detach() # 4 Using https://stat.ethz.ch/pipermail/r-help/2004-June/053343.html list[c, d] <- f(1, 2) c + 2 * d -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
I finally got around to posting my list of R gripes on a blog at http://ivo-welch.blogspot.com/2012/04/r-annoyances-and-gripes.html . I did put in some time, but I do not expect anyone to read it. it was more for myself. still, if you are curious, I would love to hear where I got it wrong and where I got it right. regards, /iaw ---- Ivo Welch (ivo.welch at gmail.com) On Sat, Mar 31, 2012 at 8:35 AM, ivo welch <ivo.welch at gmail.com> wrote:> "what is the problem you are trying to solve?" > > elegance, ease, and readability in my programs. > > R has morphed from a data manipulation, graphics, and stats program > into my mainstay programming language. ?most of this has been a huge > gain. ?the addition of the parallel package was another recent big > gain for me. > > some of it is a loss. ?I particularly lament the fact that I cannot > turn off [silent] recycling (causing great hair loss on my end at > various occasions), no applicable error line number output on error > messages; and some syntax that could be nicer. ? ?this was a prime > example of the latter. > > regards, /iaw > > [and then there are some impossible syntax desires, like in-string > substitution. ?would it not be nice if I could write f("use $a\n") > instead of f(paste("use", a, "\n", sep="")) ?] > > [some of my loss relates to my lack of understanding of some of the > magic behind R. ?I just stumbled onto the fact that I can obtain loess > x variables with l$x; until then, I was trying to figure out how to > construct names and paste to match what unlist told me. ? worse, I > have spotty memory. ?you guys reading and helping out people like me, > plus the google archives here, are angels. ?without your help, I could > not use R.] > > > ---- > Ivo Welch (ivo.welch at gmail.com)