Re: [R] list assignment syntax?

2012-04-03 Thread Bert Gunter
Ivo:

The primary source of your problems appears to be:

"Unfortunately, for a generic programming language, R still lacks some sugar."

R is **not** a generic programming language, nor was it ever meant to
be one. Have you read Chambers's et al books? R was designed as a
language for data analysis and graphics, not as a general programming
language.

This is not to say that some of your gripes/suggestions may not be
useful. I did not go through them in any detail, but a couple of them
at least seemed related to your misunderstanding above.

Cheers,
Bert

On Tue, Apr 3, 2012 at 4:29 PM, ivo welch  wrote:
> 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.we...@gmail.com)
>
>
> On Sat, Mar 31, 2012 at 8:35 AM, ivo welch  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.we...@gmail.com)
>
> __
> 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
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 assignment syntax?

2012-04-03 Thread ivo welch
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.we...@gmail.com)


On Sat, Mar 31, 2012 at 8:35 AM, ivo welch  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.we...@gmail.com)

__
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 assignment syntax?

2012-03-31 Thread ivo welch
"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.we...@gmail.com)

__
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 assignment syntax?

2012-03-30 Thread jim holtman
All of this is syntactic sugar.  Just because a language does not have
the sintax that you are used (e.g, Perl) does not make it bad/hard to
use.  What is the problem if I have to type a couple of extra lines:

f <- function(x) list(a = seq_along(x), b=x*3)
result <- f(1)
r1 <- result$a
r2 <- result$b

If you don't want to pay the "keystrokes", then use some macros
(EMACS, hotKeys, etc.) to encapsulate that in a single keystroke.

As I say in my tag line:  "what is the problem you are trying to solve".

On Sat, Mar 31, 2012 at 1:56 AM, Jeff Newmiller
 wrote:
> I agree that global side effects are a bad idea, but ivo started this by 
> pointing out that it is straightforward to do this in Perl. It might be worth 
> considering adding this capability to R.
> ---
> Jeff Newmiller                        The     .       .  Go Live...
> DCN:        Basics: ##.#.       ##.#.  Live Go...
>                                      Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
> ---
> Sent from my phone. Please excuse my brevity.
>
> Bert Gunter  wrote:
>
>>,,,But assigning to the global environment is a bad idea. You're just
>>asking for trouble -- overwriting without warning something that's
>>already there.
>>
>>May I suggest a rule of thumb: When things are difficult or clumsy to
>>do in R, don't do them.
>>
>>Of course this is not inviolable, but the OP's request may be one
>>instance where it applies.
>>
>>-- Bert
>>
>>On Fri, Mar 30, 2012 at 7:24 PM, Joshua Wiley 
>>wrote:
>>> An idiom like this would also work.
>>>
>>> f <- function(a,b) list( data.frame(a+rnorm(20), b), loess( a ~ b) )
>>> lapply(seq_along(out <- f(1:20, 1:20)), function(i) assign(c("c",
>>> "d")[i], out[[i]], envir = .GlobalEnv))
>>>
>>>  It is not elegant if you are doing this regularly, but, I think,
>>> functions typically return lists of output rather than assigning to
>>> the global environment because presumably one function returns
>>related
>>> objects so it makes sense for them to stay grouped in a list.  If the
>>> two steps really are unrelated, then use two functions.
>>>
>>> It seems like what you want is to have a function return each object
>>> not in a list but to the global environment, but you would like to be
>>> able to give them your own names (which makes sense).  If this is
>>with
>>> your own functions, you could adapt them easily to accept names and
>>> then assign to global.
>>>
>>> f <- function(a,b, names = c("a", "b")) {
>>>  assign(names[1], data.frame(a + rnorm(20), b), envir = .GlobalEnv)
>>>  assign(names[2], loess(a ~ b), envir = .GlobalEnv)
>>> }
>>> f(1:20, 1:20, c("c", "d"))
>>>
>>> I'm not arguing against Gabor's list function, just tossing out a few
>>> other ideas.  I am not familiar with perl, but in R I am used to the
>>> thing on the left being what is assigned to; the list[a, b] assigning
>>> to global is counterintuitive to me personally.
>>>
>>> Given that this works:
>>>
>>> .GlobalEnv[["a"]] <- 1
>>> a
>>>
>>> I was hoping that:
>>>
>>> .GlobalEnv[c("c", "d")] <- f(1:20, 1:20)
>>>
>>> would also, but alas environments are not subsettable.  Interesting
>>question.
>>>
>>> Cheers,
>>>
>>> Josh
>>>
>>> On Fri, Mar 30, 2012 at 6:26 PM, ivo welch 
>>wrote:
 thanks, everyone.  I should have been clearer (as always).  I used
>>the
 numbers as an example only.  I am aware that I can put numbers into
 vectors and get nice R syntax.  my problem is that I usually want to
 return multiple and/or mixed objects, such as multiple data frames.
>> I
 should have given as an example something like

   f <- function(a,b) list( data.frame(a+rnorm(20), b), loess( a ~ b)
>>)

 weidong---yes, nice syntax, but I don't want to invoke f() twice.

 peter---I don't think your unlist syntax works.  I tried it.

 gabor---#1-#3 work, but aren't what I really want.  #4 is exactly
>>what
 I wanted.  can list[] be added into the standard core R as a
>>feature?
 it would seem like a natural part of the syntax for functions
 returning multiple values.

 justin---mea culpa.

 regards,

 /iaw
 
 Ivo Welch (ivo.we...@gmail.com)



 On Fri, Mar 30, 2012 at 5:08 PM, Justin Haynes 
>>wrote:
> 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 
>>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

Re: [R] list assignment syntax?

2012-03-30 Thread Jeff Newmiller
I agree that global side effects are a bad idea, but ivo started this by 
pointing out that it is straightforward to do this in Perl. It might be worth 
considering adding this capability to R.
---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

Bert Gunter  wrote:

>,,,But assigning to the global environment is a bad idea. You're just
>asking for trouble -- overwriting without warning something that's
>already there.
>
>May I suggest a rule of thumb: When things are difficult or clumsy to
>do in R, don't do them.
>
>Of course this is not inviolable, but the OP's request may be one
>instance where it applies.
>
>-- Bert
>
>On Fri, Mar 30, 2012 at 7:24 PM, Joshua Wiley 
>wrote:
>> An idiom like this would also work.
>>
>> f <- function(a,b) list( data.frame(a+rnorm(20), b), loess( a ~ b) )
>> lapply(seq_along(out <- f(1:20, 1:20)), function(i) assign(c("c",
>> "d")[i], out[[i]], envir = .GlobalEnv))
>>
>>  It is not elegant if you are doing this regularly, but, I think,
>> functions typically return lists of output rather than assigning to
>> the global environment because presumably one function returns
>related
>> objects so it makes sense for them to stay grouped in a list.  If the
>> two steps really are unrelated, then use two functions.
>>
>> It seems like what you want is to have a function return each object
>> not in a list but to the global environment, but you would like to be
>> able to give them your own names (which makes sense).  If this is
>with
>> your own functions, you could adapt them easily to accept names and
>> then assign to global.
>>
>> f <- function(a,b, names = c("a", "b")) {
>>  assign(names[1], data.frame(a + rnorm(20), b), envir = .GlobalEnv)
>>  assign(names[2], loess(a ~ b), envir = .GlobalEnv)
>> }
>> f(1:20, 1:20, c("c", "d"))
>>
>> I'm not arguing against Gabor's list function, just tossing out a few
>> other ideas.  I am not familiar with perl, but in R I am used to the
>> thing on the left being what is assigned to; the list[a, b] assigning
>> to global is counterintuitive to me personally.
>>
>> Given that this works:
>>
>> .GlobalEnv[["a"]] <- 1
>> a
>>
>> I was hoping that:
>>
>> .GlobalEnv[c("c", "d")] <- f(1:20, 1:20)
>>
>> would also, but alas environments are not subsettable.  Interesting
>question.
>>
>> Cheers,
>>
>> Josh
>>
>> On Fri, Mar 30, 2012 at 6:26 PM, ivo welch 
>wrote:
>>> thanks, everyone.  I should have been clearer (as always).  I used
>the
>>> numbers as an example only.  I am aware that I can put numbers into
>>> vectors and get nice R syntax.  my problem is that I usually want to
>>> return multiple and/or mixed objects, such as multiple data frames.
> I
>>> should have given as an example something like
>>>
>>>   f <- function(a,b) list( data.frame(a+rnorm(20), b), loess( a ~ b)
>)
>>>
>>> weidong---yes, nice syntax, but I don't want to invoke f() twice.
>>>
>>> peter---I don't think your unlist syntax works.  I tried it.
>>>
>>> gabor---#1-#3 work, but aren't what I really want.  #4 is exactly
>what
>>> I wanted.  can list[] be added into the standard core R as a
>feature?
>>> it would seem like a natural part of the syntax for functions
>>> returning multiple values.
>>>
>>> justin---mea culpa.
>>>
>>> regards,
>>>
>>> /iaw
>>> 
>>> Ivo Welch (ivo.we...@gmail.com)
>>>
>>>
>>>
>>> On Fri, Mar 30, 2012 at 5:08 PM, Justin Haynes 
>wrote:
 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 
>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.we...@brown.edu, ivo.we...@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

Re: [R] list assignment syntax?

2012-03-30 Thread Bert Gunter
,,,But assigning to the global environment is a bad idea. You're just
asking for trouble -- overwriting without warning something that's
already there.

May I suggest a rule of thumb: When things are difficult or clumsy to
do in R, don't do them.

Of course this is not inviolable, but the OP's request may be one
instance where it applies.

-- Bert

On Fri, Mar 30, 2012 at 7:24 PM, Joshua Wiley  wrote:
> An idiom like this would also work.
>
> f <- function(a,b) list( data.frame(a+rnorm(20), b), loess( a ~ b) )
> lapply(seq_along(out <- f(1:20, 1:20)), function(i) assign(c("c",
> "d")[i], out[[i]], envir = .GlobalEnv))
>
>  It is not elegant if you are doing this regularly, but, I think,
> functions typically return lists of output rather than assigning to
> the global environment because presumably one function returns related
> objects so it makes sense for them to stay grouped in a list.  If the
> two steps really are unrelated, then use two functions.
>
> It seems like what you want is to have a function return each object
> not in a list but to the global environment, but you would like to be
> able to give them your own names (which makes sense).  If this is with
> your own functions, you could adapt them easily to accept names and
> then assign to global.
>
> f <- function(a,b, names = c("a", "b")) {
>  assign(names[1], data.frame(a + rnorm(20), b), envir = .GlobalEnv)
>  assign(names[2], loess(a ~ b), envir = .GlobalEnv)
> }
> f(1:20, 1:20, c("c", "d"))
>
> I'm not arguing against Gabor's list function, just tossing out a few
> other ideas.  I am not familiar with perl, but in R I am used to the
> thing on the left being what is assigned to; the list[a, b] assigning
> to global is counterintuitive to me personally.
>
> Given that this works:
>
> .GlobalEnv[["a"]] <- 1
> a
>
> I was hoping that:
>
> .GlobalEnv[c("c", "d")] <- f(1:20, 1:20)
>
> would also, but alas environments are not subsettable.  Interesting question.
>
> Cheers,
>
> Josh
>
> On Fri, Mar 30, 2012 at 6:26 PM, ivo welch  wrote:
>> thanks, everyone.  I should have been clearer (as always).  I used the
>> numbers as an example only.  I am aware that I can put numbers into
>> vectors and get nice R syntax.  my problem is that I usually want to
>> return multiple and/or mixed objects, such as multiple data frames.  I
>> should have given as an example something like
>>
>>   f <- function(a,b) list( data.frame(a+rnorm(20), b), loess( a ~ b) )
>>
>> weidong---yes, nice syntax, but I don't want to invoke f() twice.
>>
>> peter---I don't think your unlist syntax works.  I tried it.
>>
>> gabor---#1-#3 work, but aren't what I really want.  #4 is exactly what
>> I wanted.  can list[] be added into the standard core R as a feature?
>> it would seem like a natural part of the syntax for functions
>> returning multiple values.
>>
>> justin---mea culpa.
>>
>> regards,
>>
>> /iaw
>> 
>> Ivo Welch (ivo.we...@gmail.com)
>>
>>
>>
>> On Fri, Mar 30, 2012 at 5:08 PM, Justin Haynes  wrote:
>>> 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  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.we...@brown.edu, ivo.we...@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@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.
>
>
>
> --
> Joshua Wiley
> Ph.D. Student, Health Psychology
> Programmer Analyst II, Statistical Consulting Group
> University of California, Los Angeles
> https://joshuawiley.com/
>
> __
> R-help@r-project.org mailin

Re: [R] list assignment syntax?

2012-03-30 Thread Joshua Wiley
An idiom like this would also work.

f <- function(a,b) list( data.frame(a+rnorm(20), b), loess( a ~ b) )
lapply(seq_along(out <- f(1:20, 1:20)), function(i) assign(c("c",
"d")[i], out[[i]], envir = .GlobalEnv))

 It is not elegant if you are doing this regularly, but, I think,
functions typically return lists of output rather than assigning to
the global environment because presumably one function returns related
objects so it makes sense for them to stay grouped in a list.  If the
two steps really are unrelated, then use two functions.

It seems like what you want is to have a function return each object
not in a list but to the global environment, but you would like to be
able to give them your own names (which makes sense).  If this is with
your own functions, you could adapt them easily to accept names and
then assign to global.

f <- function(a,b, names = c("a", "b")) {
  assign(names[1], data.frame(a + rnorm(20), b), envir = .GlobalEnv)
  assign(names[2], loess(a ~ b), envir = .GlobalEnv)
}
f(1:20, 1:20, c("c", "d"))

I'm not arguing against Gabor's list function, just tossing out a few
other ideas.  I am not familiar with perl, but in R I am used to the
thing on the left being what is assigned to; the list[a, b] assigning
to global is counterintuitive to me personally.

Given that this works:

.GlobalEnv[["a"]] <- 1
a

I was hoping that:

.GlobalEnv[c("c", "d")] <- f(1:20, 1:20)

would also, but alas environments are not subsettable.  Interesting question.

Cheers,

Josh

On Fri, Mar 30, 2012 at 6:26 PM, ivo welch  wrote:
> thanks, everyone.  I should have been clearer (as always).  I used the
> numbers as an example only.  I am aware that I can put numbers into
> vectors and get nice R syntax.  my problem is that I usually want to
> return multiple and/or mixed objects, such as multiple data frames.  I
> should have given as an example something like
>
>   f <- function(a,b) list( data.frame(a+rnorm(20), b), loess( a ~ b) )
>
> weidong---yes, nice syntax, but I don't want to invoke f() twice.
>
> peter---I don't think your unlist syntax works.  I tried it.
>
> gabor---#1-#3 work, but aren't what I really want.  #4 is exactly what
> I wanted.  can list[] be added into the standard core R as a feature?
> it would seem like a natural part of the syntax for functions
> returning multiple values.
>
> justin---mea culpa.
>
> regards,
>
> /iaw
> 
> Ivo Welch (ivo.we...@gmail.com)
>
>
>
> On Fri, Mar 30, 2012 at 5:08 PM, Justin Haynes  wrote:
>> 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  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.we...@brown.edu, ivo.we...@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@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.



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/

__
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 assignment syntax?

2012-03-30 Thread ivo welch
thanks, everyone.  I should have been clearer (as always).  I used the
numbers as an example only.  I am aware that I can put numbers into
vectors and get nice R syntax.  my problem is that I usually want to
return multiple and/or mixed objects, such as multiple data frames.  I
should have given as an example something like

   f <- function(a,b) list( data.frame(a+rnorm(20), b), loess( a ~ b) )

weidong---yes, nice syntax, but I don't want to invoke f() twice.

peter---I don't think your unlist syntax works.  I tried it.

gabor---#1-#3 work, but aren't what I really want.  #4 is exactly what
I wanted.  can list[] be added into the standard core R as a feature?
it would seem like a natural part of the syntax for functions
returning multiple values.

justin---mea culpa.

regards,

/iaw

Ivo Welch (ivo.we...@gmail.com)



On Fri, Mar 30, 2012 at 5:08 PM, Justin Haynes  wrote:
> 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  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.we...@brown.edu, ivo.we...@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@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 assignment syntax?

2012-03-30 Thread Justin Haynes
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  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.we...@brown.edu, ivo.we...@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@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 assignment syntax?

2012-03-30 Thread Gabor Grothendieck
On Fri, Mar 30, 2012 at 6:40 PM, 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]]

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

__
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 assignment syntax?

2012-03-30 Thread Peter Ehlers

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.we...@brown.edu, ivo.we...@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@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 assignment syntax?

2012-03-30 Thread Weidong Gu
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  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.we...@brown.edu, ivo.we...@gmail.com)
>
> __
> 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.