I am working on a class project for R-Script and part of it wants me to create a function that takes a square matrix as input and then does a few things with it inside the body of the function itself (such as taking the transpose and finding the inverse and returning those things as output). I don't think I will have much trouble with the body of the function and getting it to perform the necessary tasks with the matrix once I have it, but I am hung up on how to make the function header that takes the required matrix in as input to get me started.
I know how to define my own functions in R (myfunction <- function(some arguments){some tasks}
) and I know how to create a basic square matrix (mymatrix <- matrix(data=c(1,2,3,4), nrow=2))
). I am just confused on how to correctly combine these two syntaxes to get what I am after and was wondering if someone could give me a demo of how to make the right function header. I'm guessing I want to create a variable like sq_matrix
and then assign to it a square matrix using the matrix function, but can I do all of this inside the header of the function or what? Would it be better to create the square matrix outside of the function header and then pass it as an argument afterwards?
I've tried making my own function with a variable assignment within the argument, but it threw me an error. I was expecting this, but it was really the only thing I could think of.