hassen62 at voila.fr
2007-Aug-22 00:33 UTC
[R] rectify a program of seasonal dummies matrix
Hi friends, I would like to construct a matrix of seasonal dummies with number of rows (observations)=100. such matrix is written as follows:[1 0 0 0;0 1 0 0;0 0 1 0;0 0 0 1;1 0 0 0;0 1 0 0;0 0 1 0;0 0 0 1;etc...] . I wrote the following program: T=100 br=matrix(0,T,4) { for (i in 1:T) for (j in 1:4) if i==j br[i,j]=1 if else (abs(i-j)%%4==0 br[i,j]=1 else br[i,j]=0 } z<-br z but unfortunately I obtained from the console the following message:> {+ for (i in 1:T) + for (j in 1:4) + (if i==j) Erreur : syntax error, unexpected SYMBOL, expecting '(' dans : " "> br[i,j]=1Erreur dans br[i, j] = 1 : objet "i" non trouvé> > (if else (abs(i-j)%%4==0)Erreur : syntax error, unexpected ELSE, expecting '(' dans " (if else"> br[i,j]=1Erreur dans br[i, j] = 1 : objet "i" non trouvé> elseErreur : syntax error, unexpected ELSE dans " else"> br[i,j]=0Erreur dans br[i, j] = 0 : objet "i" non trouvé> }Erreur : syntax error, unexpected '}' dans " }">Can you please rectify my smal program, I tried to rectify it but I can't. Many thanks in advance. [[alternative HTML version deleted]]
Your syntax is wrong; e.g., if i==j should be if (i == j) same with your use of 'if else'. You need to use the correct syntax. Your example is hard to follow without the correct indentation since you are using the incorrect syntax. On 8/21/07, hassen62 at voila.fr <hassen62 at voila.fr> wrote:> Hi friends, > I would like to construct a matrix of seasonal dummies with number of rows (observations)=100. such matrix is written as follows:[1 0 0 0;0 1 0 0;0 0 1 0;0 0 0 1;1 0 0 0;0 1 0 0;0 0 1 0;0 0 0 1;etc...] . I wrote the following program: > T=100 > br=matrix(0,T,4) > { > for (i in 1:T) > for (j in 1:4) > if i==j > br[i,j]=1 > if else (abs(i-j)%%4==0 > br[i,j]=1 > else > br[i,j]=0 > } > z<-br > z > > but unfortunately I obtained from the console the following message: > > { > + for (i in 1:T) > + for (j in 1:4) > + (if i==j) > Erreur : syntax error, unexpected SYMBOL, expecting '(' dans : > " > " > > br[i,j]=1 > Erreur dans br[i, j] = 1 : objet "i" non trouv? > > > > (if else (abs(i-j)%%4==0) > Erreur : syntax error, unexpected ELSE, expecting '(' dans " (if else" > > br[i,j]=1 > Erreur dans br[i, j] = 1 : objet "i" non trouv? > > else > Erreur : syntax error, unexpected ELSE dans " else" > > br[i,j]=0 > Erreur dans br[i, j] = 0 : objet "i" non trouv? > > } > Erreur : syntax error, unexpected '}' dans " }" > > > Can you please rectify my smal program, I tried to rectify it but I can't. Many thanks in advance. > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help at stat.math.ethz.ch 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. > >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Hello, the main problem seems to be the "if else", should be "else if". Your code is hard to read, maybe you should consider using more () {}: T <- 100; br <- matrix(0,T,4); for (i in 1:T) { for (j in 1:4) { if (i==j) { br[i,j] <- 1; } else if ((abs(i-j)%%4)==0) { br[i,j] <- 1; } else { br[i,j] <- 0; } } } A simpler approach is creating a diagonal matrix and multply it : # create small diagonal matrix mat = diag(x=1, nrow=4, ncol=4); mat # multiply diagonal matrix and re-dimension it to 4 cols br <- rep(mat, 25); dim(br) <- c(100, 4); br; Hope this helps, FS -- Friedrich Schuster mail at friedrich-schuster.de
Friedrich Schuster wrote:> Hello, > > the main problem seems to be the "if else", should be "else if". > > Your code is hard to read, maybe you should consider using more () {}: > > T <- 100; > br <- matrix(0,T,4);Thanks for the contribution. Please note: a) It is a bad idea to have a variable called T. Some people still use it as a logical value even if they should not. b) R does not need any ";" at the end of a line. Uwe Ligges> for (i in 1:T) { > for (j in 1:4) { > if (i==j) { > br[i,j] <- 1; > } > else if ((abs(i-j)%%4)==0) { > br[i,j] <- 1; > } > else { > br[i,j] <- 0; > } > } > } > > A simpler approach is creating a diagonal matrix and multply it : > > # create small diagonal matrix > mat = diag(x=1, nrow=4, ncol=4); > mat > # multiply diagonal matrix and re-dimension it to 4 cols > br <- rep(mat, 25); > dim(br) <- c(100, 4); > br; > > Hope this helps, > FS >