[R] Writing a function

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Apr 28 12:42:19 CEST 2003


On Mon, 28 Apr 2003 vincent.stoliaroff at socgen.com wrote:

> I have written the following  function
> 
> > ClearDeltaBis
> function(Matrix){
> for (i in 1:3)
>                         {
>                         for (j in 1:2)
>                                 {if (is.na(Matrix[i,j]))
> NA->(Matrix[i,j+1])}
>                         }
> Matrix
> }
> 
> it looks correct but when I try to implement it on the MatCor Matrix I get
> this error message
> 
> > ClearDeltaBis(MatCor)
> Error: couldn't find function "(<-"
> 
> I don't understand where does it come from since I have no string like (<-
> in my code

You do, in the line

NA->(Matrix[i,j+1])

That's an unusual way of writing

(Matrix[i,j+1]) <- NA

and you have a set of parentheses there on the LHS of the assignment.
R is looking for a function "(<-" to implement this as

Matrix[i,j+1] <- "(<-"(NA)

You don't need (or want) the parentheses.

You might want to look up how functions on the LHS of assignments are 
supposed to work in R.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list