I am working with the R programming language. I made the following dataset:
Startx = rnorm(600,10,2)
Starty = rnorm(600,7,1)
Endx = rnorm(600,2,2)
Endy = rnorm(600,5,1)
Lines <- data.frame(Startx, Starty, Endx, Endy)
Here is how the dataset looks like:
> head(Lines)
Startx Starty Endx Endy
1 6.139012 8.238896 1.5970315 5.314411
2 11.079151 7.478156 4.2811267 6.940548
3 8.189728 6.622191 0.4567519 5.067486
4 11.424529 6.440073 1.5545516 6.044430
5 9.291668 6.122416 0.4611325 4.927958
6 11.218345 6.382152 2.7499929 5.006792
Next, I added an "index" (ID) variable to each row:
library(dplyr)
Lines <- mutate(Lines, id = rownames(Lines))
> head(Lines)
Startx Starty Endx Endy id
1 6.139012 8.238896 1.5970315 5.314411 1
2 11.079151 7.478156 4.2811267 6.940548 2
3 8.189728 6.622191 0.4567519 5.067486 3
4 11.424529 6.440073 1.5545516 6.044430 4
5 9.291668 6.122416 0.4611325 4.927958 5
6 11.218345 6.382152 2.7499929 5.006792 6
Question: Now, I am trying to "ID's" of these rows, so that :
- the ID for the first 6 rows is "1"
- the ID for the next 6 rows is "2"
- the ID for the next 6 rows is "3"
- etc.
Something that looks like this:
Startx Starty Endx Endy id
1 6.139012 8.238896 1.5970315 5.314411 1
2 11.079151 7.478156 4.2811267 6.940548 1
3 8.189728 6.622191 0.4567519 5.067486 1
4 11.424529 6.440073 1.5545516 6.044430 1
5 9.291668 6.122416 0.4611325 4.927958 1
6 11.218345 6.382152 2.7499929 5.006792 1
7 7.574718 6.750851 2.9556587 5.417020 2
8 13.182436 7.376812 2.1243483 6.620904 2
9 10.332929 9.083481 1.2771253 4.216452 2
10 3.706943 7.758393 -1.7933726 4.751453 2
11 10.676039 6.350639 1.5230948 5.316503 2
12 14.895981 8.130952 4.3009821 5.072671 2
13 13.329396 5.631068 0.5448433 3.770325 3
14 13.452864 5.988059 5.8843498 4.877926 3
15 8.036621 5.826816 3.2609650 5.686083 3
16 11.792844 6.977793 1.5243847 4.820037 3
17 14.178991 5.165727 5.0643746 5.088643 3
18 10.905152 8.426026 4.5867895 3.670802 3
19 8.867147 7.731436 1.4050062 4.546500 4
20 11.559410 8.094610 3.2317544 4.203140 4
What I tried so far: At the moment, I was able to temporarily solve this problem by brining this data frame into Microsoft Excel and semi-manually accomplish this task. But I was hoping that there might be a somewhat more "efficient" way to do this in R.
Can someone please show me how to do this?
Thanks