Thanks, this works. I thought the code should put in the for loop, but it turns out an extra line.
On Wed, Jul 27, 2016 at 12:50 PM, <ruipbarra...@sapo.pt> wrote: > Hello, > > Try > > > chars = c('A','B','C','D') > matrix1 = matrix(nrow = length(1:100), ncol = length(1:5)*length(chars)) > k = 0 > for(i in 1:length(1:5)){ # or more simply just for(i in 1:5) > for(j in 1:length(chars)){ > k = k+1 > matrix1[,k] = k > } > } > matrix1 > tmp <- expand.grid(chars, 1:5, stringsAsFactors = FALSE) > nms <- paste(tmp[[2]], tmp[[1]], sep = "-") > rm(tmp) > colnames(matrix1) <- nms > > head(matrix1) > > > Hope this helps, > > Rui Barradas > > > Citando lily li <chocol...@gmail.com>: > > Hi all, > > I want to ask that how to create column names for a matrix. For example, > the matrix below, the column names should be: 1-A, 1-B, 1-C, 1-D, 2-A, 2-B, > 2-C, 2-D, 3-A, etc. Thanks for your help. > > chars = c('A','B','C','D') > matrix1 = matrix(nrow = length(1:100), ncol = length(1:5)*length(chars)) > k = 0 > for(i in seq(1:length(1:5))){ > for(j in seq(1:length(chars))){ > k = k+1 > matrix1[,k] = c(1:100)[k] > } > } > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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.htmland provide commented, > minimal, self-contained, reproducible code. > > > > [[alternative HTML version deleted]] ______________________________________________ R-help@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.