I've written a program that involves a loop that creates a matrix. I'd like to be able to manipulate that matrix on my R desktop, but after I run the function, that matrix does not appear when I type ls(). How can I make that matrix become an object that I can manipulate? Thanks!
Hi, On Thu, 30 Sep 2004, Jonathan Harris wrote:> I've written a program that involves a loop that creates a matrix. I'd > like to be able to manipulate that matrix on my R desktop, but after I > run the function, that matrix does not appear when I type ls(). How can > I make that matrix become an object that I can manipulate?If I understand you right, you want something like: foo <- function() { goo <- matrix(1:10, nrow = 5, ncol = 2) } then be able to call goo after running foo()? If this is what you want, then you need to do something like: foo <- function() { goo <<- matrix(1:10, nrow = 5, ncol = 2) } to force goo to become a global variable, instead of a local variable to foo(). HTH, Kevin -------------------------------- Ko-Kang Kevin Wang PhD Student Centre for Mathematics and its Applications Building 27, Room 1004 Mathematical Sciences Institute (MSI) Australian National University Canberra, ACT 0200 Australia Homepage: http://wwwmaths.anu.edu.au/~wangk/ Ph (W): +61-2-6125-2431 Ph (H): +61-2-6125-7407 Ph (M): +61-40-451-8301
Jonathan Harris <jharris <at> hss.caltech.edu> writes: : I've written a program that involves a loop that creates a matrix. I'd : like to be able to manipulate that matrix on my R desktop, but after I : run the function, that matrix does not appear when I type ls(). How can : I make that matrix become an object that I can manipulate? Just return the matrix as the value of your function. If you are already returning something else as the value of the function return a list with that value and the matrix as the two components. Check out the builtin eigen function is an example of this as it returns both eigenvalues and eigenvectors. The aforementioned is probably the best strategy but if you must then you could alternately assign the matrix into the parent or global environment from within your function using assign: assign("mat", mat, parent.frame()) assign("mat", mat, .GlobalEnv)
Jonathan Harris wrote:> I've written a program that involves a loop that creates a matrix. I'd > like to be able to manipulate that matrix on my R desktop, but after I > run the function, that matrix does not appear when I type ls(). How can > I make that matrix become an object that I can manipulate?Return it from the function and assign it outside: foo <- function(anything){ TheMatrix <- fun(anything) return(TheMatrix) } result <- foo(something) Now, you have the resulting matrix assigned to an object called "result". Please read "An Introduction to R"! Uwe Ligges> Thanks! > > ______________________________________________ > R-help at 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