Md Desa, Zairul Nor Deana Binti
2011-Oct-24 23:23 UTC
[R] Create a matrix with increment and element with zero subscript
Hello, Does anyone knows how to deal with zero subscript in R. I have this code: for (i in 1:nitems){ + for (j in 1:ncat-1) { + draw<-matrix(rnorm(nitems*(ncat-1),seed1,seed2),nitems,(ncat-1)) + d<-( sigma_d*draw ) + mu_d + draw<-matrix(rtnorm((nitems*(ncat-1)),mean = seed1, sd = seed2, lower = .1, upper = 1.5),nitems,(ncat-1)) + d<-(sigma_d*draw) + mu_d + write.matrix(cbind(b.i0,d), file = "F:/KU/MIRT group/MIMIC-DIF/R/cpprcode/b0d.dat", sep = " ") + b[i,j]<-b[i,j-1]+d[i,j] + } + } The error as following: Error in b[i, j ] <- b[i, j - 1] + d[i, j] : replacement has length zero I would like to to have a matrix where the first column takes from initial pre-assigned value (b[i,0]+d1), the second column is additive from the first column and a constant d2 that is (b(i,1)=b(i,0)+d1+d2, and so forth. Is there any way that R can read subscript of zero? Thanks, Deana
Joshua Wiley
2011-Oct-25 00:01 UTC
[R] Create a matrix with increment and element with zero subscript
Hi Deana, No, R does not deal with zero subscripts. Could it be done? Of course. The simplest approach is: 0 + 1 = 1, which is the R equivalent. You need to adjust your code to go, for example, from 1 to 30 instead of 0 to 29. Cheers, Josh On Mon, Oct 24, 2011 at 4:23 PM, Md Desa, Zairul Nor Deana Binti <zndeana at ku.edu> wrote:> Hello, > Does anyone knows how to deal with zero subscript in R. I have this code: > for (i in 1:nitems){ > + ? ? ? ? ? ? for (j in 1:ncat-1) ?{ > + ? ? ? ? ?draw<-matrix(rnorm(nitems*(ncat-1),seed1,seed2),nitems,(ncat-1)) > + ? ? ? ? ?d<-( sigma_d*draw ) + mu_d > + ? ? ? ? draw<-matrix(rtnorm((nitems*(ncat-1)),mean = seed1, sd = seed2, lower = .1, upper = 1.5),nitems,(ncat-1)) > + ? ? ? ? ? d<-(sigma_d*draw) + mu_d > + ? ? ? ? ? write.matrix(cbind(b.i0,d), file = "F:/KU/MIRT group/MIMIC-DIF/R/cpprcode/b0d.dat", sep = " ?") > + ? ? ? ? ? ? b[i,j]<-b[i,j-1]+d[i,j] > + ? ? ? ? ? ? ? ?} > + ? ? ? ? ? ? } > > The error as following: > > Error in b[i, j ] <- b[i, j - 1] + d[i, j] : > ?replacement has length zero > > I would like to to have a matrix where the first column takes from initial pre-assigned value (b[i,0]+d1), the second column is additive from the first column and a constant d2 that is (b(i,1)=b(i,0)+d1+d2, and so forth. Is there any way that R can read subscript of zero? > > Thanks, > Deana > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, ATS Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/
David Winsemius
2011-Oct-25 00:02 UTC
[R] Create a matrix with increment and element with zero subscript
On Oct 24, 2011, at 7:23 PM, Md Desa, Zairul Nor Deana Binti wrote:> Hello, > Does anyone knows how to deal with zero subscript in R. I have this > code: > for (i in 1:nitems){ > + for (j in 1:ncat-1) { > + draw<-matrix(rnorm(nitems*(ncat-1),seed1,seed2),nitems, > (ncat-1)) > + d<-( sigma_d*draw ) + mu_d > + draw<-matrix(rtnorm((nitems*(ncat-1)),mean = seed1, sd = > seed2, lower = .1, upper = 1.5),nitems,(ncat-1)) > + d<-(sigma_d*draw) + mu_d > + write.matrix(cbind(b.i0,d), file = "F:/KU/MIRT group/ > MIMIC-DIF/R/cpprcode/b0d.dat", sep = " ") > + b[i,j]<-b[i,j-1]+d[i,j] > + } > + } > > The error as following: > > Error in b[i, j ] <- b[i, j - 1] + d[i, j] : > replacement has length zero > > I would like to to have a matrix where the first column takes from > initial pre-assigned value (b[i,0]+d1), the second column is > additive from the first column and a constant d2 that is (b(i,1)=b(i, > 0)+d1+d2, and so forth. Is there any way that R can read subscript > of zero?I don't. (I seem to remember the Matrix package allows such shenanigans.) Why don't you instead augment b[,] with a top column of initial values, using rbind() and then iterate from 2:(ncat-2). (Do note the parentheses and if you do not know why, then consult ?Syntax for operatr precendence.) -- David Winsemius, MD West Hartford, CT
Reasonably Related Threads
- set seed for random draws
- How to seed the R random number generator in C (standalone) with an instance of .Random.seed
- portable parallel seeds project: request for critiques
- Do I need to transform backtest returns before using pbo (probability of backtest overfitting) package functions?
- Do I need to transform backtest returns before using pbo (probability of backtest overfitting) package functions?