Search code examples
rdebuggingsum

R: Error in simple call to sum() that I cannot reproduce - how can I find what the problem object is?


I am running these two simple lines of code:

vector <- c(2.5, 2.5, NA)
sum(vector, na.rm = TRUE)

And I get this error:

Error in sum(vector, na.rm = TRUE) : unused argument (na.rm = TRUE)

But if I run a reprex, then sum() works:

vector <- c(2.5, 2.5, NA)
sum(vector, na.rm = TRUE)
#> [1] 5

If I restart R I get the same error.

If I remove all objects, the function works.

How can I find which object is giving me this problem? Do I have to go through the file line by line and check the call to sum() every time I introduce a new object? I hope there is a better way.

Created on 2021-04-15 by the reprex package (v0.3.0)


Solution

  • As @LMc noticed, I had created another function called sum():

    sum <- function(a,b){a+b}
    vector <- c(2.5, 2.5, NA)
    sum(vector, na.rm = TRUE)
    #> Error in sum(vector, na.rm = TRUE): unused argument (na.rm = TRUE)
    

    Created on 2021-04-15 by the reprex package (v0.3.0)