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. Kind regards, Ivan
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
Hi,
Try:
set.seed(25)
g<- list(sample(1:40,20,replace=TRUE),sample(40:60,20,replace=TRUE))
names(g)<- c("2009-10-07","2012-02-29")
pdf("Trialnew.pdf")
?lapply(seq_along(g),function(i) plot(g[[i]],main=names(g)[i]))
dev.off()
A.K.
----- Original Message -----
From: Ivan Alves <pap:
ucho at me.com>
To: "R-help at r-project.org" <R-help at r-project.org>
Cc:
Sent: Wednesday, April 17, 2013 11:04 AM
Subject: [R] use of names() within lapply()
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.
Kind regards,
Ivan
______________________________________________
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.