Getting R to return a row based on the given criteria

data <- data.frame(Names = c("Alie", "Kathy", "Corie", "Suzie", "Nathan", "Ryan", "Jake"),
Earnings = c(0, 200, 1000, 890, 200, 0, 0))
library(dplyr)
data <- data %>% slice_min(Earnings, n = 1)

I got the following output:
Names Earnings
Alie 0
Ryan 0
Jake 0

Based on minimum earnings, I want R to select one row at random
There should be a similar output
Names Earnings
Ryan 0
or
Names Earnings
Jake 0

Is sample() the right command to use?
data <- data %>% slice_min(Earnings, n = 1)
data <- sample(data, 1)

Please feel free to give me advice

Sticking with dplyr, you could use sample_n().

data %>% slice_min(Earnings, n = 1) %>% sample_n(1)
#>   Names Earnings
#> 1  Jake        0

Created on 2023-04-15 with reprex v2.0.2

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.