my goal is to calculate a value, given by the sum of mean plus 3 standard deviations. The mean and standard deviation must be calculated over an increasing range:
the first values over the first 10 data points
the second on the first 20
and so on, as I wrote in the code:
I need these values to make a comparison with a column in my dataset. To give an example, taking the Iris dataset I want to check when the value in the second column is greater than my calculated value. I've tried writing this function, but I can't seem to get the different values in.
controll <- function(db_data){
average <- mean(iris[1:10,2])
dev_std <- sd(iris[1:10,2])
value <- average + 3* dev_std
if (db_data$Sepal.Width > value) {
db_data$controll <- 0
}else{
db_data$controll <- 1
}
}
controll(db_data)
Is there anything better? Is there already a function in R that can do this?
Ok this works! But in case I have multiple ranges? For example the Iris dataset has 150 rows, so in this case I would have 150 ranges. Writing them all by hand seems useless to me, isn't there a function?