Hi R-users I can not change the name of one column only of my matrix. my_matrix <- matrix (1:12,ncol=3) colnames(my_matrix)[1] <- 'myname' Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent thank you for your help Lorenzo [[alternative HTML version deleted]]
Hi Lorenzo, The problem is that my_matrix does not have dimnames. See below. my_matrix <- matrix (1:12,ncol=3) str(my_matrix) ## does not have dimnames dimnames(my_matrix) ## dimnames is NULL colnames(my_matrix) <- "myname" # fails because you are trying to alter the value of something that does not exist ## solution: Set colnames colnames(my_matrix) <- 1:dim(my_matrix)[2] str(my_matrix) # my_matrix now has colnames colnames(my_matrix)[1] <- "myname" # and now we can alter them On Mon, Sep 27, 2010 at 8:26 AM, Lorenzo Cattarino <l.cattarino at uq.edu.au> wrote:> Hi R-users > > > > I can not change the name of one column only of my matrix. > > > > my_matrix <- matrix (1:12,ncol=3) > > > > colnames(my_matrix)[1] <- 'myname' > > > > Error in dimnames(x) <- dn : > > ?length of 'dimnames' [2] not equal to array extent > > > > thank you for your help > > > > Lorenzo > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
Hi, It is because the column names do not exist. If you cast the matrix as a data frame your code would work. jon -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Lorenzo Cattarino Sent: 27. september 2010 10:27 To: r-help at r-project.org Subject: [R] name ONLY one column Hi R-users I can not change the name of one column only of my matrix. my_matrix <- matrix (1:12,ncol=3) colnames(my_matrix)[1] <- 'myname' Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent thank you for your help Lorenzo [[alternative HTML version deleted]] ______________________________________________ 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.
Hi, I'm not sure it's even possible (and if it is I don't know how, but I'm no expert). But I think it doesn't make much sense to have only one named column. Just give it a vector: vect_names <- c("myname1", "myname2", "myname3") colnames(my_matrix) <- vect_names HTH, Ivan Le 9/27/2010 10:26, Lorenzo Cattarino a écrit :> Hi R-users > > > > I can not change the name of one column only of my matrix. > > > > my_matrix<- matrix (1:12,ncol=3) > > > > colnames(my_matrix)[1]<- 'myname' > > > > Error in dimnames(x)<- dn : > > length of 'dimnames' [2] not equal to array extent > > > > thank you for your help > > > > Lorenzo > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. Säugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra@uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php [[alternative HTML version deleted]]