[R] apply ? function doesnt create object

Charilaos Skiadas skiadas at hanover.edu
Sat Mar 3 22:53:57 CET 2007


On Mar 3, 2007, at 4:28 PM, bunny , lautloscrew.com wrote:

Please use <- for assignments instead of = :

> getans = function(x=qids,bnr=1,type="block")
> 	{
> #generate name of matrix
> matnam=paste("ans",type,as.character(bnr),sep="")
>
> #display result matrix
> show(assign(matnam,matrix(as.numeric(as.matrix(allans[(allans[, 3] %in
> % x), , drop = FALSE])),ncol=dim(allans)[2])))

You are assigning things twice here.

> #create result matrix
> assign(matnam,matrix(as.numeric(as.matrix(allans[(allans[, 3] %in%
> x), , drop = FALSE])),ncol=dim(allans)[2]))

The documentation for assign makes it pretty clear that the  
assignment happens by default in the current environment, so it will  
be local to the function unless you alter the call. The description  
there, along with the examples, and a study of environments, should  
provide you with the answer.

> #print info
> cat("the matrix",matnam,"contains answers to",type,as.character 
> (bnr))		
> 	
> }

Haris Skiadas
Department of Mathematics and Computer Science
Hanover College



More information about the R-help mailing list