Hi I use the following code and it stores the results of density() in the list dr: dens <- function(run) { density( positions$X[positions$run==run], bw=3, cut=-2 ) } dr <- lapply(1:5, dens) but the results are stored in dr[[i]] and not dr[i], i.e. plot(dr[[1]]) works, but plot([1]) doesn't. Is there any way that I can store them in dr[i]? Thanks a lot, Rainer -- Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation Biology (UCT) Department of Conservation Ecology and Entomology University of Stellenbosch Matieland 7602 South Africa Tel: +27 - (0)72 808 2975 (w) Fax: +27 - (0)21 808 3304 Cell: +27 - (0)83 9479 042 email: RKrug at sun.ac.za Rainer at krugs.de
Use 'sapply' instead of 'lapply'. Type>?lapplyfor details Antonio, Fabio Di Narzo. University of Bologna, Italy 2006/9/6, Rainer M Krug <rkrug a sun.ac.za>:> Hi > > I use the following code and it stores the results of density() in the > list dr: > > dens <- function(run) { density( positions$X[positions$run==run], bw=3, > cut=-2 ) } > dr <- lapply(1:5, dens) > > but the results are stored in dr[[i]] and not dr[i], i.e. plot(dr[[1]]) > works, but plot([1]) doesn't. > > Is there any way that I can store them in dr[i]? > > Thanks a lot, > > Rainer > > > > -- > Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation > Biology (UCT) > > Department of Conservation Ecology and Entomology > University of Stellenbosch > Matieland 7602 > South Africa > > Tel: +27 - (0)72 808 2975 (w) > Fax: +27 - (0)21 808 3304 > Cell: +27 - (0)83 9479 042 > > email: RKrug a sun.ac.za > Rainer a krugs.de > > ______________________________________________ > R-help a stat.math.ethz.ch 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. >
I suspect you are not thinking about the list and the subsetting/extraction operators in the right way. A list contains a number of components. To get a subset of the list, use the '[' operator. The subset can contain zero or more components of the list, and it is a list itself. So, if x is a list, then x[2] is a list containing a single component. To extract a component from the list, use the '[[' operator. You can only extract one component at a time. If you supply a vector index with more than one element, it will index recursively. > x <- list(1,2:3,letters[1:3]) > x [[1]] [1] 1 [[2]] [1] 2 3 [[3]] [1] "a" "b" "c" > # a subset of the list > x[2:3] [[1]] [1] 2 3 [[2]] [1] "a" "b" "c" > # a list with one component: > x[2] [[1]] [1] 2 3 > # the second component itself > x[[2]] [1] 2 3 > # recursive indexing > x[[c(2,1)]] [1] 2 > x[[c(3,2)]] [1] "b" > Rainer M Krug wrote:> Hi > > I use the following code and it stores the results of density() in the > list dr: > > dens <- function(run) { density( positions$X[positions$run==run], bw=3, > cut=-2 ) } > dr <- lapply(1:5, dens) > > but the results are stored in dr[[i]] and not dr[i], i.e. plot(dr[[1]]) > works, but plot([1]) doesn't. > > Is there any way that I can store them in dr[i]? > > Thanks a lot, > > Rainer > > >
Well - it must be that the whole concept of lists, vectors / matrices, objects, data frames is not that clear to me. From my background (Delphi, Basic), I am used to that, when referencing dr[i] I get the object which is stored in the list. Is there any manual / technical manual / reference available which explains all this? The normal manual (Introduction into R) didn't made it clear to me. Rainer jim holtman wrote:> What is the problem with referencing it with 'dr[[i]]', the way a list > is supposed to be referenced? > > On 9/6/06, Rainer M Krug <rkrug at sun.ac.za> wrote: >> Antonio, Fabio Di Narzo wrote: >> > Use 'sapply' instead of 'lapply'. Type >> If I use sapply it seems to simplify / collapse to much. >> >> >> ?lapply >> > for details >> >> >> > >> > Antonio, Fabio Di Narzo. >> > University of Bologna, Italy >> > >> > 2006/9/6, Rainer M Krug <rkrug at sun.ac.za>: >> >> Hi >> >> >> >> I use the following code and it stores the results of density() in the >> >> list dr: >> >> >> >> dens <- function(run) { density( positions$X[positions$run==run], >> bw=3, >> >> cut=-2 ) } >> >> dr <- lapply(1:5, dens) >> >> >> >> but the results are stored in dr[[i]] and not dr[i], i.e. >> plot(dr[[1]]) >> >> works, but plot([1]) doesn't. >> >> >> >> Is there any way that I can store them in dr[i]? >> >> >> >> Thanks a lot, >> >> >> >> Rainer >> >> >> >> >> >> >> >> -- >> >> Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation >> >> Biology (UCT) >> >> >> >> Department of Conservation Ecology and Entomology >> >> University of Stellenbosch >> >> Matieland 7602 >> >> South Africa >> >> >> >> Tel: +27 - (0)72 808 2975 (w) >> >> Fax: +27 - (0)21 808 3304 >> >> Cell: +27 - (0)83 9479 042 >> >> >> >> email: RKrug at sun.ac.za >> >> Rainer at krugs.de >> >> >> >> ______________________________________________ >> >> R-help at stat.math.ethz.ch 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. >> >> >> >> >> -- >> Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation >> Biology (UCT) >> >> Department of Conservation Ecology and Entomology >> University of Stellenbosch >> Matieland 7602 >> South Africa >> >> Tel: +27 - (0)72 808 2975 (w) >> Fax: +27 - (0)21 808 3304 >> Cell: +27 - (0)83 9479 042 >> >> email: RKrug at sun.ac.za >> Rainer at krugs.de >> >> ______________________________________________ >> R-help at stat.math.ethz.ch 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. >> > >-- Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation Biology (UCT) Department of Conservation Ecology and Entomology University of Stellenbosch Matieland 7602 South Africa Tel: +27 - (0)72 808 2975 (w) Fax: +27 - (0)21 808 3304 Cell: +27 - (0)83 9479 042 email: RKrug at sun.ac.za Rainer at krugs.de
Thanks a lot - it looks like the sort of manual I was looking for - Could I suggest of including it into the list of manuals for R? (If it is already there, my apologies) Rainer Patrick Burns wrote:> S Poetry should help you understand this. See > especially the section of chapter 1 on subscripting. > > Patrick Burns > patrick at burns-stat.com > +44 (0)20 8525 0696 > http://www.burns-stat.com > (home of S Poetry and "A Guide for the Unwilling S User") > > Rainer M Krug wrote: > >> Hi >> >> I use the following code and it stores the results of density() in the >> list dr: >> >> dens <- function(run) { density( positions$X[positions$run==run], bw=3, >> cut=-2 ) } >> dr <- lapply(1:5, dens) >> >> but the results are stored in dr[[i]] and not dr[i], i.e. plot(dr[[1]]) >> works, but plot([1]) doesn't. >> >> Is there any way that I can store them in dr[i]? >> >> Thanks a lot, >> >> Rainer >> >> >> >> >>-- Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation Biology (UCT) Department of Conservation Ecology and Entomology University of Stellenbosch Matieland 7602 South Africa Tel: +27 - (0)72 808 2975 (w) Fax: +27 - (0)21 808 3304 Cell: +27 - (0)83 9479 042 email: RKrug at sun.ac.za Rainer at krugs.de