This is available as the slice_sample
function in dplyr
:
library(dplyr)
new_df <- df %>% group_by(ID) %>% slice_sample(n=500)
In older versions of R, the function was called sample_n
, which has been deprecated.
More Related Contents:
- Sample n random rows per group in a dataframe
- Dynamically select data frame columns using $ and a character value
- Drop unused factor levels in a subsetted data frame
- Merging a lot of data.frames [duplicate]
- Subset data frame based on multiple conditions [duplicate]
- dplyr: nonstandard column names (white space, punctuation, starts with numbers)
- Remove columns with zero values from a dataframe
- Calculating statistics on subsets of data [duplicate]
- How to convert dataframe into time series?
- Identifying duplicate columns in a dataframe
- Replace all occurrences of a string in a data frame
- Convert Named Character Vector to data.frame
- How to add a cumulative column to an R dataframe using dplyr?
- Get last row of each group in R [duplicate]
- Create empty data frame with column names by assigning a string vector? [duplicate]
- Add extra level to factors in dataframe
- Dataframe create new column based on other columns
- What is about the first column in R’s dataset mtcars?
- Determine the data types of a data frame’s columns
- finding close match from data frame 1 in data fame 2
- Combine several data frames in the global environment by row (rbind)
- Changing Million/Billion abbreviations into actual numbers? ie. 5.12M -> 5,120,000 [duplicate]
- Extract text after “/” in a data frame column
- What is difference between dataframe and list in R?
- use multiple columns as variables with sapply
- Merge overlapping ranges into unique groups, in dataframe
- Deleting specific rows from a data frame
- Count number of non-NA values for every column in a dataframe [duplicate]
- R how can I calculate difference between rows in a data frame
- Color one point and add an annotation in ggplot2?