r - Select upper and lower values and create a new column with random values -


i have data set 30 rows , 2 columns. 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 quantile function, can select values between 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 create new column random values - lower 12.5% , upper 87.5%.

how can 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) 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -