user138773 user138773 - 8 months ago 58
R Question

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? (a column for x & a column for the corresponding LR)?

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)

Answer Source

Here is a way using dplyr:

df <- data.frame(x, LR)
df1 <- df %>%
  filter((x >= 0.4) & (x <= 0.8)) %>% 
  arrange(desc(LR)) %>%

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