Home > front end >  Mean function always returns NULL in R
Mean function always returns NULL in R

Time:05-18

The mean function is always returning NULL, no matter what I input. For example, mean(1) and mean(c(1, 2, 3)) both return NULL. I tried this on another computer and it gave me the correct values. I am using RStudio Version 2022.02.2 Build 485, "Prairie Trillium" Release for macOS and have unchecked all packages except 'base' in my library. Thanks!

CodePudding user response:

You probably have redefined the mean function and this new definition as been saved as part of the workspace. This workspace is loaded again when you start R and overwrite the definition of mean from base.

With the limited information you have provided, here are a few pointers:

mean(seq(3))
# overwriting base mean so mean(seq(3)) returns NULL
mean <- function(...) NULL
mean(seq(3))
base::mean(seq(3))
# restoring mean to base::mean
mean <- base::mean
mean(seq(3))

returns:

2
NULL
2
2

If you redefine mean <- base::mean, this will likely solve your issue.

  •  Tags:  
  • r
  • Related