Re: [R] apply ? function doesnt create object
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 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] apply function
Eric Pellegrini <[EMAIL PROTECTED]> writes: > Hi all, > > I have a question about apply function. Is that possible to pass some > non-default arguments in the function we want to apply ? > > For example: > > if "mat" is a matrix and I want to use the "tabulate" function on its row. > > The command apply(mat,1,tabulate) works but I have problem with this one > apply(mat, 1, tabulate(nbins=4)). > > Any clue ? You might have gotten one by R'ing TFHP apply(mat, 1, tabulate, nbins=4) There's even an example! -- O__ Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] apply function
Try apply(mat, 1, tabulate, nbins=4). HTH, Andy > From: Eric Pellegrini > > Hi all, > > I have a question about apply function. Is that possible to pass some > non-default arguments in the function we want to apply ? > > For example: > > if "mat" is a matrix and I want to use the "tabulate" > function on its row. > > The command apply(mat,1,tabulate) works but I have problem > with this one > apply(mat, 1, tabulate(nbins=4)). > > Any clue ? > > Thanks, > > Eric > > > > -- > Eric Pellegrini, PhD > Computer-Chemie-Centrum > University of Erlangen-Nürnberg > Nägelbachstraße, 25 > D-91052 Erlangen > Germany > > __ > [EMAIL PROTECTED] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html