> On Nov 9, 2015, at 9:19 AM, Adams, Jean <jvad...@usgs.gov> wrote: > > Harun, > > Can you give a simple example? > > If your cross_section looked like this > c(144, 179, 214, 39, 284, 109, 74, 4, 249) > and your other vector looked like this > c(0, 50, 100, 150, 200, 250, 300, 350) > what would you want your subset to look like? > > Jean > > On Mon, Nov 9, 2015 at 7:26 AM, Harun Rashid via R-help < > r-help@r-project.org> wrote: > >> Hello, >> I have a dataset with two columns 1. cross_section (range: 0~635), and >> 2. elevation. The dataset has more than 100 rows. Now I want to make a >> subset on the condition that the 'cross_section' column will pick up the >> nearest cell from another vector (say 0, 50,100,150,200,.....,650). >> How can I do this? I would really appreciate a solution.
If you what the "other vector" to define the “cell” boundaries, and using Jean’s example, it is a simple application of `findInterval`: > inp <- c(144, 179, 214, 39, 284, 109, 74, 4, 249) > mids <- c(0, 50, 100, 150, 200, 250, 300, 350) > findInterval( inp, c(mids) ) [1] 3 4 5 1 6 3 2 1 5 On the other hand ... To find the number of "closest point", this might help: > findInterval(inp, c( mids[1]-.001, head(mids,-1)+diff(mids)/2, > tail(mids,1)+.001 ) ) [1] 4 5 5 2 7 3 2 1 6 — David Winsemius Alameda, CA, USA ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.