Re: [R] Accessing list names in lapply

2009-11-20 Thread SIES 73
Yet another possibility is to iterate on both values and names simultaneously 
using mapply():

df1 - split(
   x=rnorm(n=100, sd=seq(from=1, to=10, each=10)),
   f=letters[seq(from=1, to=10, each=10)]
 )

mapply(function(x, y) plot(x, ylab=y), df1, names(df1)) 


Enrique

-Original Message-
Date: Thu, 19 Nov 2009 13:27:08 +0100
From: Bjarke Christensen bjarke.christen...@sydbank.dk
Subject: [R] Accessing list names in lapply
To: r-help@r-project.org
Message-ID:
of5533f0b9.99f78ab0-onc1257673.004273ae-c1257673.00446...@bdpnet.dk
Content-Type: text/plain; charset=US-ASCII


Hi,

When using lapply (or sapply) to loop over a list, can I somehow access the
index of the list from inside the function?

A trivial example:

df1 - split(
   x=rnorm(n=100, sd=seq(from=1, to=10, each=10)),
   f=letters[seq(from=1, to=10, each=10)]
 )
str(df1)
#List of 10
# $ a: num [1:10] -0.801 0.418 1.451 -0.554 -0.578 ...
# $ b: num [1:10] -2.464 0.279 4.099 -2.483 1.921 ...
# $ c: num [1:10] -1.14 -1.773 2.512 -2.072 -0.904 ...
# $ d: num [1:10] 2.109 1.243 0.627 -2.343 -6.071 ...
#...
par(mfcol=c(5,2))
lapply(df1, plot)

This plots each element of the list, but the label on the vertical axis is
X[[0L]] (as expected from the documentation in ?lapply). I'd like the
heading for each plot to be the name of that item in the list. This can be
achieved by using a for-loop:

for (i in names(df1)) plot(df1[[i]], ylab=i)

but can it somehow be achieved bu using lapply? I would be hoping for
something like

lapply(df1, function(x) plot(x, ylab=parent.index()))

or some way to parse the index number out of the call, using match.call()
or something like that.

Thanks in advance for any comments,
Bjarke Christensen

__
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] Accessing list names in lapply

2009-11-20 Thread Bjarke Christensen
Yes, that is it. Or even simpler:
mapply(plot, x=df1, ylab=names(df1), col=1:10, main=paste(Plot,
names(df1)))

Thank you!

Bjarke Christensen



   
 Bengoechea
 Bartolomé Enrique 
 (SIES 73) Til 
 enrique.bengoech r-help@r-project.org  
 e...@credit-suisse.  cc 
 com  bjarke.christen...@sydbank.dk 
  Emne 
 20-11-2009 13:19  RE: [R] Accessing list names in 
   lapply  
   
   
   
   
   
   




Yet another possibility is to iterate on both values and names
simultaneously using mapply():

df1 - split(
   x=rnorm(n=100, sd=seq(from=1, to=10, each=10)),
   f=letters[seq(from=1, to=10, each=10)]
 )

mapply(function(x, y) plot(x, ylab=y), df1, names(df1))


Enrique

-Original Message-
Date: Thu, 19 Nov 2009 13:27:08 +0100
From: Bjarke Christensen bjarke.christen...@sydbank.dk
Subject: [R] Accessing list names in lapply
To: r-help@r-project.org
Message-ID:

of5533f0b9.99f78ab0-onc1257673.004273ae-c1257673.00446...@bdpnet.dk
Content-Type: text/plain; charset=US-ASCII


Hi,

When using lapply (or sapply) to loop over a list, can I somehow access the
index of the list from inside the function?

A trivial example:

df1 - split(
   x=rnorm(n=100, sd=seq(from=1, to=10, each=10)),
   f=letters[seq(from=1, to=10, each=10)]
 )
str(df1)
#List of 10
# $ a: num [1:10] -0.801 0.418 1.451 -0.554 -0.578 ...
# $ b: num [1:10] -2.464 0.279 4.099 -2.483 1.921 ...
# $ c: num [1:10] -1.14 -1.773 2.512 -2.072 -0.904 ...
# $ d: num [1:10] 2.109 1.243 0.627 -2.343 -6.071 ...
#...
par(mfcol=c(5,2))
lapply(df1, plot)

This plots each element of the list, but the label on the vertical axis is
X[[0L]] (as expected from the documentation in ?lapply). I'd like the
heading for each plot to be the name of that item in the list. This can be
achieved by using a for-loop:

for (i in names(df1)) plot(df1[[i]], ylab=i)

but can it somehow be achieved bu using lapply? I would be hoping for
something like

lapply(df1, function(x) plot(x, ylab=parent.index()))

or some way to parse the index number out of the call, using match.call()
or something like that.

Thanks in advance for any comments,
Bjarke Christensen



--

__
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] Accessing list names in lapply

2009-11-19 Thread Bjarke Christensen

Hi,

When using lapply (or sapply) to loop over a list, can I somehow access the
index of the list from inside the function?

A trivial example:

df1 - split(
   x=rnorm(n=100, sd=seq(from=1, to=10, each=10)),
   f=letters[seq(from=1, to=10, each=10)]
 )
str(df1)
#List of 10
# $ a: num [1:10] -0.801 0.418 1.451 -0.554 -0.578 ...
# $ b: num [1:10] -2.464 0.279 4.099 -2.483 1.921 ...
# $ c: num [1:10] -1.14 -1.773 2.512 -2.072 -0.904 ...
# $ d: num [1:10] 2.109 1.243 0.627 -2.343 -6.071 ...
#...
par(mfcol=c(5,2))
lapply(df1, plot)

This plots each element of the list, but the label on the vertical axis is
X[[0L]] (as expected from the documentation in ?lapply). I'd like the
heading for each plot to be the name of that item in the list. This can be
achieved by using a for-loop:

for (i in names(df1)) plot(df1[[i]], ylab=i)

but can it somehow be achieved bu using lapply? I would be hoping for
something like

lapply(df1, function(x) plot(x, ylab=parent.index()))

or some way to parse the index number out of the call, using match.call()
or something like that.

Thanks in advance for any comments,
Bjarke Christensen

__
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] Accessing list names in lapply

2009-11-19 Thread Gabor Grothendieck
lapply over the list names rather than the list itself:

junk - lapply(names(df1), function(nm) plot(df1[[nm]], ylab = nm))


On Thu, Nov 19, 2009 at 7:27 AM, Bjarke Christensen
bjarke.christen...@sydbank.dk wrote:

 Hi,

 When using lapply (or sapply) to loop over a list, can I somehow access the
 index of the list from inside the function?

 A trivial example:

 df1 - split(
   x=rnorm(n=100, sd=seq(from=1, to=10, each=10)),
   f=letters[seq(from=1, to=10, each=10)]
  )
 str(df1)
 #List of 10
 # $ a: num [1:10] -0.801 0.418 1.451 -0.554 -0.578 ...
 # $ b: num [1:10] -2.464 0.279 4.099 -2.483 1.921 ...
 # $ c: num [1:10] -1.14 -1.773 2.512 -2.072 -0.904 ...
 # $ d: num [1:10] 2.109 1.243 0.627 -2.343 -6.071 ...
 #...
 par(mfcol=c(5,2))
 lapply(df1, plot)

 This plots each element of the list, but the label on the vertical axis is
 X[[0L]] (as expected from the documentation in ?lapply). I'd like the
 heading for each plot to be the name of that item in the list. This can be
 achieved by using a for-loop:

 for (i in names(df1)) plot(df1[[i]], ylab=i)

 but can it somehow be achieved bu using lapply? I would be hoping for
 something like

 lapply(df1, function(x) plot(x, ylab=parent.index()))

 or some way to parse the index number out of the call, using match.call()
 or something like that.

 Thanks in advance for any comments,
 Bjarke Christensen

 __
 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] Accessing list names in lapply

2009-11-19 Thread Duncan Murdoch

On 19/11/2009 7:27 AM, Bjarke Christensen wrote:

Hi,

When using lapply (or sapply) to loop over a list, can I somehow access the
index of the list from inside the function?
  


No, but you can loop over the indices in lapply, not just in a for 
loop.  For example,


lapply(names(df1), function(x) plot(df1[[x]], ylab=x))

Duncan Murdoch

A trivial example:

df1 - split(
   x=rnorm(n=100, sd=seq(from=1, to=10, each=10)),
   f=letters[seq(from=1, to=10, each=10)]
 )
str(df1)
#List of 10
# $ a: num [1:10] -0.801 0.418 1.451 -0.554 -0.578 ...
# $ b: num [1:10] -2.464 0.279 4.099 -2.483 1.921 ...
# $ c: num [1:10] -1.14 -1.773 2.512 -2.072 -0.904 ...
# $ d: num [1:10] 2.109 1.243 0.627 -2.343 -6.071 ...
#...
par(mfcol=c(5,2))
lapply(df1, plot)

This plots each element of the list, but the label on the vertical axis is
X[[0L]] (as expected from the documentation in ?lapply). I'd like the
heading for each plot to be the name of that item in the list. This can be
achieved by using a for-loop:

for (i in names(df1)) plot(df1[[i]], ylab=i)

but can it somehow be achieved bu using lapply? I would be hoping for
something like

lapply(df1, function(x) plot(x, ylab=parent.index()))

or some way to parse the index number out of the call, using match.call()
or something like that.

Thanks in advance for any comments,
Bjarke Christensen

__
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] Accessing list names in lapply

2009-11-19 Thread Romain Francois

Maybe this : http://tolstoy.newcastle.edu.au/R/e4/help/08/04/8720.html

Romain

On 11/19/2009 01:27 PM, Bjarke Christensen wrote:

Hi,

When using lapply (or sapply) to loop over a list, can I somehow access the
index of the list from inside the function?

A trivial example:

df1- split(
x=rnorm(n=100, sd=seq(from=1, to=10, each=10)),
f=letters[seq(from=1, to=10, each=10)]
  )
str(df1)
#List of 10
# $ a: num [1:10] -0.801 0.418 1.451 -0.554 -0.578 ...
# $ b: num [1:10] -2.464 0.279 4.099 -2.483 1.921 ...
# $ c: num [1:10] -1.14 -1.773 2.512 -2.072 -0.904 ...
# $ d: num [1:10] 2.109 1.243 0.627 -2.343 -6.071 ...
#...
par(mfcol=c(5,2))
lapply(df1, plot)

This plots each element of the list, but the label on the vertical axis is
X[[0L]] (as expected from the documentation in ?lapply). I'd like the
heading for each plot to be the name of that item in the list. This can be
achieved by using a for-loop:

for (i in names(df1)) plot(df1[[i]], ylab=i)

but can it somehow be achieved bu using lapply? I would be hoping for
something like

lapply(df1, function(x) plot(x, ylab=parent.index()))

or some way to parse the index number out of the call, using match.call()
or something like that.

Thanks in advance for any comments,
Bjarke Christensen


--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/EAD5 : LondonR slides
|- http://tr.im/BcPw : celebrating R commit #5
`- http://tr.im/ztCu : RGG #158:161: examples of package IDPmisc

__
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] Accessing list names in lapply

2009-11-19 Thread Henrique Dallazuanna
You can try this:

par(mfcol=c(5,2))
lapply(df1, function(x){
nm - 
names(eval(as.list(sys.call(-1))[[2]]))[as.numeric(gsub([^0-9],
, deparse(substitute(x]
plot(x, main = nm)
})

On Thu, Nov 19, 2009 at 10:27 AM, Bjarke Christensen
bjarke.christen...@sydbank.dk wrote:

 Hi,

 When using lapply (or sapply) to loop over a list, can I somehow access the
 index of the list from inside the function?

 A trivial example:

 df1 - split(
   x=rnorm(n=100, sd=seq(from=1, to=10, each=10)),
   f=letters[seq(from=1, to=10, each=10)]
  )
 str(df1)
 #List of 10
 # $ a: num [1:10] -0.801 0.418 1.451 -0.554 -0.578 ...
 # $ b: num [1:10] -2.464 0.279 4.099 -2.483 1.921 ...
 # $ c: num [1:10] -1.14 -1.773 2.512 -2.072 -0.904 ...
 # $ d: num [1:10] 2.109 1.243 0.627 -2.343 -6.071 ...
 #...
 par(mfcol=c(5,2))
 lapply(df1, plot)

 This plots each element of the list, but the label on the vertical axis is
 X[[0L]] (as expected from the documentation in ?lapply). I'd like the
 heading for each plot to be the name of that item in the list. This can be
 achieved by using a for-loop:

 for (i in names(df1)) plot(df1[[i]], ylab=i)

 but can it somehow be achieved bu using lapply? I would be hoping for
 something like

 lapply(df1, function(x) plot(x, ylab=parent.index()))

 or some way to parse the index number out of the call, using match.call()
 or something like that.

 Thanks in advance for any comments,
 Bjarke Christensen

 __
 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] Accessing list names in lapply

2009-11-19 Thread Bjarke Christensen
Thanks to everybody who replied - I got three distinct, very useful
suggestions.

Bjarke Christensen



   
 Romain Francois   
 romain.francois@ 
 dbmail.com   Til 
   Bjarke Christensen  
 19-11-2009 14:33  bjarke.christen...@sydbank.dk 
cc 
   r-help@r-project.org
  Emne 
   Re: [R] Accessing list names in 
   lapply  
   
   
   
   
   
   




Maybe this : http://tolstoy.newcastle.edu.au/R/e4/help/08/04/8720.html

Romain

On 11/19/2009 01:27 PM, Bjarke Christensen wrote:
 Hi,

 When using lapply (or sapply) to loop over a list, can I somehow access
the
 index of the list from inside the function?

 A trivial example:

 df1- split(
 x=rnorm(n=100, sd=seq(from=1, to=10, each=10)),
 f=letters[seq(from=1, to=10, each=10)]
   )
 str(df1)
 #List of 10
 # $ a: num [1:10] -0.801 0.418 1.451 -0.554 -0.578 ...
 # $ b: num [1:10] -2.464 0.279 4.099 -2.483 1.921 ...
 # $ c: num [1:10] -1.14 -1.773 2.512 -2.072 -0.904 ...
 # $ d: num [1:10] 2.109 1.243 0.627 -2.343 -6.071 ...
 #...
 par(mfcol=c(5,2))
 lapply(df1, plot)

 This plots each element of the list, but the label on the vertical axis
is
 X[[0L]] (as expected from the documentation in ?lapply). I'd like the
 heading for each plot to be the name of that item in the list. This can
be
 achieved by using a for-loop:

 for (i in names(df1)) plot(df1[[i]], ylab=i)

 but can it somehow be achieved bu using lapply? I would be hoping for
 something like

 lapply(df1, function(x) plot(x, ylab=parent.index()))

 or some way to parse the index number out of the call, using match.call()
 or something like that.

 Thanks in advance for any comments,
 Bjarke Christensen

--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/EAD5 : LondonR slides
|- http://tr.im/BcPw : celebrating R commit #5
`- http://tr.im/ztCu : RGG #158:161: examples of package IDPmisc

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