Hello everyone. This is a cryfor help regarding my Homework.
I need help fixing a function I wrote because it is not doing the purpose I wrote it for.
The assignment was to write a function (you can name it however you want) and it suposed goes like this:
MyMean <- function (x, na.rm = FALSE)
x is a numeric not empty vector that could have NA values.
na.rm is a Boolean value that his default is FALSE.
Term 1: If na.rm== FALSE and there are missing values at X, the function should return NA
Term 2: If na.rm == FALSE and there are no missing values, the function should calculate the mean of the numeric vector mathematical organs.
Term 3: if na.rm == TRUE, and their are missing organs, the function should return the mean of the vector mathematical organs without the missing one.
This is what I wrote:
myMean <- function(x, na.rm=FALSE){
for(ind in 1:length(x)){
if(is.na(x[ind]== TRUE & na.rm == FALSE)){
return(NA)
}else if(is.na(x[ind]== FALSE & na.rm == FALSE)){
return(sum(x)/length(x))
}else if(is.na(x[ind]== TRUE & na.rm == TRUE)){
new.Sum <- 0
Deacrese <- 0
for(ind in 1:length(x)){
if(is.na(x[ind]== FALSE)){
new.Sum <- new.Sum + x[ind]
}else{
Deacrese <- Deacrese + 1
}
return(new.Sum/(length(x)-Deacrese))
}
}
}
}
Thank you for the help and God Bless You All