Question

I have the following dataframe

GenA<-rep(c("C/C","A/C"),5)
GenB<-rep(c("G/G","G/C"),5)
GenC<-rep(c("A/A","A/T","A/A","A/T","A/A"),2)
Treatment<-rep(c("A","B"),5)
e<-data.frame(GenA,GenB,GenC,Treatment)

With that I would like to calculate at which frequency certain genes are present in a given treatment. So I do:

e$MutA<-ifelse(!(e$GenA=="C/C"),"MutA","")
e$MutB<-ifelse(!(e$GenB=="G/G"),"MutB","")
e$MutC<-ifelse(!(e$GenC=="A/A"),"MutC","")


e$summary<-paste(e$MutA,e$MutB,e$MutC,sep=",")
round(prop.table(ftable(e$summary,e$Treatment),2),digits=2)

Which gives me my desired values:

                  A   B

,,              0.6 0.0
MutA,MutB,      0.0 0.6
MutA,MutB,MutC  0.0 0.4
,,MutC          0.4 0.0

Now I would like to include that in a Latex document via xtable and therefore need to change the rownames to exclude any comma at the beginning of a word. Ideally the output would look like:

                  A   B

no Mut          0.6 0.0
MutA,MutB,      0.0 0.6
MutA,MutB,MutC  0.0 0.4
MutC            0.4 0.0

I don't want to use the rownames command since my underlying dataset is changing and every week a lot of rows are added. Furthermore I am having more columns than posted here. Therefore I am looking for another way of achieving this outcome. Do you have any ideas?

Was it helpful?

Solution

How about this?

tab <- prop.table(ftable(e$summary,e$Treatment),2)
# remove leading commas using a regular expression
attr(tab,'row.vars')[[1]] <- sub('^,+','',attr(tab,'row.vars')[[1]])
# replace entries with no mutations with 'no Mut'
attr(tab,'row.vars')[[1]][attr(tab,'row.vars')[[1]]==''] <- 'no Mut'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top