I am trying to sort a column in a data.frame with mixed orders.
Specifically, I would like to sort it in ascending order first. Then for the highest 30% observations, I want them to be in descending order. How can I do that?
For example,
x <- c(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
I want to sort it into this:
c(1, 2, 3, 4, 5, 6, 7, 10, 9, 8)
How can I do that?
To find: a function object f , such that f(x) = y
Solution:
x <- c(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
y <- c(1, 2, 3, 4, 5, 6, 7, 10, 9, 8)
get_top <- function(x,n) ceiling(quantile(x, probs = n))
make_vct <- function(x,n) {
c(
sort(x[x < ceiling((get_top(x,.7)))]),
x[x >= ceiling((get_top(x,.7)))])
}
make_vct(x,.7)
#> [1] 1 2 3 4 5 6 7 10 9 8
Created on 2020-09-27 by the reprex package (v0.3.0.9001)
I am trying to make your function work on data.table but couldn't get it done.
mixed_sort <- function(data_table, col, prob)
{data_table1 <- data_table[eval(parse(text = col)) < quantile(eval(parse(text = col)), prob, na.rm = TRUE)]
setorder(data_table1, eval(parse(text = col)))
data_table2 <- data_table[eval(parse(text = col)) >= quantile(eval(parse(text = col)), prob, na.rm = TRUE)]
setorder(data_table1, eval(parse(text = paste0("-", col))))
return(rbind(data_table1, data_table2))}
data_table <- data.table(x = 1:20)
mixed_sort(data_table, "x", 0.7)
I am not sure how to make this code work.
What x does the function require, and were is it to be found in data_table
?
I solved this problem:
mixed_sort <- function(data_table, col, prob)
{data_table1 <- data_table[get(col) < quantile(get(col), prob, na.rm = TRUE)]
data_table1 <- data_table1[order(get(col))]
data_table2 <- data_table[eval(parse(text = col)) >= quantile(eval(parse(text = col)), prob, na.rm = TRUE)]
data_table2 <- data_table2[order(-get(col))]
return(rbind(data_table1, data_table2))}
data_table <- data.table(x = 1:20)
mixed_sort(data_table, "x", 0.7)
1 Like
I do a similar thing as shown below
# Sort counties with 20 largest first, then alphabetical
ByPop <- arrange(County_pop, -Population)
ByAlpha <- arrange(ByPop[21:nrow(ByPop),], County)
County_pop <- bind_rows(ByPop[1:20,], ByAlpha)
ByPop <- ByAlpha <- NULL
system
Closed
October 6, 2020, 2:47pm
7
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.