```
### QUESTION 1
#
## Topic: Sheet 8 Exercise 3
## Question:
# I tried to add something to remove all NAs from the argument vector, but R still shows me NA as the answer.
# I don't know where my mistake is and can not find it. I would be glad, if you could schow me at least the part, where I'm wrong.
# Code from the student
se<-function (x,na.rm=FALSE){
if(!is.numeric(x)){
warning ("Argument is not numeric: returning NA")
} else{
return(sd(x)/sqrt(length(x)))
}
if(na.rm ==TRUE){
x<-x[is.na(x)==FALSE]
}
return(sd(x)/sqrt(length(x)))
}
se(c(3, 5, "a", 7),na.rm=TRUE)
```

```
## Warning in se(c(3, 5, "a", 7), na.rm = TRUE): Argument is not numeric: returning
## NA
```

```
## Warning in var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm =
## na.rm): NAs introduced by coercion
```

`## [1] NA`

`se(c(3, NA, 8, 2), na.rm=TRUE)`

`## [1] NA`

```
## Answer:
# the problem in your code was the else condition.
# What happened in your code was that the else condition would be returned as long as there are no non-numeric values.
# The problem is that in a vector of numeric values, the NA will not be recognized as non-numeric.
# You can check this with:
is.numeric(c(3, NA, 8, 2))
```

`## [1] TRUE`

```
# This means your code would do the calculation using the vector with NA before even getting to the step of removing the NA.
# It would also make more sense to leave out the else condition altogether and instead put the return function outside of both if conditions at the very end.
###corrected version
se<-function (x,na.rm=FALSE){
if(!is.numeric(x)){
warning ("Argument is not numeric: returning NA")
return(NA)
}
if(na.rm ==TRUE){
x<-x[is.na(x)==FALSE]
}
return(sd(x)/sqrt(length(x)))
}
```