简体   繁体   English

在R Vector中找到16个最大值

[英]finding 16 largest values in R Vector

Given the R code below , I was wondering how to create two columns such that the 16 largest values of LR and their corresponding xs when "x" is bounded between .4 and .8 be shown? 给定下面的R代码 ,我想知道如何创建两列,以便当“ x”介于0.4和0.8之间时显示LR的16个最大值及其对应的xs吗? ( a column for x & a column for the corresponding LR )? x 的列和对应的LR的列 )?

n=100
h=60
x=seq(0,1,by=0.02)
LR <- dbeta(x,h+1,n-h+1)/max(dbeta(x,h+1,n-h+1))

I'm trying the following, but can't create the two columns: 我正在尝试以下操作,但是无法创建两列:

head(sort(LR[which(x<=.8 & x>=.4)], decreasing=TRUE), 16)

您可以在您的条件下使用sort对向量LR的子集进行排序:

sort(LR[x <= .8 & x >= .4],decreasing = TRUE)[1:16]

Here is a way using dplyr : 这是使用dplyr的方法:

library(dplyr)
df <- data.frame(x, LR)
df1 <- df %>%
  filter((x >= 0.4) & (x <= 0.8)) %>% 
  top_n(16, LR)

# to save the data
write.csv(df1, "abc.csv", row.names=FALSE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM