0
votes

I have a data frame with cell ID in row names and classification in column x.

         x
ACCTTAC  do
ACCGTGG  si
AGTCGGG  si

I also have a matrix with cell IDs in columns and genes in rows.

      ACCTTAC ACCGTGG AGTCGGG
Gene1 4       5       1
Gene2 3       1       0
Gene3 3       3       1

I would like to paste in colnames of the Matrix classification coming from the data frame, see an example below.

       do_ACCTTAC si_ACCGTGG si_AGTCGGG
Gene1  4          5       1
Gene2  3          1       0
Gene3  3          3       1
dput(df) 
structure(list(x = c("do", "si", "si"), row.names = c("ACCTTAC", "ACCGTGG", "AGTCGGG"), class = "data.frame")) 
mat = matrix(c(8,4,5,6,7,9,1,8,9),nrow = 3, ncol = 3,byrow = TRUE) 
mat <- matrix(1:9, nrow = 3,dimnames = list(c("Gene1","Gene2","Gene3"), c("ACCTTAC","ACCGTGG","AGTCGGG"))) 
1
Please, could you paste the formula for creating the data frame and matrix? dataframe <- data.frame(..., that way we can help you better.Manu
dput(df) structure(list(x = c("do", "si", "si"), row.names = c("ACCTTAC", "ACCGTGG", "AGTCGGG"), class = "data.frame"))giegie
mat = matrix(c(8,4,5,6,7,9,1,8,9),nrow = 3, ncol = 3,byrow = TRUE) mat <- matrix(1:9, nrow = 3,dimnames = list(c("Gene1","Gene2","Gene3"), c("ACCTTAC","ACCGTGG","AGTCGGG")))giegie
Please, post the code in your questionManu
Done. I added the code to the question.giegie

1 Answers

1
votes

Les use colnames and rownames according to your data code:

dataframe <- data.frame(x = c("do", "si", "si"), 
                        row.names = c("ACCTTAC", "ACCGTGG", "AGTCGGG")) 
mat <- matrix(c(8,4,5,6,7,9,1,8,9),nrow = 3, ncol = 3,byrow = TRUE) 
mat <- matrix(1:9, nrow = 3,dimnames = list(c("Gene1","Gene2","Gene3"),
 c("ACCTTAC","ACCGTGG","AGTCGGG")))

dataframe$col_names <- row.names(dataframe)
for(i in 1:ncol(mat)) {
  for(j in 1:nrow(dataframe)) {
    if(row.names(dataframe)[j] == colnames(mat)[i])
      colnames(mat)[i] = paste0(dataframe$x[j],"_",dataframe$col_names[j])
  }
}

The result is:

> mat
      do_ACCTTAC si_ACCGTGG si_AGTCGGG
Gene1          1          4          7
Gene2          2          5          8
Gene3          3          6          9