Error when using the Apply function in R as an example tutorial

I am trying to learn how to use the apply function, and I came across this tutorial: http://nsaunders.wordpress.com/2010/08/20/a-brief-introduction-to-apply-in-r/ , which seems clear and concise, but I immediately run into a problem. The very first example they give for demonstration are:

> # create a matrix of 10 rows x 2 columns > m <- matrix(c(1:10, 11:20), nrow = 10, ncol = 2) > # mean of the rows > apply(m, 1, mean) [1] 6 7 8 9 10 11 12 13 14 15 

It seems very simple, but I thought I would try. Here is my result:

 > # create a matrix of 10 rows x 2 columns > m <- matrix(c(1:10, 11:20), nrow = 10, ncol = 2) > # mean of the rows > apply(m, 1, mean) Error in FUN(newX[, i], ...) : unused argument(s) (newX[, i]) 

Needless to say, I'm lost on this ...

To provide additional information, I tried to make another example, given in the textbook, and got the correct result. The difference in this case was that the function was specifically specified in the apply function:

 apply(m, 1:2, function(x) x/2) [,1] [,2] [1,] 0.5 5.5 [2,] 1.0 6.0 [3,] 1.5 6.5 [4,] 2.0 7.0 [5,] 2.5 7.5 [6,] 3.0 8.0 [7,] 3.5 8.5 [8,] 4.0 9.0 [9,] 4.5 9.5 [10,] 5.0 10.0 

sessionInfo () is output below:

 R version 2.15.3 (2013-03-01) Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit) locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] tools_2.15.3 

And the output for conflicts (details = TRUE)

 $.GlobalEnv [1] "edit" "mean" $`package:utils` [1] "edit" $`package:methods` [1] "body<-" "kronecker" $`package:base` [1] "body<-" "kronecker" "mean" 
+6
source share
1 answer

As others have found out, this is possible because you have a conflict on mean . When you call something (functions, objects), R goes through the search path until it is found (and if it is not found, R will complain accordingly):

 > search() [1] ".GlobalEnv" "tools:RGUI" "package:stats" [4] "package:graphics" "package:grDevices" "package:utils" [7] "package:datasets" "package:methods" "Autoloads" [10] "package:base" 

If you're fairly new to R, note that when you create a function, unless you specify otherwise, it will usually live in ".GlobalEnv" . R looks there before going any further, so it’s quite important to name your functions reasonably so as not to conflict with common functions (e.g. mean , plot , summary ).

It is probably a good idea to start with a clean session once in a while. At the debugging stage, it is often enough to name the variables x or y (the names are chosen for convenience, and not for informational purposes ... we are still people), which can be unexpectedly problematic along the line. When you have a workspace that is quite crowded, the likelihood of conflicts increases, so (a) carefully select the names and (b) rebooting without recovery will be my advice.

+2
source

All Articles