# 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
```

### 2 Answers

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

Saurabh Chauhan
posted this

Let assume you have this dataset:

```
dt<-data.frame(ID=c("A","A","A","B","B","B","C","C","C"),
stringsAsFactors = F)
```

**Solution 1:**

```
dt$rowNum<-1:nrow(dt) # Add a column wich contains the index of each row
dt[(dt$rowNum %%5 ==0), ]
```

If you do not need the rowNum:

```
dt$rowNum<-NULL
```

**Solution 2:**

```
lapply(1:floor(nrow(dt)/5),function(x) dt[x*5, ] )
```

Salman Lashkarara
posted this

## Have an answer?

JD

It is about MS Excel?