Skip to content
Prev 349964 / 398513 Next

help assigning values to matrix

I think it would be easier to keep track of what you're doing, if you save
the assignment to the very end of your for() loop.  For example ...

# create an empty matrix to be used as a template
Mtemplate <- matrix(data=NA, nrow=13, ncol=3,
    dimnames=list(c(10,20,30,35,40,50,60,70,80,90,100,120,999),
      c("col1", "col2", "col3")))

# create as many 13x3 matrices as vectors I have.
for (j in 1:length(mynames))    {

  # do all your work with the matrix Mnew
  Mnew <- Mtemplate
  # The rowname of the last row will be the length of the vector
  dimnames(Mnew)[[1]][dim(Mnew)[1]] <- length(get(mynames[j]))

  # Example: assign three values in the last row of the created matrices
  Mnew[13, ] <- c(3, 4, 5)

  # then, when you're all done, assign Mnew to the name you want to keep
  Mname <- paste("results", mynames[j], 1, sep="_")
  assign(Mname, Mnew)

}

Jean
On Tue, Apr 14, 2015 at 5:48 AM, David <dasolexa at hotmail.com> wrote: