Re: [R] Using lapply and list names not available

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.be>
Date: Tue, 5 Feb 2008 17:24:42 +0100

hits=-2.5 tests=BAYES_00,FORGED_RCVD_HELO X-USF-Spam-Flag: NO

maybe you could use mapply(), e.g.,

people <- list(Andrew = rnorm(10), Mary = rnorm(10),

    Jane = rnorm(10), Richard = rnorm(10))

doplot <- function (individual, main) {

    plot(individual, main = main)
}

par(mfrow = c(2,2))
jpeg(file="test.jpg")
mapply(doplot, people, names(people))
dev.off()

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/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm



>
> Hello All
>
> Using lapply and ending up with lists of lists I often end up in the
> position of not having the names of the list passed by lapply. So,
> if I
> am doing something like a plot, and I would like the title to
> reflect
> which plot it is, I cannot easily do it. So I find myself doing some
> unstructured variable passing and counting to be able to keep track
> of
> my data. Then I think perhaps I should not use lapply and just use
> simple loops.
>
> Is there a better way to do this?
>
> Here is a simple example to illustrate what I am talking about. The
> list
> has the names of people and I need the names to use as the headings
> of
> the plots.
>
>
>
> ########################################################################
> #########
>
> # Make some test data
> people<-list(Andrew=rnorm(10), Mary=rnorm(10), Jane=rnorm(10),
> Richard=rnorm(10))
>
>
> # Function to plot each list entry with its title name
> doplot<-function(individual, peoplenames) {
> peoplecount<<-peoplecount + 1
> plot(individual, main=peoplenames[peoplecount])
> }
>
> #
> peoplecount<-0
> jpeg(file="test.jpg")
> par(mfrow=c(2,2))
> lapply(people, doplot, names(people))
> dev.off()
>
> ########################################################################
> #############
>
>
> Thank you for any suggestions.
>
>
> John Seers
>
> ---
>
> ______________________________________________
> 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.
>

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



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. Received on Tue 05 Feb 2008 - 16:26:46 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 05 Feb 2008 - 16:30:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive