Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201977

Get X rows from dataframe based on lowest values in specified column; and randomly select among ties

$
0
0

top_n in dplyr is doing almost what I want; however, when there are ties it returns all, but I would like it to randomly select among the ties to satisfy my cutoff of 2 rows.

top_number=2
x <- c(1, 3, 4, 6, 6, 6)
y <- c(3, 2, 5, 1, 3, 3)
xy <- data.frame(x, y) 
xy
xy_1 <- dplyr::top_n(xy, top_number, wt = x)
xy_1

As you can see I get 3 rows, not 2. Would be nice with a tidyverse solution.


Viewing all articles
Browse latest Browse all 201977

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>