Instead of trying to create object back in the global environment (which is
not really recommended from a function), return the answer in a list that
you can then access it as you want.  Here is an example:


> getans <- function(x){
+     # create the name that you want in the list
+     .name <- paste(x, '.Ans', sep='')
+     .ans <- list(runif(10))   # create some data to return
+     names(.ans) <- .name
+     .ans    # return the answer
+ }
>
> my.ans <- getans('newName')
> my.ans  # show what was returned
$newName.Ans
 [1] 0.2059746 0.1765568 0.6870228 0.3841037 0.7698414 0.4976992 0.7176185
0.9919061 0.3800352 0.7774452

> # access by the new name
> my.ans$newName.Ans
 [1] 0.2059746 0.1765568 0.6870228 0.3841037 0.7698414 0.4976992 0.7176185
0.9919061 0.3800352 0.7774452
>
>



On 3/3/07, Matthias Bannert <matthias@mediabar.info> wrote:
>
> hello,
> i have written a function to extract certain lines from a matrix. the
> result is a matrix with 6 cols, named dynamically according to the
> functions arguments.
>
> the problem is now, that i'm not able to return the resultmatrix for
> further use. the object is not being created.
>
> example from my console:
>
> getans(27,27)
> [...]
> [189,] 3969  161   27    1    0    1
> [190,] 2142   87   27    1    0    1
> [191,] 1318   52   27    1    0    1
> [192,] 2881  120   27    1    0    1
> [193,] 3669  152   27    1    0    1
> [194,] 5653  229   27    1    0    1
> [195,] 2308   95   27    1    0    2
> [196,] 4942  200   27    1    0    1
> the matrix ansblock27 contains answers to block 27
> > ansblock27
> error: object "ansblock27" not found
> >
>
>
>
> the code of the function:
>
> 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])))
>
> #create result matrix
> assign(matnam,matrix(as.numeric(as.matrix(allans[(allans[, 3] %in%
> x), , drop = FALSE])),ncol=dim(allans)[2]))
>
> #print info
> cat("the matrix",matnam,"contains answers to",type,as.character(bnr))
>
> }
>
> i have tried to use "return", but had no success.
>
> what else can i do?
>
> thx in advance, and have a nice weekend
> regards,
> matthias
>
>
>
> ________________
> es schreibt Ihnen:
>
> Matthias Bannert
> ---
> mediabar (Off / Bannert GbR)
> Brüelstr.21
> 78462 Konstanz
> fon.: 0711 50 88 72 34
> mobil.: 0179 29 85 630
>
> web: http://www.mediabar.info
> email: matthias@mediabar.info
>
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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.
>
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

	[[alternative HTML version deleted]]

