从数据框中选择每第 n 行 [英] Select every nth row from dataframe

查看:31
本文介绍了从数据框中选择每第 n 行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表,想从中提取每五行以创建一个新表.是否有实现此目的的命令?

I have a data table and want to extract every fifth row from it to create a new table. Is there a command to achieve this?

这是我的数据示例:

count   Idf_Nr  block
1   1233    B12
2   1233    B12
3   1446    B12
4   1446    B12
5   365 B12
6   365 B12
7   876 B12
8   876 B12
9   842 B12
10  842 B12
11  1092    B12
12  1092    B12
13  923 B12
14  923 B12
15  1266    B12
16  1266    B12
17  256 B12
18  256 B12
19  588 B12
20  588 B12
21  1074    B12
22  1074    B12
23  474 B12
24  474 B12
25  1421    B12

推荐答案

对于一个数据框 df,你可以得到 df.new 为:

For a data frame df, you can get df.new as:

df.new = df[seq(1, nrow(df), 5), ]

这会每 5 行创建一个从第 1 行到 nrow(表的行数)的索引.您可以使用起始点和 5 来提取其他序列.

This creates an index from row 1 to nrow (number of rows of the table) every 5 rows. You can play with the starting point and the 5 to extract other sequences.

这篇关于从数据框中选择每第 n 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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