R colnames Function


colnames() function retrieves or sets the column names of matrix.

colnames(x, do.NULL = TRUE, prefix = "col")
colnames(x) <- value


x: matrix
do.NULL: logical. Should this create names if they are NULL?
prefix: for created names
value: a valid value for that component of dimnames(x)

Following is a csv file example:



Let first read in the data from the file:

> x <- read.csv("matrix.csv",header=T,sep=",")
> colnames(x)

[1] "Expression" "Quality"    "Height"


> x <- as.matrix(BOD)
> x

Time demand
[1,] 1 8.3
[2,] 2 10.3
[3,] 3 19.0
[4,] 4 16.0
[5,] 5 15.6
[6,] 7 19.8


> is.matrix(x)

[1] TRUE


> colnames(x)

[1] "Time" "demand"


Change the column names:

> colnames(x) <- c("No.","Value")
> x

No. Value
[1,] 1 8.3
[2,] 2 10.3
[3,] 3 19.0
[4,] 4 16.0
[5,] 5 15.6
[6,] 7 19.8


If do.NULL=FALSE and the names are NULL, prefix argument can be used to help create the names.

> colnames(cbind(1:6,5))
NULL
> colnames(cbind(1:6,5),prefix="tp",do.NULL=FALSE)
[1] "tp1" "tp2"




endmemo.com © 2024  | Terms of Use | Privacy | Home