[R] help assigning values to matrix

Adams, Jean jvadams at usgs.gov
Tue Apr 14 13:16:31 CEST 2015


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:

>
>
> Hi group,
>
> I am automatically creating several matrices to store results from
> different analyses on vectors of different lengths. The matrices are named
> according  to each vector, so I can trace back results. I am using the
> following commands, which give me an error. My idea is to populate the for
> loop to include several steps and keep adding results to the matrices. But
> to start with:
>
> > ls()
> [1] "PS013_1" "PS056_1" "PS058_1" "PS080_1" "PS117_1" "PS193_1" "PS194_1"
>
> > mynames<- c("PS013","PS056","PS058","PS080","PS117","PS193","PS194")
>
> > for (j in 1:length(mynames))    {
>
> #create as many 13x3 matrices as vectors I have. The rowname of the last
> row will be the length of the vector
> >
> assign(paste("results",mynames[j],"1",sep="_"),matrix(data=NA,nrow=13,ncol=3,dimnames
> =
> list(c(10,20,30,35,40,50,60,70,80,90,100,120,print(length(get(paste(mynames[j],1,sep="_"))))),c("col1","col2","col3"))))
>
> # Example: assign three values in the last row of the created matrices
> >assign(paste("results",mynames[j],"1",sep="_")[13,],c(3,4,5))
>
> }
>
> Error in paste("results", mynames[j], "1", sep = "_")[13, ] :
>   incorrect number of dimensions
>
>
> I have noticed that to access the positions of a matrix I cannot use
> [row,column] coordinates, but actual "count" positions like:
>
> #let's write something  in one of the matrices, since they are all NAs
> > results_PS013_1[1,]<-c(1,14,27)
> > get(paste("results",mynames[1],"1",sep="_"))
>     col1 col2     col3
> 10  1 14 27
> 20  NA     NA      NA
> 30  NA     NA      NA
> 35  NA     NA      NA
> 40  NA     NA      NA
> 50  NA     NA      NA
> 60  NA     NA      NA
> 70  NA     NA      NA
> 80  NA     NA      NA
> 90  NA     NA      NA
> 100 NA     NA      NA
> 120 NA     NA      NA
> 295 NA     NA      NA
>
> > get(paste("results",mynames[1],"1",sep="_"))[1]
> [1] 1
> > get(paste("results",mynames[1],"1",sep="_"))[2]
> [1] NA
> > get(paste("results",mynames[1],"1",sep="_"))[14]
> [1] 14
> > get(paste("results",mynames[1],"1",sep="_"))[c(1,14,27)]
> [1] 1    14   27
>
> So if I try to write three other values to the first row of the first
> matrix, I now try the following
>
> > assign(get(paste("results",mynames[1],"1",sep="_"))[c(1,14,27)],c(3,4,5))
> Error in assign(get(paste("results", mynames[1], "1", sep = "_"))[c(1,  :
>   invalid first argument
>
>
> Can anyone explain to me why I cannot assign the values in this way and
> how is it that I cannot use [row,column] coordinates?
>
> Thanks in advance for your help
>
> Dave
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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]]



More information about the R-help mailing list