[R] colnames

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Mon Jul 18 18:03:00 CEST 2005


This normally happens to me when I read in a table where the rownames
will be appended by an "X". Read help(make.names) for more information. 
Remember that R is primarily a statistical software and thus likes
colnames classes to be characters.

 mat1 <- matrix( 1:12, nc=3, dimnames=list(NULL, c(0,1,2)) )
 mat1
     0 1  2
[1,] 1 5  9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
 colnames(mat1)
[1] "0" "1" "2"
 is.character( colnames(mat1) )
[1] TRUE

However I am not able to reproduce what your problem

  mat2 <- matrix( 101:108, nc=2, dimnames=list(NULL, c("A", "B")) )
  mat2
        A   B
 [1,] 101 105
 [2,] 102 106
 [3,] 103 107
 [4,] 104 108

 cbind(mat1, mat2)
      0 1  2   A   B
 [1,] 1 5  9 101 105
 [2,] 2 6 10 102 106
 [3,] 3 7 11 103 107
 [4,] 4 8 12 104 108

I tried other operation such as mat1[ , 1:2] + mat2 and 
mat1[ ,1] <- mat2[ ,2] but it does not add a preceding "X".
Can you give a reproducible example please ?

If you want to get rid of the preceding "X", try

 colnames( mat1 ) <- c("X0", "X1", "X2") 
 colnames( mat1 ) <- gsub("^X", "", colnames(mat1))

Why do want to do this anyway ?

Regards, Adai


On Mon, 2005-07-18 at 16:11 +0100, Gilbert Wu wrote:
> Hi,
>  
> I have a matrix with column names starting with a character in [0-9]. After some matrix operations (e.g. copy to another matrix), R seems to add a character 'X' in front of the column name. Is this a normal default behaviour of R? Why has it got this behaviour? Can it be changed? What would be the side effect?
>  
> Thank you.
>  
> Regards,
>  
> Gilbert
> 
> 	[[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
>




More information about the R-help mailing list