Reputation: 5925
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 :
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
Upvotes: 1
Views: 38
Reputation: 887681
We can use
library(dplyr)
Lines <- Lines %>%
mutate(id = as.integer(gl(n(), 6, n())))
Upvotes: 1
Reputation: 79184
library(dplyr)
Lines <- mutate(Lines, id = rownames(Lines)) %>%
mutate(ID = rep(row_number(), each=6, length.out = n()))
Startx Starty Endx Endy id ID
1 6.791532 6.344849 0.56099835 4.557484 1 1
2 9.907718 6.333883 -0.36249572 5.159065 2 1
3 10.073533 6.031520 -0.37077947 5.124256 3 1
4 6.407786 8.020749 5.66129797 2.063402 4 1
5 11.736735 6.926289 7.19877205 3.880176 5 1
6 10.699439 7.250388 0.63691737 4.748458 6 1
7 10.387614 6.240162 3.53096881 3.154894 7 2
8 9.126212 6.463472 3.45527039 3.536357 8 2
9 9.875189 7.486275 3.00520682 4.750458 9 2
10 8.157215 5.853551 0.88678342 4.222574 10 2
11 10.122712 7.491003 -0.05283184 3.311954 11 2
12 11.169240 6.113054 -1.32465120 4.879334 12 2
13 8.611155 6.974169 2.84887239 4.545410 13 3
14 10.275210 7.497898 0.47346846 4.332957 14 3
15 8.940241 6.014243 3.84548597 4.413747 15 3
16 13.440112 7.125526 1.28334553 6.691715 16 3
17 8.521694 5.439896 1.34850499 5.281371 17 3
18 9.169292 6.692165 2.83474345 3.654708 18 3
19 6.358885 5.968021 3.45486083 4.647540 19 4
20 11.072854 7.416212 1.98031674 6.080660 20 4
21 12.855619 5.905539 3.01461705 5.800007 21 4
...
Update for OP:
Lines %>%
as_tibble() %>%
mutate(id = row_number(),
ID = rep(row_number(), each=6, length.out = n()))
Upvotes: 1