Select the upper and lower values ​​and create a new column with random values

advertisements

I have a data set with 30 rows and 2 columns. For example:

value <- c(305.19, 30.19, 126.97, 178.89, 640.01, 189.28, 15.92,
           219.79, 178.89, 911.61, 178.89, 18.28, 305.19, 322.90,
           126.97, 12.97, 178.89, 12.97, 322.90, 158.92, 322.90,
           219.79, 911.61, 219.79, 911.61, 325.90, 322.90, 911.61,
           158.92, 322.78)

Date <- c("1997-05-01","1997-05-02","1997-05-03","1997-05-04","1997-05-05",
          "1997-05-06","1997-05-07","1997-05-08","1997-05-09","1997-05-10",
          "1997-05-11","1997-05-12","1997-05-13","1997-05-14","1997-05-15",
          "1997-05-16","1997-05-17","1997-05-18","1997-05-19","1997-05-20",
          "1997-05-21","1997-05-22","1997-05-23","1997-05-24","1997-05-25",
          "1997-05-26","1997-05-27","1997-05-28","1997-05-29","1997-05-30")

data <- data.frame(value, Date)

Using the quantile function, I can select values between the percentages 25%, 50%, 75%.

For example:

data$value <- data$value[data$value>=quantile(data$value)[4]]
newvalue <- data$value[data$value>=quantile(data$value)[4]]
data$value <- sample(newvalue, dim(data)[1], replace=T)

I would like to create a new column with random values only - lower 12.5% and upper 87.5%.

how can I do that?


We can do

q1 <- quantile(data$value, probs = c(0.125, 0.875))
data$newValue <- sample(data$value[data$value > q1[1] & data$value < q1[2]],
                             nrow(data), replace = TRUE)