Dear R users/helpers, I am wondering is there an existing function in which you can round numbers to a set of values. I know you can use 5 * round(x/5) for rounding to the nearest 5 or so, but what if the interval size is not constant. For example: ## Not run test <- rnorm(100) round(test, c(1, 5, 10, 20, 50)) so that the test is rounded to the closest value in the vector. Thanks for the help. Cheers,
?findInterval .. would get you the endpoints and then you could determine which is nearer. Of course in your "example", everything would get "rounded" to 1. -- Bert On Thu, Dec 1, 2011 at 7:55 AM, Michael Kao <mkao006rmail at gmail.com> wrote:> Dear R users/helpers, > > I am wondering is there an existing function in which you can round numbers > to a set of values. I know you can use 5 * round(x/5) for rounding to the > nearest 5 or so, but what if the interval size is not constant. > > For example: > ## Not run > test <- rnorm(100) > round(test, c(1, 5, 10, 20, 50)) > > so that the test is rounded to the closest value in the vector. > > Thanks for the help. > > Cheers, > > ______________________________________________ > 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
On 01/12/2011 10:55 AM, Michael Kao wrote:> Dear R users/helpers, > > I am wondering is there an existing function in which you can round > numbers to a set of values. I know you can use 5 * round(x/5) for > rounding to the nearest 5 or so, but what if the interval size is not > constant. > > For example: > ## Not run > test<- rnorm(100) > round(test, c(1, 5, 10, 20, 50)) > > so that the test is rounded to the closest value in the vector.I think you could put together such a thing using cut(). Duncan Murdoch
Thanks for all the response, I manage to write a small function to complete what I want based on the wonderful helps. iround <- function(x, interval){ ## Round numbers to desired interval ## ## Args: ## x: Numeric vector to be rounded ## interval: The interval the values should be rounded towards. ## Retunrs: ## a numeric vector with x rounded to the desired interval. ## interval[ifelse(x < min(interval), 1, findInterval(x, interval))] } iround(0:100, c(1, 5, 10, 20, 50)) Cheers, and thanks for all the help again. On 1/12/2011 5:20 p.m., Duncan Murdoch wrote:> On 01/12/2011 10:55 AM, Michael Kao wrote: >> Dear R users/helpers, >> >> I am wondering is there an existing function in which you can round >> numbers to a set of values. I know you can use 5 * round(x/5) for >> rounding to the nearest 5 or so, but what if the interval size is not >> constant. >> >> For example: >> ## Not run >> test<- rnorm(100) >> round(test, c(1, 5, 10, 20, 50)) >> >> so that the test is rounded to the closest value in the vector. > > > I think you could put together such a thing using cut(). > > Duncan Murdoch
Maybe Matching Threads
- [LLVMdev] gcc 4.8.x dragonegg support
- [LLVMdev] gcc 4.8.x dragonegg support
- Is there a way to round numbers up or down to the nearest "natural looking" number?
- find closest value in a vector based on another vector values
- larger decimal numbers get rounded ....