[R] Simple extraction of level,x,y from contourLines()

Duncan Murdoch murdoch at stats.uwo.ca
Thu Oct 22 21:03:06 CEST 2009


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.




More information about the R-help mailing list