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:
db_data <- iris
average1 <- mean(iris[1:10,2])
dev_std1 <- sd(iris[1:10,2])
value1 <- average + 3* dev_std
average2 <- mean(iris[1:20,2])
dev_std2 <- sd(iris[1:20,2])
value2 <- average2 + 3* dev_std2
average3 <- mean(iris[1:30,2])
dev_std3 <- sd(iris[1:30,2])
value3 <- average3 + 3* dev_std3
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?