MrF*_*ick 16
只需使用sample()选择一些组
iris %>% filter(Species %in% sample(levels(Species),2))
Run Code Online (Sandbox Code Playgroud)
小智 8
如果您使用 dplyr,我认为这种方法最有意义:
iris_grouped <- iris %>%
group_by(Species) %>%
nest()
Run Code Online (Sandbox Code Playgroud)
其中产生:
# A tibble: 3 x 2
Species data
<fct> <list>
1 setosa <tibble [50 × 4]>
2 versicolor <tibble [50 × 4]>
3 virginica <tibble [50 × 4]>
Run Code Online (Sandbox Code Playgroud)
然后您可以使用它sample_n:
iris_grouped %>%
sample_n(2)
# A tibble: 2 x 2
Species data
<fct> <list>
1 virginica <tibble [50 × 4]>
2 versicolor <tibble [50 × 4]>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3105 次 |
| 最近记录: |