Skip to content

passing arguments to FUN in lapply

5 messages · Pingping Zheng, Uwe Ligges, Dimitris Rizopoulos +2 more

#
Suppose I have a nx2 matrix of data, X, the following code generate
density estimation for each column and plot them

denlist <- apply(X, 2, density)
par(mfrow=c(1,2))
lapply(denlist, plot)

Does anyone know how to change the main title of each density plot
to "var 1", "var 2" by passing optional argument "main"? I've tried

lapply(denlist, plot, main=c("var 1", "var 2"))
which generates two same titles "var 1/var 2" and "var 1/var 2"

lapply(denlist, plot, main=list("var 1", "var 2"))
which generates two same titles "var 1"

Both are not I want.
#
Pingping Zheng wrote:

            
If the title is contained in denlist, you can specify a anonymous 
function, if not, use mapply or simply a for() loop.

Uwe Ligges
#
an indirect solution is the following:

lapply(seq(along=denlist), function(i) plot(denlist[[i]], 
main=paste("var", i)))

I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm

----- Original Message ----- 
From: "Pingping Zheng" <pingping.zheng at lancaster.ac.uk>
To: <r-help at stat.math.ethz.ch>
Sent: Friday, March 18, 2005 12:40 PM
Subject: [R] passing arguments to FUN in lapply
#
Pingping Zheng <pingping.zheng <at> lancaster.ac.uk> writes:

: 
: Suppose I have a nx2 matrix of data, X, the following code generate
: density estimation for each column and plot them
: 
: denlist <- apply(X, 2, density)
: par(mfrow=c(1,2))
: lapply(denlist, plot)
: 
: Does anyone know how to change the main title of each density plot
: to "var 1", "var 2" by passing optional argument "main"? I've tried
: 
: lapply(denlist, plot, main=c("var 1", "var 2"))
: which generates two same titles "var 1/var 2" and "var 1/var 2"
: 
: lapply(denlist, plot, main=list("var 1", "var 2"))
: which generates two same titles "var 1"
: 
: Both are not I want.
: 

Try mapply instead of lapply:

mapply(plot, denlist, main = paste("var", 1:2))
#
See Andy Liaw's and my suggestion to this post
http://files.protsuggest.org/biocond/html/7818.html
On Fri, 2005-03-18 at 11:40 +0000, Pingping Zheng wrote: