如何使用索引R获取前n个值?
我有一个只有一列的数据框,我想用它的索引找到最大的三个值。例如,我的数据帧如何使用索引R获取前n个值?,r,R,我有一个只有一列的数据框,我想用它的索引找到最大的三个值。例如,我的数据帧df如下所示: distance 1 1 2 4 3 2 4 3 5 4 6 5 7 5 我想找到最大的3值及其索引,因此我的预期结果是: distance 6 5 7 5 2 4 5 4 4 3 我该怎么做?由于我只有一列,是否也可以使用列表而不是数据框?我们可以使用排序和索引。return=TRUE返回带有列表中索引的值。然后,我们可以根据“x”中的前3个唯一元素对列表进行子集划分 lst <
df
如下所示:
distance
1 1
2 4
3 2
4 3
5 4
6 5
7 5
我想找到最大的3值及其索引,因此我的预期结果是:
distance
6 5
7 5
2 4
5 4
4 3
我该怎么做?由于我只有一列,是否也可以使用列表而不是数据框?我们可以使用
排序和索引。return=TRUE
返回带有列表中索引的值。然后,我们可以根据“x”中的前3个唯一元素对列表进行子集划分
lst <- sort(df1$distance, index.return=TRUE, decreasing=TRUE)
lapply(lst, `[`, lst$x %in% head(unique(lst$x),3))
#$x
#[1] 5 5 4 4 3
#$ix
#[1] 6 7 2 5 4
lst
如果有更多的列,那么应该有
df[order(df$column_name, decreasing=TRUE)[1:3],,drop=FALSE]
我以前的代码有点笨拙:
df[order(df$distance, decreasing = TRUE)[sort(unique(df$distance))], , drop = FALSE]
distance
6 5
7 5
2 4
5 4
4 3
如果您正在寻找一个列以从递增到递减的顺序进行排序
rownames = rownames(df)
indexes <- order(df$ColumnName,decreasing = TRUE)[1:N]
result <- NULL
for (i in indexes)
result<- c(rownames[i],result)
result
rownames=rownames(df)
使用库数据索引。表是一个更快的解决方案,因为setorder
比order
和sort
更快:
library(data.table)
select_top_n<-function(scores,n_top){
d <- data.frame(
x = copy(scores),
indice=seq(1,length(scores)))
setDT(d)
setorder(d,-x)
n_top_indice<-d$indice[1:n_top]
return(n_top_indice)
}
select_top_n2<-function(scores,n_top){
n_top_indice<-order(-scores)[1:n_top]
return(n_top_indice)
}
select_top_n3<-function(scores,n_top){
n_top_indice<-sort(s, index.return=TRUE, decreasing=TRUE)$ix[1:n_top]
return(n_top_indice)
}
请参阅您可以使用软件包Rfast
中的函数nth
获取索引或值
> x=runif(100000)
> num.of.nths <- 3
> Rfast2::benchmark(a<-Rfast::nth(x,3,num.of.nths,TRUE,TRUE),b<-order(x,decreasing = T)[1:3],times = 10)
milliseconds
min mean max
a <- Rfast::nth(x, 3, 3, TRUE, TRUE) 1.6483 2.12419 3.1238
b <- order(x, decreasing = T)[1:3] 6.8648 12.31633 27.1988
>
> a
[,1]
[1,] 8058
[2,] 63946
[3,] 17556
> b
[1] 8058 63946 17556
>x=runif(100000)
>num.of.nths Rfast2::benchmark(a获取任何列的最高百分比(比例
df%slice\u max(IndexCol,prop=.25)
或者由一群人
df%group\u by(col1,col2)%%>%slice\u max(IndexCol,prop=.25)
非常感谢您的回答。但我不知道可以提前返回多少值。可能是5、4或3……嗨,akrun,我知道,您在Lappy中使用了[
。什么是[
?@xirururururu它只是根据返回的索引对数据集进行子集划分,列出%head中的$x%(唯一的.
。不使用匿名函数。它可以写为lappy(lst,函数(y)y[lst$x%in%head(唯一的(lst$x),3)])
@xirururururu您可以从?Extract
或中找到更多信息[”
你好,阿克伦,非常感谢!:D我现在在提取页面。这真的很酷,我可以从一个小问题中学到很多东西。DHi Theodor,谢谢你的回答,但我得到的结果是:5,5,4。实际上,我想要3个距离值,所以前3个值是5,5,4,3。你知道,我怎么做吗?
set.seed(123)
s=runif(100000)
library(microbenchmark)
mbm<-microbenchmark(
ind1 = select_top_n(s,100),
ind2=select_top_n2(s,100),
ind3=select_top_n3(s,100),
times = 10L
)
Unit: milliseconds
expr min lq mean median uq max neval
ind1 5.824576 5.98959 6.209746 6.052658 6.270312 7.422736 10
ind2 9.627950 10.08661 10.274867 10.377451 10.560912 10.588223 10
ind3 10.397383 11.32129 12.087122 12.498817 12.856840 13.155845 10
> x=runif(100000)
> num.of.nths <- 3
> Rfast2::benchmark(a<-Rfast::nth(x,3,num.of.nths,TRUE,TRUE),b<-order(x,decreasing = T)[1:3],times = 10)
milliseconds
min mean max
a <- Rfast::nth(x, 3, 3, TRUE, TRUE) 1.6483 2.12419 3.1238
b <- order(x, decreasing = T)[1:3] 6.8648 12.31633 27.1988
>
> a
[,1]
[1,] 8058
[2,] 63946
[3,] 17556
> b
[1] 8058 63946 17556