bobby
bobby

Reputation: 183

How to randomly select n observations in a dataframe?

given a fairly large data frame I want to randomly select 500 observations

samp <- sample_n(df,500)
samp 

doesn't work for some reason it just gives me an error "Error: size must be less or equal than 1 (size of data), set replace = TRUE to use sampling with replacement"

Upvotes: 0

Views: 973

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389235

As the dataframe is grouped it is trying to take 500 observations from each group. ungroup the dataframe and then use sample_n.

library(dplyr)
samp <- df %>% ungroup() %>% sample_n(500)

In base R, we can use sample directly without ungrouping.

samp <- df[sample(nrow(df), 500), ]

Upvotes: 4

Related Questions