This is a test version of Biostars. For the public version, visit https://www.biostars.org.
how to convert R contingency table to a matrix that will be used to merge with other matrix

Dear All,

I created a contingency table between gene names and repeats count in each gene as shown below:

"(CA)n" "(TG)n" "AT_rich" "B1_Mm" "B1_Mur1" "B1_Mur2"
"0610005C13Rik" 0 0 1 0 0 0
"0610007N19Rik" 0 0 0 0 0 0
"0610007P14Rik" 0 0 1 0 1 0
"0610008F07Rik" 0 0 0 0 0 0
"0610009B14Rik" 0 1 0 0 1 0

Now I need to merge this file with another file (data frame) using the gene name as a reference in both files.In the other file which is as data frame I have the gene name in the first column but in the file above which is a contingency table, I couldn't use the gene name because it doesn't appear as a separate column. so how can I convert this contingency table to normal count table with gene names in the first column and repeats counts in the other columns.

r

1 answer

I came up with a bit convoluted solution:

class(your_table)<-"matrix"
df<-as.data.frame(your_table)
df$gene_name<-rownames(df)
merged_df<-merge(df,your_other_data_frame,by="gene_name")

Thanks for helping me, but there is one problem which is in the R code above the gene name will treated as a character while in my other file it is treated as a factor, so is there any way to convert it to be a factor.

df$gene_name<-as.factor(df$gene_name)

Log in to answer this question.