从每组的前3个值制作新的数据框

时间:2019-10-15 09:00:16

标签: r sorting dataframe select

我想从每个“ gr”的“值”列的前3行创建一个新的数据框。因此,在下面的示例中,基本上是一个数据帧,其中'gr'1,2,3,4,5的前3个“值”。

这是一个示例数据集:

dataframe <- data.frame(sample = c("OP2645ii_c","OP5048___e","OP5048___f","OP5046___d","OP2645ii_e","OP2645ii_a","OP5054DNAa","OP5048___c","OP2645ii_d","OP5048___b","OP5047___a","OP5048___h","OP5053DNAb","OP3088i__a","OP5048___g","OP5053DNAa","OP5049___a","OP2645ii_b","OP5046___c","OP5044___c","OP2413iiia","OP5054DNAc","OP5046___e","OP5054DNAb","OP5044___a","OP5046___a","OP5046___b","OP2413iiib","OP5051DNAa","OP5048___d","OP5044___b","OP5049___b","OP5051DNAc","OP5051DNAb","OP5053DNAc","OP5047___b","OP5043___b","OP5043___a","OP5052DNAa"),
                          gr = c("5","3","3","5","5","5","5","3","5","3","3","3","3","3","3","3","2","1","2","1","1","1","2","2","2","1","2","1","1","1","2","1","1","4","4","4","4","4","4"),
                          value = c("20.06915","20.06915","19.53556","19.39911","19.06339","18.35938","18.34701","17.85767","17.60714","17.30706","17.08515","16.91452","16.72728","16.46812","15.85850","15.42839","14.92798","14.65943","14.53258","14.33954","14.33583","14.23938","14.19658","14.12557","14.03669","13.89811","13.78137","13.75599","13.51798","13.41058","13.17932","13.11952","12.67316","12.57049","11.88663","11.08443","10.75299","10.61885","10.40393"))

非常感谢 干杯

1 个答案:

答案 0 :(得分:0)

首先,让我们创建一个为我们提供前n行的函数:

top_n <- function(x, n = 3L) {
  x[head(order(x$value), n = n), ]
}

使用基数R,我们可以使用by()rbind()返回的列表:

res <- by(dataframe, dataframe$gr, top_n)
do.call(rbind, res)

使用data.table,我们可以使用更简洁的语法实现相同的目标:

data.table::setDT(dataframe)
dataframe[, top_n(.SD), by = "gr"]