[R] counting occurrence of text in a dataframe

Stefan Grosse singularitaet at gmx.net
Sat May 23 15:48:15 CEST 2009


On Sat, 23 May 2009 12:44:19 +0000 (GMT) Iain Gallagher
<iaingallagher at btopenworld.com> wrote:

IG> I am hoping for some help with a relatively simple problem. I have
IG> a data frame arranged as below. I want to be able to count the
IG> occurrence of each gene (eg let-7e) by Experiment. In other words
IG> how many times does a given gene crop up in the dataframe. 

IG> Tanaka     Mitchell   Wang       Hunter     Chen       Chim       
IG> miR-191*   let-7e     let-7b     miR-126    let-7a     let-7g     
IG> miR-198    let-7f     let-7c     miR-146a   let-7b     let-7i     

Hi Iain,

I would rearrange the dataframe to the following structure:

Gene   Experiment
let-7e Mitchell
...

then you can use ftable.

probably you could use reshape for your data. I could not figure out
how to do it without a time var, so here a quick and dirty way:

exp.wide<-data.frame(Tim=c("a","a","b"),Struppi=c("b","b","b"),More=c("a","b","b"))

nam<-names(exp.wide)
ctL<-length(exp.wide[1,])
ctW<-length(exp.wide[,1])

exp.long<-cbind(as.character(exp.wide[,1]),rep(nam[1],ctW))

for(i in 2:ctL){
   tmp<-cbind(as.character(exp.wide[,i]),nam[i])
   exp.long<-rbind(exp.long,tmp)
}

exp.long<-as.data.frame(exp.long)
names(exp.long)<-c("gene","exp")
ftable(exp.long)


hth
Stefan




More information about the R-help mailing list