如何对dplyr中的每n行进行非随机采样? [英] How to non-randomly sample every n rows in dplyr?

查看:73
本文介绍了如何对dplyr中的每n行进行非随机采样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在dplyr中执行 sample_n(),除了我不希望采样是随机的,我打算每n行进行一次采样。

I'd like to do sample_n() in dplyr, except I don't want the sampling to be random, I intend to sample every n rows.

有办法吗?

例如,我想获取<$的第10行在按排序后,c $ c> airquality 数据集。预期产量:

For example, I want to get every 10th row of the airquality dataset after ordering by Month and Day. Expected output:

   Ozone Solar.R Wind Temp Month Day
      NA     194  8.6   69     5  10
      11      44  9.7   62     5  20
     115     223  5.7   79     5  30
      71     291 13.8   90     6   9
      12     120 11.5   73     6  19
      NA      31 14.9   77     6  29

    ...


推荐答案

您真的想

mtcars [seq(1,nrow(mtcars),10),]

将两个 mtcars 事件都替换为您的data.frame,并替换 10 和要提取的第n行

Replace both mtcars occurances with your data.frame, And replace 10 with the nth row you want to extract

这篇关于如何对dplyr中的每n行进行非随机采样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆