Usage

    slice_dt(.data, …, by = NULL)

    slice_head_dt(.data, n, by = NULL)

    slice_tail_dt(.data, n, by = NULL)

    slice_max_dt(.data, order_by, n, by = NULL, with_ties = TRUE)

    slice_min_dt(.data, order_by, n, by = NULL, with_ties = TRUE)

    slice_sample_dt(.data, n, replace = FALSE, by = NULL)

    使用行位置筛选数据集 - 图1

    1. a = iris
    2. slice_dt(a,1,2)
    3. slice_dt(a,2:3)
    4. slice_dt(a,141:.N)
    5. slice_dt(a,1,.N)
    6. slice_head_dt(a,5)
    7. slice_head_dt(a,0.1)
    8. slice_tail_dt(a,5)
    9. slice_tail_dt(a,0.1)
    10. slice_max_dt(a,Sepal.Length,10)
    11. slice_max_dt(a,Sepal.Length,10,with_ties = FALSE)
    12. slice_min_dt(a,Sepal.Length,10)
    13. slice_min_dt(a,Sepal.Length,10,with_ties = FALSE)
    14. slice_sample_dt(a,10)
    15. slice_sample_dt(a,0.1)
    16. # use by to slice by group
    17. ## following codes get the same results
    18. slice_dt(a,1:3,by = "Species")
    19. slice_dt(a,1:3,by = Species)
    20. slice_dt(a,1:3,by = .(Species))
    21. slice_head_dt(a,2,by = Species)
    22. slice_tail_dt(a,2,by = Species)
    23. slice_max_dt(a,Sepal.Length,3,by = Species)
    24. slice_max_dt(a,Sepal.Length,3,by = Species,with_ties = FALSE)
    25. slice_min_dt(a,Sepal.Length,3,by = Species)
    26. slice_min_dt(a,Sepal.Length,3,by = Species,with_ties = FALSE)
    27. # in `slice_sample_dt`, "by" could only take character class
    28. slice_sample_dt(a,.1,by = "Species")
    29. slice_sample_dt(a,3,by = "Species")
    30. slice_sample_dt(a,51,replace = TRUE,by = "Species")