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

查看:106
本文介绍了从数据框中选择第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), ]

这将创建一个从第1行到第5行的索引(表的行数)。您可以使用起点和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天全站免登陆