Hi, Can someone please give me a pointer as to how I can set values of an array? Why does the code below not work? my_array <- array(dim=c(2,2)) my_array[][] = 0 my_array [,1] [,2] [1,] 0 0 [2,] 0 0 for(i in seq(1,2,by=1)){ for(j in seq(1,2,by=1)){ my_array[i][j] = 5 } } Warning messages: 1: In my_array[i][j] = 5 : number of items to replace is not a multiple of replacement length 2: In my_array[i][j] = 5 : number of items to replace is not a multiple of replacement length my_array [,1] [,2] [1,] 5 0 [2,] 5 0 -- View this message in context: http://www.nabble.com/populating-an-array-tp25903190p25903190.html Sent from the R help mailing list archive at Nabble.com.
On Wed, 14 Oct 2009, tdm wrote:> > Hi, > > Can someone please give me a pointer as to how I can set values of an array?See ?Subscript and study the examples. What do you suppose array( 1:4, dim=c(2, 2) )[1][2] is? R is not C. HTH, Chuck> > Why does the code below not work? > > my_array <- array(dim=c(2,2)) > my_array[][] = 0 > my_array > [,1] [,2] > [1,] 0 0 > [2,] 0 0 > > for(i in seq(1,2,by=1)){ > for(j in seq(1,2,by=1)){ > my_array[i][j] = 5 > } > } > > Warning messages: > 1: In my_array[i][j] = 5 : > number of items to replace is not a multiple of replacement length > 2: In my_array[i][j] = 5 : > number of items to replace is not a multiple of replacement length > > my_array > [,1] [,2] > [1,] 5 0 > [2,] 5 0 > > > > -- > View this message in context: http://www.nabble.com/populating-an-array-tp25903190p25903190.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Could you possibly consider reading "An Introduction to R", especially the first few pages of chapter 5, and also a bit about vectors from chapter 2, and maybe eventually even some other parts... You have x[i][j] where length(i)==1, so x[i] will be a single element. Having [j] there makes no sense except if j==1 (in that case, it makes no difference). Why would you ask for a second element of a vector of length 1? K On Thu, Oct 15, 2009 at 8:23 AM, tdm <philb@philbrierley.com> wrote:> > Hi, > > Can someone please give me a pointer as to how I can set values of an > array? > > Why does the code below not work? > > my_array <- array(dim=c(2,2)) > my_array[][] = 0 > my_array > [,1] [,2] > [1,] 0 0 > [2,] 0 0 > > for(i in seq(1,2,by=1)){ > for(j in seq(1,2,by=1)){ > my_array[i][j] = 5 > } > } > > Warning messages: > 1: In my_array[i][j] = 5 : > number of items to replace is not a multiple of replacement length > 2: In my_array[i][j] = 5 : > number of items to replace is not a multiple of replacement length > > my_array > [,1] [,2] > [1,] 5 0 > [2,] 5 0 > > > > -- > View this message in context: > http://www.nabble.com/populating-an-array-tp25903190p25903190.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > 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]]
R doesn't access arrays like C, use [i,j] to access a 2-d array, e.g.:> my_array <- array(0,dim=c(2,2)) > for(i in seq(1,2,by=1)){+ for(j in seq(1,2,by=1)){ + my_array[i,j] = i+j + } + }> > my_array[,1] [,2] [1,] 2 3 [2,] 3 4>tdm wrote:> Hi, > > Can someone please give me a pointer as to how I can set values of an array? > > Why does the code below not work? > > my_array <- array(dim=c(2,2)) > my_array[][] = 0 > my_array > [,1] [,2] > [1,] 0 0 > [2,] 0 0 > > for(i in seq(1,2,by=1)){ > for(j in seq(1,2,by=1)){ > my_array[i][j] = 5 > } > } > > Warning messages: > 1: In my_array[i][j] = 5 : > number of items to replace is not a multiple of replacement length > 2: In my_array[i][j] = 5 : > number of items to replace is not a multiple of replacement length > > my_array > [,1] [,2] > [1,] 5 0 > [2,] 5 0 > > >