user138773 - 2 months ago 28

R Question

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**)?

`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)`

Answer

Here is a way using `dplyr`

:

```
library(dplyr)
df <- data.frame(x, LR)
df1 <- df %>%
filter((x >= 0.4) & (x <= 0.8)) %>%
arrange(desc(LR)) %>%
head(16)
# to save the data
write.csv(df1, "abc.csv", row.names=FALSE)
```