0

I have a table like:

    me    mine    
1   z       ghm     
2   d       gwm
3   d       gom
4   d       gum
5   f       gom
6   g       gum
7   h       gom
8   t       ghm
9   y       gom
10  u       gom

how can I sort these data based on the repetition in mine clumn, note: all start with "g" and end with "m". the result im looking for is like"

     me    mine    
1   d       gom     
2   f       gom
3   h       gom
4   y       gom
5   u       gom
6   d       gum
7   g       gum
8   t       ghm
9   z       ghm
10  d       gwm

or somthing like this:

gom    d,f,h,y,u    
gum    d,g,    
ghm    t,z     
gwm      z,d 
Jerodev
  • 32,252
  • 11
  • 87
  • 108

3 Answers3

0

Try this (df is your data.frame):

   indices<-match(df$mine,names(sort(table(df$mine),decreasing=TRUE)))
   df[order(indices,df$me),] 

There is a reason you want gum before ghm?

nicola
  • 24,005
  • 3
  • 35
  • 56
0

Just to add another one-liner:

split(df, df$mine)[order(sapply(split(df, df$mine), NROW), decreasing = TRUE)]

If you want to have it in data.frame format, just add a do.call(rbind,.).

thothal
  • 16,690
  • 3
  • 36
  • 71
-1

What about something like that

> z = c('a', 'b', 'c', 'b', 'b', 'z','a')
> sort(table(z))
z
c z a b 
1 1 2 3 
> 
Donbeo
  • 17,067
  • 37
  • 114
  • 188