Harun Rashid
2015-Nov-09 13:26 UTC
[R] Subsetting dataframe by the nearest values of a vector elements
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. Regards, Harun -- <mailto:mhrashidbau at yahoo.com> <mailto:mhrashidbau at yahoo.com> [[alternative HTML version deleted]]
Adams, Jean
2015-Nov-09 17:19 UTC
[R] Subsetting dataframe by the nearest values of a vector elements
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 at 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. > Regards, > Harun > -- > > <mailto:mhrashidbau at yahoo.com> > <mailto:mhrashidbau at yahoo.com> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >[[alternative HTML version deleted]]
jim holtman
2015-Nov-09 17:57 UTC
[R] Subsetting dataframe by the nearest values of a vector elements
Do you want the "closest" or what range it is in? If you want the range, then use 'cut':> x <- c(144, 179, 214, 39, 284, 109, 74, 4, 249) > range <- c(0, 50, 100, 150, 200, 250, 300, 350) > result <- cut(x, breaks = range) > cbind(x, as.character(result))x [1,] "144" "(100,150]" [2,] "179" "(150,200]" [3,] "214" "(200,250]" [4,] "39" "(0,50]" [5,] "284" "(250,300]" [6,] "109" "(100,150]" [7,] "74" "(50,100]" [8,] "4" "(0,50]" [9,] "249" "(200,250]" Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Mon, Nov 9, 2015 at 12:19 PM, Adams, Jean <jvadams at 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 at 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. > > Regards, > > Harun > > -- > > > > <mailto:mhrashidbau at yahoo.com> > > <mailto:mhrashidbau at yahoo.com> > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at 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. > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >[[alternative HTML version deleted]]
David Winsemius
2015-Nov-10 02:17 UTC
[R] Subsetting dataframe by the nearest values of a vector elements
> On Nov 9, 2015, at 9:19 AM, Adams, Jean <jvadams at 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 at 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