[R] Subsetting dataframe by the nearest values of a vector elements
jim holtman
jholtman at gmail.com
Mon Nov 9 18:57:16 CET 2015
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]]
More information about the R-help
mailing list