Skip to content
Prev 222433 / 398500 Next

problem with a function

On May 28, 2010, at 12:03 PM, Sarah Goslee wrote:

            
Actually, it's bad but not quite that bad.

 > mean <- c(1,2,3,4)
 > mean(1:10)
[1] 5.5
 > mean(mean)
[1] 2.5
 > mean[3]
[1] 3
 > mean
[1] 1 2 3 4

 > apply(matrix(1:100, ncol=10),1, mean)
  [1] 46 47 48 49 50 51 52 53 54 55
 > rm(mean)  # the interpreter "knows" to only remove the vector object
 > mean
function (x, ...)
UseMethod("mean")
<environment: namespace:base>
 > rm(mean)  # and will refuse to remove the base function
Warning message:
In rm(mean) : object 'mean' not found


Generally the interpreter can tell when a name is being intended as a  
function. Certainly when () follows the name, a function will be  
sought and other objects with identical names ignored.There are  
exceptions to that statement and your point is very well taken, but  
the main level of confusion is in the human brain rather than the R- 
interpreter. There used to be more partial name matching, but my  
reading of the NEWS items makes me think there is a shift away from  
that facility.  Other functions that people often mis-use as object  
names, generally without obvious deleterious effects:

df  # the density of the F distribution
c
data
sd
var
names
David Winsemius, MD
West Hartford, CT