0

Does anyone know how to create a matrix in a function, which can later be used outside of it? I want the matrix to have the name that I use in the argument. This is my code so far:

newacc <- function(accname){
   accname <<- matrix(data=0, nrow=1, ncol=2)
   colnames(accname) <- c("DEBIT", "CREDIT")
   return(accname)
   }

So I want the input I give for the argument accname to be the name of the matrix that is created outside of the function. Currently I can only create a matrix outside of the function if I give the input accname=accname.

I would be very grateful for any kind of help :)

Thank you

cholz
  • 83
  • 2
  • 12
  • you may need `assign` i.e. `assign(accname, value = matrix(data=0, nrow=1, ncol=2, dimnames = list(NULL, c("DEBIT", "CREDIT"))), envir = parent.frame())` inside the function – akrun Sep 26 '16 at 17:47
  • 1
    Bad idea to have dependent objects in different environments – Rich Scriven Sep 26 '16 at 17:49
  • You are already `return`ing the matrix - you just need to assign it outside the function (and not worry about having the function name it). `accname <- newacc()`. ... your function gets simplified to `newacc = function() {m <- matrix(data = 0, nrow = 1, ncol = 2); colnames(m) <- c("DEBIT", "CREDIT"); return(m)}` – Gregor Thomas Sep 26 '16 at 19:56
  • Thank you for your answer! The main idea behind the function is that the function creates and names the matrix. I know I can rename it outside, but the idea is to create a really simple book-keeping tool in R, which has a good workflow. – cholz Sep 27 '16 at 10:35

1 Answers1

0

It might be useful to provide some context on why you are hoping to assign variable names within a function environment, because there might be another strategy to employ.

If you are mainly trying to avoid the standard assignment structure, there is an alternative that doesn't require having dependent objects in different environments.

Instead of

accname <- newacc(accname)

you can use assign() like so:

assign("accname", newacc(accname))

EDIT:

I might be reading this wrong, but I want to make sure that you know that it doesn't really matter what the name of the variable is inside of the function. If you want to re-use the same function, but have the output variable names have different names, then that is something you do when you call the function.

Try running the following code in a new R session, and let me know if this is the type of functionality you're looking for. Notice how the matrix you're returning is called acc within the function, but when you actually call the function the new variable is just whatever you told the function to return to.

newacc <- function(x){
   acc <- matrix(data=x, nrow=1, ncol=2)
   colnames(acc) <- c("DEBIT", "CREDIT")
   return(acc)
}

accname <- newacc(0)

accname2 <- newacc(10)

accname3 <- newacc(20) 
jmartindill
  • 260
  • 1
  • 8
  • Thank you for your answer! I want to write a simple book-keeping tool. The user should be able to write newacc(assets) to open a new t-account with debit and credit columns. The problem is that the matrix that is created in the global environment should have the name given as variable in the function. – cholz Sep 27 '16 at 10:27
  • So the idea is to overwrite the input variable with a matrix? – jmartindill Sep 27 '16 at 16:57
  • More or less. Or to create a matrix with the name of the input variable. – cholz Sep 27 '16 at 18:49
  • Is the input variable coming from the global environment? – jmartindill Sep 27 '16 at 19:54
  • Nope, the first argument of the function should be the name of the matrix - the variable isn't in the environment beforehand. This function should just create a matrix with certain attributes and I don't want to have to rename it every single time to optimize workflow. Thank you for helping! – cholz Sep 29 '16 at 08:38
  • I don't totally understand why you need to assign the output variable name via an input to the function. See my edit above. If this answers your question, great. Otherwise, I could use a bit more clarification on why you prefer `newacc(accname)` over `accname <- newacc()` – jmartindill Sep 29 '16 at 17:34
  • Thank you again! I just wanted to code a free simple book-keeping package that people without prior R-knowledge could use easily. Then, if they choose to, they can dive into statistical analysis with R - their business' data is already there :-) – cholz Sep 30 '16 at 11:47
  • In my option, that's all the more reason to have the function work in a 'normal' way. If it's going to be somebody's first introduction to R, it's probably better that they learn it the "right" way. And I don't feel like it's much more complicated to do it the right way, in this case. That said, if you're set on doing things this way, [check out this previous question](http://stackoverflow.com/questions/1236620/global-variables-in-r) – jmartindill Sep 30 '16 at 16:51