根据列中的值为每个组选择前N行

时间:2017-07-10 08:02:35

标签: r loops dataframe dplyr top-n

我的数据框如下: -

x<-c(3,2,1,8,7,11,10,9,7,5,4)
y<-c("a","a","a", "b","b","c","c","c","c","c","c")
z<-c(2,2,2,1,1,3,3,3,3,3,3)
df<-data.frame(x,y,z)

df
    x y z
1   3 a 2
2   2 a 2
3   1 a 2
4   8 b 1
5   7 b 1
6  11 c 3
7  10 c 3
8   9 c 3
9   7 c 3
10  5 c 3
11  4 c 3

我想按列y为每个组选择前n行,其中n在列z中提供。 所以输出应该是:

output:
       x   y  z
     1 3   a  2
     2 2   a  2
     3 8   b  1
     4 11  c  3
     5 10  c  3
     6 9   c  3

4 个答案:

答案 0 :(得分:2)

基础R的解决方案:

# df is split according to y, then we keep only the top "z" value (after ordering x) 
# and rbind everything back together:
do.call(rbind, 
        lapply(split(df, df$y), 
               function(df1) df1[order(df1$x, decreasing=TRUE), ][1:unique(df1$z), ]))
#     x y z
#a.1  3 a 2
#a.2  2 a 2
#b    8 b 1
#c.6 11 c 3
#c.7 10 c 3
#c.8  9 c 3

修改
@ mt1022评论提供了一种更直接的方式(仍在基础R中):

df[ave(1:nrow(df), df$y, FUN = seq_along) <= df$z, ]
#   x y z
#1  3 a 2
#2  2 a 2
#4  8 b 1
#6 11 c 3
#7 10 c 3
#8  9 c 3

答案 1 :(得分:1)

使用data.table的一种方法:

library(data.table)
setDT(df)
df[,.(inc=seq_len(.N)<=z,x,z),by=.(y)][inc==T ,-2]
#   y  x z
#1: a  3 2
#2: a  2 2
#3: b  8 1
#4: c 11 3
#5: c 10 3
#6: c  9 3

答案 2 :(得分:0)

dplyr使用do的解决方案:

df %>%
   group_by(y) %>%
   do(head(.,as.numeric(unique(.$z))))

答案 3 :(得分:0)

我正在发布使用dplyr寻找的解决方案。它基于@HNSKD:

library(dplyr)
x<-c(3,2,1,8,7,11,10,9,7,5,4)
y<-c("a","a","a", "b","b","c","c","c","c","c","c")
z<-c(2,2,2,1,1,3,3,3,3,3,3)

df<-data.frame(x,y,z)

df %>% group_by(y) %>% slice(1:2)

每个y返回哪个前两个元素:

# A tibble: 6 x 3
# Groups:   y [3]
      x y         z
  <dbl> <fct> <dbl>
1     3 a         2
2     2 a         2
3     8 b         1
4     7 b         1
5    11 c         3
6    10 c         3