[R] keep adding elements to the matrix

David Winsemius dwinsemius at comcast.net
Wed Oct 27 17:01:34 CEST 2010


On Oct 27, 2010, at 10:37 AM, Alaios wrote:

> Hello everyone,
> I would like to create a "dynamic" array to keep storing number in it
>
>
> for (i in c(2:length(final))){
>    myarray <-final[i]-final[i-1]
>    myarray2<-2*final[i]
>  }


Because there are no indices on the left hand side of those  
assignments, you are just overwriting those values. If you want  
myarray and myarray2 to actually be vectors, (or matrices or arrays),  
then you need to create them with the proper dimension(s) and then  
index them when assigning into  them.
>
> At the end I would like to use myarray as the x values of an array
> and the myarray2 as the yvalues of the same array.

Perhaps
myarray <- matrix(NA, nrow=length(final)-1, ncol=2)

myarray[ , 1] <- diff(final)
myarray[ , 2] <- 2*final[2:length(final)]


> I tried cbind but it didnot work.
>
> Could you please help me with that?

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list