1

I am practicing writing functions and testing them. I have written the following functions but when I try to specify na.rm=TRUE my NA's are not being removed.

my.mean <- function(x, na.rm) {
 stopifnot(is.numeric(x))
 answer <- sum(x)/length(x)
 return(answer)
}

My test vector is

t <- c(12,14,NA,1)

Result

my.mean(t, na.rm = TRUE)
[1] NA

I am not sure why I am not getting the answer 9. I have tried specifying

na.rm = FALSE

in the original function but know I should not have to.

Any ideas?

Thank you.

PS I know mean is a built in function for R I am just practicing. Also it is happening for my other written functions.

markus
  • 25,843
  • 5
  • 39
  • 58
snowy
  • 17
  • 2
  • 5
    You need to change `sum(x)` to `sum(x, na.rm = na.rm)`. Otherwise the second parameter of your function isn't being used at all (extra arguments aren't automatically passed to functions within a function, you have to do it yourself) – IceCreamToucan Nov 15 '18 at 21:27
  • 6
    You never use na.rm inside your function – hrbrmstr Nov 15 '18 at 21:28
  • 7
    In addition, try changing `length(x)` to `length(na.omit(x))` otherwise your result will be `6.75` not `9`. – markus Nov 15 '18 at 21:31
  • IceCreamToucan that did work to return a value but it is not the correct value. It give 6.75. so the sum is being divined by 4 entries when it should be 3. I want the NA's removed completely. – snowy Nov 15 '18 at 21:33
  • See @markus comment. I missed that part – IceCreamToucan Nov 15 '18 at 21:35
  • Ok @markus that did work to return the correct value (9). I am doing this for an assignment and for the previous test we are not supposed to specify na.rm=TRUE and get the return of NA. – snowy Nov 15 '18 at 21:35
  • 2
    It's still dividing by 4 because you haven't told the function `length` to do anything particular about the NA values. It sounds like rather than `na.rm`, you might prefer to just run `na.omit` on the vector first. Otherwise, you'd have to do something other than `length`, like `sum(!is.na(x))`. – joran Nov 15 '18 at 21:36
  • @joran I do like that solution but it seems like my professor does not always want NA's removed. Only if I specify for it to remove NA's – snowy Nov 15 '18 at 21:40
  • 1
    @snowy joran's suggestion is a good one. If there are `NA` values and `na.rm` is not TRUE, then the sum will be `NA` and the length won't matter. `NA / x = NA` for all `x`. Or, if you do feel like worrying about it, perhaps you've learned about someway way to do an operation (like `na.omit`) only `if` a certain condition is met... – Gregor Thomas Nov 15 '18 at 21:43
  • ok. so I added an `if` statement to the function. `my.mean <- function(x, na.rm) { stopifnot(is.numeric(x)) if na.rm=TRUE {na.omit()} answer <- sum(x)/length(x) return(answer) }` There are two tests i am running `t.2 <- c(12,14, NA, 1) my.mean(t.2) t.3 <- c(12,14,NA, 1) my.mean(t.3,na.rm=TRUE)` Before t.2 was working and t.3 wasn't and it was returning NA. now with the `if` statement t.3 is working and t.2 gives `Error in my.mean(t.2) : argument "na.rm" is missing, with no default` – snowy Nov 20 '18 at 03:26
  • YAY! I got it to work! I was forgetting to put a double equals sign in my true statement! – snowy Nov 20 '18 at 04:49

0 Answers0