Dear list! I would like to write a function to transform matrix, which is input argument of a written function. It is easy with new matrix (see below), but my idea is to transform input argument (matrix) of function without any additional matrixes. Here is an example: fun1 <- function(xy) { xy <- cbind(xy[,1], xy[,2], xy[,1] + xy[,2]) return(xy) } df1 <- matrix(c(1,2,3,1,2,3), ncol = 2) fun1(df1) fun2 <- function(xy) { xy <- cbind(xy[,1], xy[,2], xy[,1] + xy[,2]) return(invisible(xy)) } fun2(df1) df1> should be[,1] [,2] [,3] [1,] 1 1 2 [2,] 2 2 4 [3,] 3 3 6 Thanks to all for help, OV [[alternative HTML version deleted]]
R. Michael Weylandt
2012-Nov-17 15:46 UTC
[R] transform input argument (matrix) of function
I'm afraid I don't really understand what you're searching for, but note that your functions can be written without assignment/return: fun3 <- function(x) invisible(cbind(xy[,1], xy[,2], xy[,1] + xy[,2])) Cheers, Michael On Sat, Nov 17, 2012 at 3:41 PM, Omphalodes Verna <omphalodes.verna at yahoo.com> wrote:> Dear list! > > I would like to write a function to transform matrix, which is input argument of a written function. It is easy with new matrix (see below), but my idea is to transform input argument (matrix) of function without any additional matrixes. Here is an example: > > fun1 <- function(xy) { > xy <- cbind(xy[,1], xy[,2], xy[,1] + xy[,2]) > return(xy) > } > > df1 <- matrix(c(1,2,3,1,2,3), ncol = 2) > fun1(df1) > > fun2 <- function(xy) { > xy <- cbind(xy[,1], xy[,2], xy[,1] + xy[,2]) > return(invisible(xy)) > } > > fun2(df1) > df1 >> should be > [,1] [,2] [,3] > [1,] 1 1 2 > [2,] 2 2 4 > [3,] 3 3 6 > > Thanks to all for help, > OV > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help at r-project.org 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. >
Hello, Your function fun2 (in fact both of them) changes a _copy_ of df1, not df1 itself. To have a copy of the output of fun2 you need to assign the value of fun2 outside it. df2 <- fun2(df1) Now df2 has what you want. (If I understand it well.) Hope this helps, Rui Barradas Em 17-11-2012 15:41, Omphalodes Verna escreveu:> Dear list! > > I would like to write a function to transform matrix, which is input argument of a written function. It is easy with new matrix (see below), but my idea is to transform input argument (matrix) of function without any additional matrixes. Here is an example: > > fun1 <- function(xy) { > xy <- cbind(xy[,1], xy[,2], xy[,1] + xy[,2]) > return(xy) > } > > df1 <- matrix(c(1,2,3,1,2,3), ncol = 2) > fun1(df1) > > fun2 <- function(xy) { > xy <- cbind(xy[,1], xy[,2], xy[,1] + xy[,2]) > return(invisible(xy)) > } > > fun2(df1) > df1 >> should be > [,1] [,2] [,3] > [1,] 1 1 2 > [2,] 2 2 4 > [3,] 3 3 6 > > Thanks to all for help, > OV > [[alternative HTML version deleted]] > > > > ______________________________________________ > R-help@r-project.org 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.[[alternative HTML version deleted]]
On Nov 17, 2012, at 7:41 AM, Omphalodes Verna wrote:> Dear list! > > I would like to write a function to transform matrix, which is input > argument of a written function. It is easy with new matrix (see > below), but my idea is to transform input argument (matrix) of > function without any additional matrixes. Here is an example: > > fun1 <- function(xy) { > xy <- cbind(xy[,1], xy[,2], xy[,1] + xy[,2]) > return(xy) > } > > df1 <- matrix(c(1,2,3,1,2,3), ncol = 2) > fun1(df1) > > fun2 <- function(xy) { > xy <- cbind(xy[,1], xy[,2], xy[,1] + xy[,2]) > return(invisible(xy)) > } > > fun2(df1) > df1 >> should beNo, it should not be a three column matrix. When you call a function with an appropriate argument and fail fail to assign it to anything, the result evaporates. ?'<-' ?assign Functions generally do not modify objects "in place". If you wanted to have df1 be the result of fun2(df1) you would have needed to do this: df1 <- fun2(df1) There are ways to get around that limitation (described in each of those help pages), but I think it is better for one to learn ordinary functional programming conventions first.> [,1] [,2] [,3] > [1,] 1 1 2 > [2,] 2 2 4 > [3,] 3 3 6 > > Thanks to all for help, > OV > [[alternative HTML version deleted]]This persistent practice after multiple postings to Rhelp marks you as someone either having difficulty in reading the Posting Guide.> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html-- David Winsemius, MD Alameda, CA, USA