I'd like to pass a list object created by one function as an argument of
another function. once inside the second function, I'd like to break the
list up to it's individual elements, each then identifiable by the 'names'
of the list.
The list looks something like
lst<-list(a=1, b=2, df=5, g=7)
then inside the function I've been writing a sequence of statements that
extract the objects within lst like
do_something<-function(L){
a<-lst$a
b<-lst$b
df<-lst$df
g<-lst$g
a+b+df+g
}
do_something(lst)
My question is, is it possible to avoid the above sequence of statements,
and achieve the same thing with a single line of code? Perhaps this would
be bad programming practise, as you can't 'see' where objects in the
function are coming from?
Thanks,
alastair
--
View this message in context: http://r.789695.n4.nabble.com/Passing-lists-between-functions-tp4650792.html
Sent from the R help mailing list archive at Nabble.com.
Passing lists between functions
7 messages · Ally, arun, jim holtman +1 more
Hi,
If the end result you want is the sum of those elements, will this work for you?
?sum(unlist(lst))
#[1] 15
#or
sum(do.call(c,lst)) #in this case
#[1] 15
A.K.
----- Original Message -----
From: Ally <a.rushworth at stats.gla.ac.uk>
To: r-help at r-project.org
Cc:
Sent: Sunday, November 25, 2012 8:24 PM
Subject: [R] Passing lists between functions
I'd like to pass a list object created by one function as an argument of
another function.? once inside the second function, I'd like to break the
list up to it's individual elements, each then identifiable by the 'names'
of the list.?
The list looks something like
lst<-list(a=1, b=2, df=5, g=7)
then inside the function I've been writing a sequence of statements that
extract the objects within lst like
do_something<-function(L){
a<-lst$a
b<-lst$b
df<-lst$df
g<-lst$g
a+b+df+g
}
do_something(lst)
My question is, is it possible to avoid the above sequence of statements,
and achieve the same thing with a single line of code?? Perhaps this would
be bad programming practise, as you can't 'see' where objects in the
function are coming from?
Thanks,
alastair
--
View this message in context: http://r.789695.n4.nabble.com/Passing-lists-between-functions-tp4650792.html
Sent from the R help mailing list archive at Nabble.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.
Just reference the objects in the list:
do_something<-function(L){
lst$a+lst$b+lst$df+lst$g
}
do_something(lst)
On Sun, Nov 25, 2012 at 8:24 PM, Ally <a.rushworth at stats.gla.ac.uk> wrote:
I'd like to pass a list object created by one function as an argument of
another function. once inside the second function, I'd like to break the
list up to it's individual elements, each then identifiable by the 'names'
of the list.
The list looks something like
lst<-list(a=1, b=2, df=5, g=7)
then inside the function I've been writing a sequence of statements that
extract the objects within lst like
do_something<-function(L){
a<-lst$a
b<-lst$b
df<-lst$df
g<-lst$g
a+b+df+g
}
do_something(lst)
My question is, is it possible to avoid the above sequence of statements,
and achieve the same thing with a single line of code? Perhaps this would
be bad programming practise, as you can't 'see' where objects in the
function are coming from?
Thanks,
alastair
--
View this message in context: http://r.789695.n4.nabble.com/Passing-lists-between-functions-tp4650792.html
Sent from the R help mailing list archive at Nabble.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.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
Wrong -- I forgot to use the parameter name
do_something<-function(L){
L$a+L$b+L$df+L$g
}
do_something(lst)
On Sun, Nov 25, 2012 at 9:02 PM, jim holtman <jholtman at gmail.com> wrote:
Just reference the objects in the list:
do_something<-function(L){
lst$a+lst$b+lst$df+lst$g
}
do_something(lst)
On Sun, Nov 25, 2012 at 8:24 PM, Ally <a.rushworth at stats.gla.ac.uk> wrote:
I'd like to pass a list object created by one function as an argument of
another function. once inside the second function, I'd like to break the
list up to it's individual elements, each then identifiable by the 'names'
of the list.
The list looks something like
lst<-list(a=1, b=2, df=5, g=7)
then inside the function I've been writing a sequence of statements that
extract the objects within lst like
do_something<-function(L){
a<-lst$a
b<-lst$b
df<-lst$df
g<-lst$g
a+b+df+g
}
do_something(lst)
My question is, is it possible to avoid the above sequence of statements,
and achieve the same thing with a single line of code? Perhaps this would
be bad programming practise, as you can't 'see' where objects in the
function are coming from?
Thanks,
alastair
--
View this message in context: http://r.789695.n4.nabble.com/Passing-lists-between-functions-tp4650792.html
Sent from the R help mailing list archive at Nabble.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.
-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
On 26/11/12 14:24, Ally wrote:
I'd like to pass a list object created by one function as an argument of
another function. once inside the second function, I'd like to break the
list up to it's individual elements, each then identifiable by the 'names'
of the list.
The list looks something like
lst<-list(a=1, b=2, df=5, g=7)
then inside the function I've been writing a sequence of statements that
extract the objects within lst like
do_something<-function(L){
a<-lst$a
b<-lst$b
df<-lst$df
g<-lst$g
a+b+df+g
}
Did you mean to write "L" in the foregoing where you have written "lst"?
do_something(lst) My question is, is it possible to avoid the above sequence of statements, and achieve the same thing with a single line of code? Perhaps this would be bad programming practise, as you can't 'see' where objects in the function are coming from?
Perhaps:
do_something <- function(L){
with(L,a+b+df+g)
}
cheers,
Rolf Turner
I should have been clearer, the function I supplied is just a toy example, I actually have lots more elements, each with different classes, and need to do more complex things than just summation. The main reason I'm interested in this is to avoid having to keep adding an assignment like x<-lst$x each time the input list is extended. cheers, alastair -- View this message in context: http://r.789695.n4.nabble.com/Passing-lists-between-functions-tp4650792p4650799.html Sent from the R help mailing list archive at Nabble.com.
Thanks Rolf, that was a mistake. Brilliant, I think with() is what I'm looking for, thanks a lot! cheers, alastair Rolf Turner-3 wrote
On 26/11/12 14:24, Ally wrote:
I'd like to pass a list object created by one function as an argument of
another function. once inside the second function, I'd like to break the
list up to it's individual elements, each then identifiable by the
'names'
of the list.
The list looks something like
lst<-list(a=1, b=2, df=5, g=7)
then inside the function I've been writing a sequence of statements that
extract the objects within lst like
do_something<-function(L){
a<-lst$a
b<-lst$b
df<-lst$df
g<-lst$g
a+b+df+g
}
Did you mean to write "L" in the foregoing where you have written
"lst"?
do_something(lst) My question is, is it possible to avoid the above sequence of statements, and achieve the same thing with a single line of code? Perhaps this would be bad programming practise, as you can't 'see' where objects in the function are coming from?
Perhaps:
do_something <- function(L){
with(L,a+b+df+g)
}
cheers,
Rolf Turner
______________________________________________
R-help@
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.
-- View this message in context: http://r.789695.n4.nabble.com/Passing-lists-between-functions-tp4650792p4650801.html Sent from the R help mailing list archive at Nabble.com.