简体   繁体   English

如何在文件Excel中保留第五行(并删除所有其他行)?

[英]How to keep every fifth row (and deleting all the others) in a file Excel?

How to keep every fifth row (and deleting all the others) in a file Excel? 如何在文件Excel中保留第五行(并删除所有其他行)? For example, I have a starting file like this: 例如,我有一个像这样的起始文件:

07/12/1989  106,9
08/12/1989  106,05
12/12/1989  103,1
13/12/1989  106,5
14/12/1989  104,75
15/12/1989  105,6
18/12/1989  104,5
19/12/1989  106,2
20/12/1989  106,5
21/12/1989  107,5
22/12/1989  109,8

and I would like the result: 我想要结果:

07/12/1989  106,9
15/12/1989  105,6
22/12/1989  109,8

Try this: 尝试这个:

Step 1: Read excel file in R using read.xlsx 步骤1:使用read.xlsx在R中读取excel文件

Step 2: Generate the sequences and then retrieve rows based on sequences 步骤2:生成序列,然后根据序列检索行

indexes<-seq(1,nrow(df),5) # Set index
df[indexes,] # Retrive only index

Output: 输出:

         V1    V2
1  07/12/1989 106,9
6  15/12/1989 105,6
11 22/12/1989 109,8

Step 3: Store this result to excel file using write.xlsx 步骤3:使用write.xlsx结果存储到excel文件中

Let assume you have this dataset: 假设您具有以下数据集:

dt<-data.frame(ID=LETTERS, stringsAsFactors = F)

Then you can do: 然后,您可以执行以下操作:

as.data.frame( dt[ 1:nrow(dt) %% 5 ==0,])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM