如何从表中选择每个第x行

时间:2022-11-10 09:51:42

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?

我有一个数据表,并希望从中提取每五行以创建一个新表。是否有命令实现这一目标?

Here is an sample of my data:

以下是我的数据示例:

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

2 个解决方案

#1


15  

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

对于数据框df,您可以将df.new作为:

df.new = df[seq(1, nrow(df), 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.

这将从每行5行创建第1行到第n行(表的行数)的索引。您可以使用起点和5来提取其他序列。

#2


4  

If you want to extract 5,10...

如果你想提取5,10 ......

newdf <- df[c(rep(FALSE,4),TRUE), ]

If 1,6,11,

newdf <- df[c(TRUE,rep(FALSE,4)), ]

#1


15  

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

对于数据框df,您可以将df.new作为:

df.new = df[seq(1, nrow(df), 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.

这将从每行5行创建第1行到第n行(表的行数)的索引。您可以使用起点和5来提取其他序列。

#2


4  

If you want to extract 5,10...

如果你想提取5,10 ......

newdf <- df[c(rep(FALSE,4),TRUE), ]

If 1,6,11,

newdf <- df[c(TRUE,rep(FALSE,4)), ]