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@psp.ucl.ac.be




	[[alternative HTML version deleted]]


