[R] name ONLY one column

Ista Zahn izahn at psych.rochester.edu
Mon Sep 27 16:53:15 CEST 2010


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



More information about the R-help mailing list