how to convert R contingency table to a matrix that will be used to merge with other matrix
1
0
Entering edit mode
9.0 years ago
M K ▴ 660

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 • 4.5k views
ADD COMMENT
0
Entering edit mode
9.0 years ago
mkulecka ▴ 360

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")
ADD COMMENT
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode
df$gene_name<-as.factor(df$gene_name)
ADD REPLY

Login before adding your answer.

Traffic: 2357 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6