简体   繁体   中英

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? 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

Step 2: Generate the sequences and then retrieve rows based on sequences

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

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,])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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