[R] Using lapply and list names not available

Gabor Grothendieck ggrothendieck at gmail.com
Tue Feb 5 17:17:16 CET 2008


The problem is your data is in wide format and you want it in long format.
See ?reshape and also see the reshape package.   In your example, ?stack
is sufficient:

library(lattice)
xyplot(values ~ seq_along(values) | ind, data = stack(people))


On Feb 5, 2008 11:05 AM, john seers (IFR) <john.seers at bbsrc.ac.uk> wrote:
>
> 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.
>



More information about the R-help mailing list