Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mean function always returns NULL in R

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!

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading