我在数据框中有一个因子。我想从长度最长(大多数条目)到最短长度的那些因素中排序因子的水平(该因子水平的最少条目。
df<- data.frame(x= c("B","B","B","A","A","C","C","C","C"), y= c(
"2", "2", "2","1","1","3","3","3","3"))
我的输出是:
df<- data.frame(x= c("C","C", "C","C","B","B","B","A","A",), y= c(
"3","3","3","3", "2", "2","2","1","1",))
我试过了:
A.data$loc<-sort(length(A.data$loc))
答案 0 :(得分:0)
这是使用dplyr的另一种方式:
require(dplyr)
require(magrittr)
df %>%
group_by(x) %>%
mutate(count = n()) %>%
arrange(desc(count)) %>%
select(-count)
输出结果为:
x y
<fctr> <fctr>
1 C 3
2 C 3
3 C 3
4 C 3
5 B 2
6 B 2
7 B 2
8 A 1
9 A 1
答案 1 :(得分:0)
我们可以使用library(data.table)
setDT(df)[, n := .N, x][order(-n)][, n := NULL][]
# x y
#1: C 3
#2: C 3
#3: C 3
#4: C 3
#5: B 2
#6: B 2
#7: B 2
#8: A 1
#9: A 1
table
或base R
df[with(df, order(factor(x, levels = names(sort(table(x), decreasing = TRUE))))),]
set(gca,'Ycolor',fig.color,'Ycolor',fig.color)