Consider a vector of numbers. I would like to sample a number which is between a given position in the vector and its neighbors such that the two closest neighbors have the largest impact. Also, this impact is in the decreasing according to the distance from the reference point.

For example, consider the following vector:

` vec = c(15, 16, 18, 21, 24, 30, 31)`

and my reference is the number 16 in position #2. I would like to sample a number which will be with a high probability between 15 and 16 or (with the same high probability) between 16 and 18. The sampled numbers can be floats. Then, with a decreasing probability to sample a number between 16 and 21, and with a yet lower probability between 16 and 24, and so on.

The position of the reference is not known in advance, it can be anywhere in the vector.

I tried using with runif and quantiles, but I'm not sure how to design the scores of the neighbors.

Specifically, I wrote the following function but I suspect there might be a better/more efficient way of doing this:

```GenerateNumbers <- function(Ind,N){
dist <-  1/abs(Ind- 1:length(N))
dist <-  dist[!is.infinite(dist)]
dist <- dist/sum(dist)
sum(dist) #sanity check --> 1

V = numeric(length(N) - 1)
for (i in 1:(length(N)-1)) {
V[i] = runif(1, N[i], N[i+1])
}
sample(V,1,prob = dist)
}```

where Ind is the position of the reference number (16 in this case), and N is the vector. "Dist" is a way of weighing the probabilities so that the closest neighbors have a higher impact.

Any help is highly appreciated. May 29, 2018 173 views

## 1 answer to this question.

I would suggest you to use the truncated Gaussian random sample generator.

On your considered example, you can try the following code::

```# To install it: install.package("truncnorm")
library(truncnorm)
vec <- c(15, 16, 18, 21, 24, 30, 31)
x <- rtruncnorm(n=100, a=vec, b=vec, mean=vec, sd=1)```

The histogram of the generated sample fulfills the given prerequisites.  answered May 29, 2018 by
• 6,360 points

## How to remove NA values from a Vector in R?

Hello team, you can use na.omit x <- c(NA, 3, ...READ MORE

## How to import data from a Json file into R?

Easy and simple: library("rjson") json_file <- "file_name" json_data <- fromJSON(file=json_file)) or ...READ MORE

+1 vote

## I’m a noob to R and I’m supposed to printing all even numbers from 1 to 100. Can somebody help me with it?

Try this: Two <- seq(2,100,2) READ MORE

## R programming: How to pass variables from a r program to mysql function?

To include the R variables called start.date and end.date, you can use paste to ...READ MORE

## How to sample random rows in dataframe?

Create data frame and then implement as ...READ MORE

## How to sample n random rows per group in a dataframe?

You can assign a random ID to ...READ MORE

## 100 samples of 20 from the dataset and drawing regression lines along with population regression line

I have a datasetwith two variables hours ...READ MORE

## What happens when prob argument in sample sums to less/greater than 1?

Excellent query. The documentation is ambiguous on ...READ MORE

+1 vote