简体   繁体   中英

Add a name to the column header for an existing matrix in r

I have a headered matrix and I would like to add the word "genes" to the first position in the column header for the matrix, basically attaching that word to the beginning of the column header.

Here is what I have so far: I input a matrix into R,

matrix_a <- read.table(args[1], sep='\t', header=T, row.names=1);

and generate a heatmap from that matrix, using heatmap.2. I then extract the data for the corresponding heatmap using the carpet variable.

Here is the code used to generate the heatmap:

result <- heatmap.2(mtscaled, Rowv=T, scale='none', dendrogram="row", symm = T, col=bluered(16), breaks = my.breaks)

Here I am extracting the values for the clustered matrix, after passing the original matrix through heatmap.2:

new_matrix <- result$carpet
old_name <- colnames(new_matrix)

Here I am trying to attach the name "genes" to the column name

old_name <- cat("genes",old_name)
colnames(new_matrix) <- old_name;
write.table(new_matrix, file="data_result3.txt",sep = " \t",col.names = T, row.names = T);

When I try to attach "genes" to the header using:

old_name <- cat("genes",old_name)

The headers are printed out to the screen properly, but when I examine the result file the vector number is printed:

"V1" "V2" "V3" "V4" "V5" "V6"

Instead I would like the result to look like:

genes Pacs-11 Pacs-2 PC06E7.3 PC49C3.3 Pceh-60 PF52C6.12

In this way genes comes before the rest of the matrix header.

Here is a link to my dataset: Full Dataset

Here is the dataSet after running dput(head(new_matrix)) output of dput

# to have a space between gene and column_name 
old_name <- paste("genes", old_name, sep=" ")

Edit (based on your new comment), perhaps you need:

old_name <- c("genes", old_name)

Here is a trivial example

> test <- matrix(1:50, ncol=5)
> test
      [,1] [,2] [,3] [,4] [,5]
 [1,]    1   11   21   31   41
 [2,]    2   12   22   32   42
 [3,]    3   13   23   33   43
 [4,]    4   14   24   34   44
 [5,]    5   15   25   35   45
 [6,]    6   16   26   36   46
 [7,]    7   17   27   37   47
 [8,]    8   18   28   38   48
 [9,]    9   19   29   39   49
[10,]   10   20   30   40   50
> colnames(test) <- c("genes", paste("V", 1:4))
> test
      genes V 1 V 2 V 3 V 4
 [1,]     1  11  21  31  41
 [2,]     2  12  22  32  42
 [3,]     3  13  23  33  43
 [4,]     4  14  24  34  44
 [5,]     5  15  25  35  45
 [6,]     6  16  26  36  46
 [7,]     7  17  27  37  47
 [8,]     8  18  28  38  48
 [9,]     9  19  29  39  49
[10,]    10  20  30  40  50


# to only add "genes" as the first column's name
colnames(test) <- c("genes", colnames(test)[-1])

I was able to get it to work by printing out the first row with genes and then the rest:

new_matrix <- result$carpet
old_name <- colnames(new_matrix)
sink("data_result3.txt")

cat(c("genes",old_name), "\n")

for (i in 1:nrow(new_matrix))
{
    cat (old_name[i], new_matrix[i,], "\n")
}

sink()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM