2019-03-18
阅读量:
7054
R语言中从数据框中选择随机行
可以使用函数sample_n()选择n个随机行,也可以使用sample_frac()选择行的随机分数。 我们首先使用函数set.seed()来启动随机数生成器引擎。 这对于用户重现分析非常重要。
set.seed(1234)
#无放回的随机取五行
my_data %>% sample_n(5, replace = FALSE)
#无放回的随机取5%行
my_data %>% sample_frac(0.05, replace = FALSE)
#根据Sepal.Length值取最大的五行
my_data %>% top_n(5, Sepal.Length)
#根据Species分组,然后以Sepal.Length值取最高的五行
my_data %>%
group_by(Species) %>%
top_n(5, Sepal.Length)






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
0条评论