Re: [R] return named list from foreach

2015-02-20 Thread Jeff Newmiller
You cannot do that in one step. Do it right after:

names(out) - df$nm

Please don't post using HTML format.. it scrambles code, and since we cannot 
see what you saw it doesn't help in any way.

Also note that df is a function in the base stats package... not a good name 
to use.

---
Jeff NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  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.

On February 20, 2015 7:44:41 AM PST, Alexander Shenkin ashen...@ufl.edu wrote:
Hello all,

I've been trying to figure out how to return a named list from foreach.
 
Given that the order of the returned list is guaranteed to be in the 
order in which the object is passed to foreach, list members can be 
named afterwards.  However, I'm wondering if there's a better way to do

it, perhaps with some sort of combine function?

library(doParallel)
library(foreach)

cl - makeCluster(4)
registerDoParallel(cl)

df = data.frame(nm = letters[11:20], a = 1:10, b=11:20)

out = foreach(i=1:nrow(df)) %dopar% {
 a = list(j = sqrt(df[i,]$a), k = sqrt(df[i,]$b))
 a
}

How do I name the elements of out using the corresponding values
df$nm?

thanks,
allie

   [[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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 -- To UNSUBSCRIBE and more, see
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] return named list from foreach

2015-02-20 Thread Alexander Shenkin
Hello all,

I've been trying to figure out how to return a named list from foreach.  
Given that the order of the returned list is guaranteed to be in the 
order in which the object is passed to foreach, list members can be 
named afterwards.  However, I'm wondering if there's a better way to do 
it, perhaps with some sort of combine function?

library(doParallel)
library(foreach)

cl - makeCluster(4)
registerDoParallel(cl)

df = data.frame(nm = letters[11:20], a = 1:10, b=11:20)

out = foreach(i=1:nrow(df)) %dopar% {
 a = list(j = sqrt(df[i,]$a), k = sqrt(df[i,]$b))
 a
}

How do I name the elements of out using the corresponding values df$nm?

thanks,
allie

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Return invisible list

2011-07-08 Thread S Ellison
 -Original Message-
 [mailto:r-help-boun...@r-project.org] On Behalf Of francisco.ahued
 Subject: [R] Return invisible list
 
 x=read.table(data.txt,header=T)
 goa=(x[,3])
 meplot(goa)
 
 I can see the plot, but I would like to see the values of the 
 x and y axis. 
 

Something returned as invisible is returned but not printed. 
You just need to catch it in a suitable variable. That's what you do with any 
other function where you want to keep the value: 

 x=read.table(data.txt,header=T)
 goa=(x[,3])
mplist -  meplot(goa) #assigns the output to a variable called mplist

mplist  #calls the default print method for that object.

If you don't need to keep the values for re-use, putting something in 
parentheses evaluates the expression and returns the result, effectively 
removing the invisibility cloak without explicit assignment.

Example

z-rnorm(100)
hist( z ) #returns invisible list

but

( hist( z ) ) #additionally displays the output.

S Ellison***
This email and any attachments are confidential. Any use...{{dropped:8}}

__
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] Return invisible list

2011-07-07 Thread francisco.ahued
Hi, I'm new to R. I'm trying to do some extreme value theory analysis,
looking at the Mean Excess Plot of a series. These are the commands I type
to get the plot:

x=read.table(data.txt,header=T)
goa=(x[,3])
meplot(goa)

I can see the plot, but I would like to see the values of the x and y axis. 

According to this page
(http://rss.acs.unt.edu/Rdoc/library/VGAM/html/meplot.html), A list is
returned invisibly with the following components. 

threshold  The x axis values.  
meanExcess  The y axis values. Each value is a sample mean minus a value u.  



So my question is, how can I see (or even export) threshold and meanExcess?

Thanks,

F

--
View this message in context: 
http://r.789695.n4.nabble.com/Return-invisible-list-tp3652323p3652323.html
Sent from the R help mailing list archive at Nabble.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] Return invisible list

2011-07-07 Thread David Winsemius


On Jul 7, 2011, at 3:00 PM, francisco.ahued wrote:


Hi, I'm new to R. I'm trying to do some extreme value theory analysis,
looking at the Mean Excess Plot of a series. These are the commands  
I type

to get the plot:

x=read.table(data.txt,header=T)
goa=(x[,3])
meplot(goa)

I can see the plot, but I would like to see the values of the x and  
y axis.


According to this page
(http://rss.acs.unt.edu/Rdoc/library/VGAM/html/meplot.html), A list  
is

returned invisibly with the following components.


If a function returns something invisibly, it means that you need to  
assign the result to a named object, which can then be inspected.




threshold  The x axis values.
meanExcess  The y axis values. Each value is a sample mean minus a  
value u.




So my question is, how can I see (or even export) threshold and  
meanExcess?



--
David Winsemius, MD
West Hartford, CT

__
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] Return invisible list

2011-07-07 Thread savage.arrow
Thanks! How do I do that?

--
View this message in context: 
http://r.789695.n4.nabble.com/Return-invisible-list-tp3652323p3652396.html
Sent from the R help mailing list archive at Nabble.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] Return invisible list

2011-07-07 Thread David Winsemius


On Jul 7, 2011, at 3:41 PM, savage.arrow wrote:

Adding back context from earlier posting (WHICH IS YOUR DUTY  
savage.arrow)

--

x=read.table(data.txt,header=T)
goa=(x[,3])
meplot(goa)

I can see the plot, but I would like to see the values of the x and y  
axis.


According to this page
(http://rss.acs.unt.edu/Rdoc/library/VGAM/html/meplot.html), A list is
returned invisibly with the following components.

So my question is, how can I see (or even export) threshold and  
meanExcess?

---

Thanks! How do I do that?


?-

# And this also means you need to pick up your copy of Introoduction  
to R and read the Posting Guide where the request to include context  
is made and the justification for the request is offered.




--
View this message in context: 
http://r.789695.n4.nabble.com/Return-invisible-list-tp3652323p3652396.html
Sent from the R help mailing list archive at Nabble.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.


David Winsemius, MD
West Hartford, CT

__
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] Return a list

2008-09-26 Thread Stefan Fritsch
Dear R Users,

another problem for me is the output of a function.

I have several output variables which I give back with the list command.

test - function{return(list(a,b,c,d,e,f,g,...))}

After the usage of the function I want to assign the variables to the output 
variables.

result - test()

a - result$a
b - result$b
c - result$c
d - result$d
...

is there a more elegant way to assign these variables, without writing them all 
down?

thank you very much for your help!

Stefan Fritsch

__
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] Return a list

2008-09-26 Thread baptiste auguie

?attach
or ?with

Hope this helps,

baptiste


On 26 Sep 2008, at 14:57, Stefan Fritsch wrote:


Dear R Users,

another problem for me is the output of a function.

I have several output variables which I give back with the list  
command.


test - function {return(list(a,b,c,d,e,f,g,...))}

After the usage of the function I want to assign the variables to  
the output variables.


result - test()

a - result$a
b - result$b
c - result$c
d - result$d
...

is there a more elegant way to assign these variables, without  
writing them all down?


thank you very much for your help!

Stefan Fritsch

__
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.


_

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag

__
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] Return a list

2008-09-26 Thread Henrique Dallazuanna
Try this:

sapply(names(result), function(nm)assign(nm, result[[nm]], envir = globalenv()))


On Fri, Sep 26, 2008 at 10:57 AM, Stefan Fritsch
[EMAIL PROTECTED] wrote:
 Dear R Users,

 another problem for me is the output of a function.

 I have several output variables which I give back with the list command.

 test - function{return(list(a,b,c,d,e,f,g,...))}

 After the usage of the function I want to assign the variables to the output 
 variables.

 result - test()

 a - result$a
 b - result$b
 c - result$c
 d - result$d
 ...

 is there a more elegant way to assign these variables, without writing them 
 all down?

 thank you very much for your help!

 Stefan Fritsch

 __
 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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

__
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] Return a list

2008-09-26 Thread Mike Prager
Stefan Fritsch [EMAIL PROTECTED] wrote:

 I have several output variables which I give back with the list command.
 
 test - function  {return(list(a,b,c,d,e,f,g,...))}
 
 After the usage of the function I want to assign the variables to the output 
 variables.
 
 result - test()
 
 a - result$a
 b - result$b
 c - result$c
 d - result$d
 ...
 
 is there a more elegant way to assign these variables, without writing them 
 all down?
 
 thank you very much for your help!

I don't have a good answer for your question, but I do encourage
you to choose a method that will be readily intelligible to you
when you revisit your code X years later.  

-- 
Mike Prager, NOAA, Beaufort, NC
* Opinions expressed are personal and not represented otherwise.
* Any use of tradenames does not constitute a NOAA endorsement.

__
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] Return a list

2008-09-26 Thread Wacek Kusnierczyk
Mike Prager wrote:
 Stefan Fritsch [EMAIL PROTECTED] wrote:

   
 I have several output variables which I give back with the list command.

 test - function {return(list(a,b,c,d,e,f,g,...))}

 After the usage of the function I want to assign the variables to the output 
 variables.

 result - test()

 a - result$a
 b - result$b
 c - result$c
 d - result$d
 ...

 is there a more elegant way to assign these variables, without writing them 
 all down?

 

arguably ugly and risky, but simple:

for (name in names(result)) assign(name, result[[name]])

(note, for this to work you actually need to name the components of the
returned list: return(list(a=a,b=b,...)))

vQ

__
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] Return a list

2008-09-26 Thread Bert Gunter
But why do this? Just leave the (preferably named) variables as list
components and work with them there.

1. ?comment tells you how to add a comment attribute to the list for self
documentation (what were the components? how are they related? etc.)

2. ?with  shows you how to access the components of the list individually in
the usual way.

3. ?lapply and friends shows you how to loop over list components .

etc.

Cheers,
Bert

Bert Gunter 
Genentech  

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Wacek Kusnierczyk
Sent: Friday, September 26, 2008 12:40 PM
To: Mike Prager
Cc: R help
Subject: Re: [R] Return a list

Mike Prager wrote:
 Stefan Fritsch [EMAIL PROTECTED] wrote:

   
 I have several output variables which I give back with the list command.

 test - function {return(list(a,b,c,d,e,f,g,...))}

 After the usage of the function I want to assign the variables to the
output variables.

 result - test()

 a - result$a
 b - result$b
 c - result$c
 d - result$d
 ...

 is there a more elegant way to assign these variables, without writing
them all down?

 

arguably ugly and risky, but simple:

for (name in names(result)) assign(name, result[[name]])

(note, for this to work you actually need to name the components of the
returned list: return(list(a=a,b=b,...)))

vQ

__
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] Return a list

2008-09-26 Thread N. Lapidus
The answers that were previously given allow you to easily extract results
from your returned list, but if I understand well, this list is created only
because you cannot return several arguments whereas you need to keep the
values of a, b, c, etc. Am I right?
Another solution would be to directly send the values you want to keep
into the environment where they are needed. The following example supposes
you need to keep a only in the upper environment from which your function
was launched, and b in another one (e.g. .GlobalEnv).
Hope this may help.
Nael

 # Here is a function such as yours:
 test - function(){
+ a - 1
+ b - 2
+ return(list(a=a, b=b, c=c))
+ }

 result - test()
 (a - result$a)
[1] 1
 (b - result$b)
[1] 2

 rm(a, b)

 # Now our variables will be automatically assigned into the chosen
environment
 test2 - function(){
+ a - 1
+ b - 2
+ assign(a, a, envir=parent.frame(n=1))
+ assign(b, b, envir=.GlobalEnv)
+ return(NULL)
+ }

 # Suppose test2 is launched by another function
 test2.launcher - function() {
+ test2()
+ print(paste(a exists inside test2.launcher:, exists(a)))
+ print(paste(b exists inside test2.launcher:, exists(b)))
+ return (NULL)
+ }

 test2.launcher()
[1] a exists inside test2.launcher: TRUE
[1] b exists inside test2.launcher: TRUE
NULL
 exists(a)# a still exists in the upper environment
[1] FALSE
 exists(b)# b does not
[1] TRUE




On Fri, Sep 26, 2008 at 9:39 PM, Wacek Kusnierczyk 
[EMAIL PROTECTED] wrote:

 Mike Prager wrote:
  Stefan Fritsch [EMAIL PROTECTED] wrote:
 
 
  I have several output variables which I give back with the list command.
 
  test - function {return(list(a,b,c,d,e,f,g,...))}
 
  After the usage of the function I want to assign the variables to the
 output variables.
 
  result - test()
 
  a - result$a
  b - result$b
  c - result$c
  d - result$d
  ...
 
  is there a more elegant way to assign these variables, without writing
 them all down?
 
 

 arguably ugly and risky, but simple:

 for (name in names(result)) assign(name, result[[name]])

 (note, for this to work you actually need to name the components of the
 returned list: return(list(a=a,b=b,...)))

 vQ

 __
 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.


[[alternative HTML version deleted]]

__
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] Return a list

2008-09-26 Thread Wacek Kusnierczyk
Bert Gunter wrote:
 But why do this? Just leave the (preferably named) variables as list
 components and work with them there.

 1. ?comment tells you how to add a comment attribute to the list for self
 documentation (what were the components? how are they related? etc.)

 2. ?with  shows you how to access the components of the list individually in
 the usual way.

 3. ?lapply and friends shows you how to loop over list components .

 etc.

 Cheers,
 Bert

 Bert Gunter 
 Genentech  

   
it depends on what the original author wanted.

with constructs a new environment, and all assignments, if any, made in
the expression evaluated within with are invisible to the outside
(unless one plays with environments, again):

x = 1:10
a = 3
with(test(), { x[1:3] = c(a,b,c); x = x+d; a = a + 1 })
x # still 1:10, whatever test returns
a # still 3. whatever test returns

if the author wanted the values included in the list to be visible and
accessible by simple names, and used in assignments in a larger part of
code, using with might be inconvenient.
it does not mean that my solution below is a good one, it's just a quick
fix.  i wouldn't do that in myself, it's badly non-functional ;)

vQ


   
 
 I have several output variables which I give back with the list command.

 test - function{return(list(a,b,c,d,e,f,g,...))}

 After the usage of the function I want to assign the variables to the
   
 output variables.
   
 result - test()

 a - result$a
 b - result$b
 c - result$c
 d - result$d
 ...

 is there a more elegant way to assign these variables, without writing
   
 them all down?
   
 
   

 arguably ugly and risky, but simple:

 for (name in names(result)) assign(name, result[[name]])

 (note, for this to work you actually need to name the components of the
 returned list: return(list(a=a,b=b,...)))

 vQ

 __
 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.
   


-- 
---
Wacek Kusnierczyk, MD PhD

Email: [EMAIL PROTECTED]
Phone: +47 73591875, +47 72574609

Department of Computer and Information Science (IDI)
Faculty of Information Technology, Mathematics and Electrical Engineering (IME)
Norwegian University of Science and Technology (NTNU)
Sem Saelands vei 7, 7491 Trondheim, Norway
Room itv303

Bioinformatics  Gene Regulation Group
Department of Cancer Research and Molecular Medicine (IKM)
Faculty of Medicine (DMF)
Norwegian University of Science and Technology (NTNU)
Laboratory Center, Erling Skjalgsons gt. 1, 7030 Trondheim, Norway
Room 231.05.060

__
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] Return a list

2008-09-26 Thread hadley wickham
 it depends on what the original author wanted.

 with constructs a new environment, and all assignments, if any, made in
 the expression evaluated within with are invisible to the outside
 (unless one plays with environments, again):

 x = 1:10
 a = 3
 with(test(), { x[1:3] = c(a,b,c); x = x+d; a = a + 1 })
 x # still 1:10, whatever test returns
 a # still 3. whatever test returns

 if the author wanted the values included in the list to be visible and
 accessible by simple names, and used in assignments in a larger part of
 code, using with might be inconvenient.

What about within ?

Hadley

-- 
http://had.co.nz/

__
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.