[R] error in which(): recursive default argument reference

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Apr 11 13:18:43 CEST 2006


'letters' is an argument to your function, with default 'letters', that it 
itself.  (Default arguments are evaluated in the evaluation frame of the 
function, not in the caller.)

Try letters=base::letters in your argument list.

On Tue, 11 Apr 2006, Matthieu Dubois wrote:

> Dear useRs,
>
> I have written a very simple function to compute some probabilities
> on words (function is below). The function includes a which()
> statement applied to a vector of characters (word.split): sapply
> (word.split, function(x) which(letters==x)). This statement  worked
> as expected when used outside the global function :
> > word <- "hello"
> > (word.split <- unlist(strsplit(word, split=c())))
> [1] "h" "e" "l" "l" "o"
> > (used.letters <- sapply(word.split, function(x) which(letters==x)))
> h  e  l  l  o
> 8  5 12 12 15
>
> However, when embedded in the fonction, the following error occurred :
> >  compute.word("hello")
> Error in which(letters == x) : recursive default argument reference
>
>
> Any insights would be helpful ...
>
> Cheers,
>
> Matthieu
>
> Function :
> #computes some probabilities based on (1) word frequency (word.freq),
> letters visibility(letters.vis), letters frequency(letters.freq) and
> viewing eccentricity (pos)
> compute.word <- function(word, pos=X, word.freq=word.freq,
> letters=letters, letters.vis=letters.vis, letters.freq=letters.freq)
> 	{
> 		word.split <- unlist(strsplit(word, split=c()))
>
> 		#indexing vectors
> 		used.letters <- sapply(word.split, function(x) which(letters==x))
> 		pos <- c(1,rep(2,(length(word.split)-2)),3)
>
> 		letters.freq.word <- unlist(letters.freq[cbind(used.letters, pos)])
> 		letters.vis.word <- letters.vis[paste("X", pos, sep=""),]
>
> 		#computations
> 		Mean.Lvis <- mean(letters.vis.word)
> 		Prod.Lvis <- prod(letters.vis.word)
> 		PosRel <- sum((word.freq/letters.freq.word)*(letters.vis.word))
> 		output <- list(Mean.Lvis, Prod.Lvis, PosRel)
> 		names(output) <- c("Mean.Lvis", "Prod.Lvis", "PosRel")
> 		return(output)
> 	}
>
>
>
> Matthieu Dubois, PH.D. Student
> Cognitive Neuroscience Unit
> Université catholique de Louvain
> 10, Place cardinal Mercier - 1348 Louvain-la-Neuve - BELGIUM
>
> Matthieu.Dubois at psp.ucl.ac.be
>
>
>
>
> 	[[alternative HTML version deleted]]
>
>

-- 
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