问题
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
回答1:
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
?
回答2:
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,.)
.
回答3:
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
>
来源:https://stackoverflow.com/questions/26279282/sort-r-rows-based-on-the-number-of-repetition