# How to add new value to existing dataset so that only the range changes but mean remains the same in R?

## Issue

Hi I’m a student studying statistic, as my textbook does not include much of the R coding but more of the basic calculation. Hence, would like to ask if it is there a way in R, for adding additional number to the existing generated set with specific mean and range?

1(a) Apply R to simulate a set of 100 numbers, with mean value of 20 and standard deviation of 2. List out the set of numbers.

``````> x <- rnorm(100,20,2)
> print(x)
[1] 20.59256 20.66069 12.68841 21.13575 24.09587 21.69535 20.18661 21.71236 20.92864 19.63182 22.12583 19.06238
[13] 18.73813 22.59813 17.30012 16.98957 20.74050 21.28319 19.75426 20.62065 20.20814 18.16406 22.24261 22.05673
[25] 21.27086 18.78538 21.86479 18.03242 21.00538 20.27731 22.59440 23.24389 20.20846 19.73281 19.50040 20.51712
[37] 20.16493 23.56715 21.25884 18.37542 19.84470 19.81911 16.94701 19.06637 17.74580 18.03151 19.57144 16.45314
[49] 20.89975 21.86249 17.42996 23.52514 21.17759 20.20160 18.11839 21.69716 16.93685 20.62335 20.37935 22.46131
[61] 17.78489 19.90424 17.67674 20.20571 21.60567 20.41897 20.25134 22.44366 19.06513 20.62692 24.04101 24.03634
[73] 20.15566 20.33157 20.22881 20.54014 19.49401 17.34388 19.94099 18.71450 19.24386 19.91813 18.71863 20.94027
[85] 17.55676 17.18079 24.96868 24.09565 19.87488 20.06114 19.21374 18.39874 21.01435 18.38329 20.91788 21.45158
[97] 20.43168 21.80438 20.50405 23.07149
``````

(b) Add another 2 numbers to the set simulated in Question 1(a), such that the new set now has (same) mean of 20, but range becomes 200. List out the set of numbers.

## Solution

First create reproducible data:

``````set.seed(42)
x <- rnorm(100,20,2)
mean(x)
# [1] 20.06503
range(x)
# [1] 14.01382 24.57329
(x2 <- mean(x) + c(-100, 100))
# [1] -79.93497 120.06503
``````

To keep the mean the same we need to add points 100 above the mean and 100 below the mean. Fortunately these points lie beyond the original range.

``````mean(c(x, x2))
# [1] 20.06503
diff(range(c(x, x2)))
# [1] 200
``````

The mean is the same and the range is now 200.