Sample n random rows per group in a dataframe
问题 From these questions - Random sample of rows from subset of an R dataframe & Sample random rows in dataframe I can easily see how to randomly sample (select) \'n\' rows from a df, or \'n\' rows that originate from a specific level of a factor within a df. Here are some sample data: df <- data.frame(matrix(rnorm(80), nrow=40)) df$color <- rep(c(\"blue\", \"red\", \"yellow\", \"pink\"), each=10) df[sample(nrow(df), 3), ] #samples 3 random rows from df, without replacement. To e.g. just sample 3