(Ted Harding)
2009-Oct-22 18:57 UTC
[R] Simple extraction of level,x,y from contourLines()
A follow-up to my previous query. Say one of the results returned by contourLines() is C.W <- contourLines(....) Then C.W is a list of (in this case 28) lists, each of which is a list with components $level (a single number), $x (the x coords of the points on a contour at that level) and $y (the y coords). I can of course get these individually with, for the 5th one for instance, C.W[[5]]$level C.W[[5]]$x C.W[[5]]$y But I can't see how to obtain, in one line and without running a nasty loop, to get all the levels at once! In other words, I'm looking for an expression which will return the vector c(C.W[[1]]$level,C.W[[2]]$level,...,C.W[[28]]$level) (This is probably something I should already have learned from the documentation, but I think I may have been asleep during that class ... ). With thanks, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 22-Oct-09 Time: 19:57:09 ------------------------------ XFMail ------------------------------
Duncan Murdoch
2009-Oct-22 19:03 UTC
[R] Simple extraction of level,x,y from contourLines()
On 22/10/2009 2:57 PM, (Ted Harding) wrote:> A follow-up to my previous query. > > Say one of the results returned by contourLines() is > > C.W <- contourLines(....) > > Then C.W is a list of (in this case 28) lists, > each of which is a list with components $level (a single number), > $x (the x coords of the points on a contour at that level) and > $y (the y coords). > > I can of course get these individually with, for the 5th one for > instance, > > C.W[[5]]$level > C.W[[5]]$x > C.W[[5]]$y > > But I can't see how to obtain, in one line and without running > a nasty loop, to get all the levels at once! > > In other words, I'm looking for an expression which will return > the vector > > c(C.W[[1]]$level,C.W[[2]]$level,...,C.W[[28]]$level)sapply(C.W., function(x) x$level) should do it. (It will contain repeats if your contours have gaps in them; unique() can remove those.) Duncan Murdoch> (This is probably something I should already have learned from > the documentation, but I think I may have been asleep during > that class ... ). > > With thanks, > Ted. > > -------------------------------------------------------------------- > E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> > Fax-to-email: +44 (0)870 094 0861 > Date: 22-Oct-09 Time: 19:57:09 > ------------------------------ XFMail ------------------------------ > > ______________________________________________ > 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.
Alberto Monteiro
2009-Oct-22 19:20 UTC
[R] Simple extraction of level,x,y from contourLines()
Ted Harding asked:> > I can of course get these individually with, for the 5th one for > instance, > > C.W[[5]]$level > C.W[[5]]$x > C.W[[5]]$y > > But I can't see how to obtain, in one line and without running > a nasty loop, to get all the levels at once! > > In other words, I'm looking for an expression which will return > the vector > > c(C.W[[1]]$level,C.W[[2]]$level,...,C.W[[28]]$level) >Hmmmm... Did you try this? # reproducible example C.W <- list(list(level = 1, x = 2), list(level = 2, y = 3), list(level = 10, z = 4)) sapply(C.W, function(x) x$level) Alberto Monteiro