[R] use of names() within lapply()

arun smartpink111 at yahoo.com
Wed Apr 17 18:58:56 CEST 2013


Dear Ivan,
No problem.
If you want it in a single plot:


matplot(do.call(cbind,g),ylab="value",pch=1:2,main="Some plot",col=c("red","orange"),type="o")
 legend("topleft",inset=.01,lty=c(1,1),title="Plot",col=c("red","orange"),names(g),horiz=TRUE)
A.K.


________________________________
 From: Ivan Alves <papucho at me.com>
To: Duncan Murdoch <murdoch.duncan at gmail.com>; arun <smartpink111 at yahoo.com> 
Cc: "R-help at r-project.org" <R-help at r-project.org> 
Sent: Wednesday, April 17, 2013 11:33 AM
Subject: Re: [R] use of names() within lapply()
 

Dear Duncan and A.K.
Many thanks for your super quick help. The modified lapply did the trick, mapply died with a error "Error in dots[[2L]][[1L]] : object of type 'builtin' is not subsettable".
Kind regards,
Ivan
On 17 Apr 2013, at 17:12, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:

> On 17/04/2013 11:04 AM, Ivan Alves wrote:
>> Dear all,
>> 
>> List g has 2 elements
>> 
>> > names(g)
>> [1] "2009-10-07" "2012-02-29"
>> 
>> and the list plot
>> 
>> lapply(g, plot, main=names(g))
>> 
>> results in equal plot titles with both list names, whereas distinct titles names(g[1]) and names(g[2]) are sought. Clearly, lapply is passing 'g' in stead of consecutively passing g[1] and then g[2] to process the additional 'main'  argument to plot.  help(lapply) is mute as to what to element-wise pass parameters.  Any suggestion would be appreciated.
> 
> I think you want mapply rather than lapply, or you could do lapply on a vector of indices.  For example,
> 
> mapply(plot, g, main=names)
> 
> or
> 
> lapply(1:2, function(i) plot(g[[i]], main=names(g)[i]))
> 
> Duncan Murdoch



More information about the R-help mailing list