Tim Henderson
Tim Henderson

Reputation: 11

Randomly selecting specific rows in R data

I'm wondering if it's possible to randomly select specific categories from rows of a excel spreadsheet in R.

E.g. here is sample data sample data

I want to randomly select only one type of the "Station" rows (e.g. only the 'LC04' rows, but have this determined randomly). Is there a way this can be done in R? I have a large dataset, and want to randomize the selection ~100 times, so then I can use each random outcome for additional analyses.

Thanks!

Upvotes: 1

Views: 273

Answers (1)

Bettina Shirley
Bettina Shirley

Reputation: 21

Using the group_by and sample_n function from the dplyr library

newdata <- sampledata %>% group_by(station) %>% sample_n(2)

The sample_n value can be modified according to the required number of rows of each type of station

Upvotes: 2

Related Questions