对于该数据集,我想按照从最大到最小的顺序,按相应的频率对Var1
进行排序,并按行顺序排在前5位。我一直在使用rank()
,sort()
和order()
函数,但无济于事。
Var1 Freq
2 Moderate 33
3 Luxury 31
4 Couples 31
5 Families with Children 33
6 Nightlife 23
7 Europe 60
8 Architecture 23
9 Drink 58
10 Northern Europe 27
11 Skiing 29
理想情况下,我希望最终输出为:
Var1 Freq
7 Europe 60
9 Drink 58
5 Families with Children 33
2 Moderate 33
3 Luxury 31
当我使用上述函数时,R返回一系列数字,这些数字要么是乱码,要么只会按排名顺序返回Freq
列。
答案 0 :(得分:1)
这是一个dplyr
解决方案。
df %>% top_n(5, Freq) %>% arrange(-Freq)
这将按顺序为您提供前5个得分。
# Var1 Freq
# 1 Europe 60
# 2 Drink 58
# 3 Moderate 33
# 4 Families with Children 33
# 5 Luxury 31
# 6 Couples 31
请注意,由于平局,其中包括6个条目。
如果您只希望排名前5名,而无需打领带,则可以使用以下方法:
df %>% arrange(-Freq) %>% filter(row_number() <= 5)
# Var1 Freq
# 1 Europe 60
# 2 Drink 58
# 3 Moderate 33
# 4 Families with Children 33
# 5 Luxury 31
答案 1 :(得分:0)
这里是单线。它使用order
和head
。
head(dat[order(dat$Freq, decreasing = TRUE), ], 5)
# Var1 Freq
#7 Europe 60
#9 Drink 58
#2 Moderate 33
#5 Families with Children 33
#3 Luxury 31
数据。
dat <-
structure(list(Var1 = structure(c(7L, 6L, 2L, 5L, 8L, 4L, 1L,
3L, 9L, 10L), .Label = c("Architecture", "Couples", "Drink",
"Europe", "Families with Children", "Luxury", "Moderate", "Nightlife",
"Northern Europe", "Skiing"), class = "factor"), Freq = c(33L,
31L, 31L, 33L, 23L, 60L, 23L, 58L, 27L, 29L)), .Names = c("Var1",
"Freq"), class = "data.frame", row.names = c("2", "3", "4", "5",
"6", "7", "8", "9", "10", "11"))
答案 2 :(得分:0)
dat <- structure(list(Var1 = structure(c(7L, 6L, 2L, 5L, 8L, 4L, 1L, 3L, 9L, 10L), .Label = c("Architecture", "Couples", "Drink",
"Europe", "Families with Children", "Luxury", "Moderate", "Nightlife",
"Northern Europe", "Skiing"), class = "factor"), Freq = c(33L,
31L, 31L, 33L, 23L, 60L, 23L, 58L, 27L, 29L)), .Names = c("Var1",
"Freq"), class = "data.frame", row.names = c("2", "3", "4", "5",
"6", "7", "8", "9", "10", "11"))
使用data.table
。
library(data.table)
DFDT <- as.data.table(dat)
DFDT[order(-Freq)][1:5]
Var1 Freq
1: Europe 60
2: Drink 58
3: Moderate 33
4: Families with Children 33
5: Luxury 31