I'm trying to write a function in R that returns a matrix of k columns, where k is a function argument.
The columns are formed inside the function by a loop and I have used (assign(paste ), i) to name them col.1,...,col.i. So far so good, and the code works to generate the column vectors correctly.
Already defined in the function are vectors, col.2 to col.k. X is also defined as equal to col.1.
I have then tried to use a loop within the function, to cbind() the columns, as follows:
for (i in 2:k){
X <- cbind(X, col.i)
}
The error message is "object 'col.i' not found".
I don't have much experience of coding, so it's probably a beginner's mistake, but I'd be grateful for any suggestions.