How can I create a new column with the same id every n rows in R?

Viewed 628

I have a data frame where I want to create a new column in which to assign the same ID every 30 rows.

My data frame is from an experiment and I wish to create a new "bloc" column, so that every 30 rows it increments by 1

example:

col1 : response latency = 1,0002, 1.2566, ...30times, 1.5422, ...

col2 : difficulty = easy, hard, intermediate, ...

col3 : ID = 1, 2, 3, ...30times, 31, 32, ...

And I want a new column

new col : bloc = 1, 1, ...30times, 2, 2, ...30times, 3, 3, ...

2 Answers

Using 5 as an example, but this of course works the same for 30

df <- data.frame(rownum = 1:23)

bloc_len <- 5

df$bloc <- 
  rep(seq(1, 1 + nrow(df) %/% bloc_len), each = bloc_len, length.out = nrow(df))

df
#    rownum bloc
# 1       1    1
# 2       2    1
# 3       3    1
# 4       4    1
# 5       5    1
# 6       6    2
# 7       7    2
# 8       8    2
# 9       9    2
# 10     10    2
# 11     11    3
# 12     12    3
# 13     13    3
# 14     14    3
# 15     15    3
# 16     16    4
# 17     17    4
# 18     18    4
# 19     19    4
# 20     20    4
# 21     21    5
# 22     22    5
# 23     23    5

You could also use %/% (same output)

df$bloc <- 
  1 + seq(0, nrow(df) - 1) %/% bloc_len

You can use rep(x, times) function to create the bloc you wished.

See the example above

set.seed(12345)

Create a random data set

data <- data.frame(
    response_latency = abs(rnorm(90, 2, 1)),
    difficulty = sample(c("easy", "hard", "intermediate"), 90, replace = TRUE),
    ID = 1:90
)

head(data, n = 35)

   response_latency   difficulty ID bloc
1         1.8890497 intermediate  1    1
2         2.9996586 intermediate  2    1
3         3.0255886         hard  3    1
4         0.3949156         hard  4    1
5         2.0027199         easy  5    1
6         2.9580737         hard  6    1
7         1.3337903 intermediate  7    1
8         1.4844084         hard  8    1
9         1.3941750         hard  9    1
10        1.6923244 intermediate 10    1
11        1.8186642         easy 11    1
12        0.9167691         easy 12    1
13        2.5987185         easy 13    1
14        1.8345693 intermediate 14    1
15        0.9177725         hard 15    1
16        2.3445309         easy 16    1
17        2.5187724         hard 17    1
18        1.2220053         hard 18    1
19        2.1636086         hard 19    1
20        0.7847963         hard 20    1
21        1.3785363         hard 21    1
22        2.9451529 intermediate 22    1
23        2.3722482 intermediate 23    1
24        2.1812877 intermediate 24    1
25        0.1383615         easy 25    1
26        1.3996498         easy 26    1
27        3.7593749         hard 27    1
28        2.0056114         hard 28    1
29        3.2195714         hard 29    1
30        2.1481248         easy 30    1
31        3.2546741 intermediate 31    2
32        2.4221608         hard 32    2
33        2.0465687 intermediate 33    2
34        1.7649423         easy 34    2
35        1.7338255         hard 35    2

Here, to add the bloc column in your dataset, you can use the following code:

bloc <- c(rep(x = 1, times = 30), rep(x = 2, times = 30), rep(x = 3, times = 30))
data$bloc <- bloc

head(data,n=35)

The new dataset will be as follow.

   response_latency   difficulty ID bloc
1         1.8890497 intermediate  1    1
2         2.9996586 intermediate  2    1
3         3.0255886         hard  3    1
4         0.3949156         hard  4    1
5         2.0027199         easy  5    1
6         2.9580737         hard  6    1
7         1.3337903 intermediate  7    1
8         1.4844084         hard  8    1
9         1.3941750         hard  9    1
10        1.6923244 intermediate 10    1
11        1.8186642         easy 11    1
12        0.9167691         easy 12    1
13        2.5987185         easy 13    1
14        1.8345693 intermediate 14    1
15        0.9177725         hard 15    1
16        2.3445309         easy 16    1
17        2.5187724         hard 17    1
18        1.2220053         hard 18    1
19        2.1636086         hard 19    1
20        0.7847963         hard 20    1
21        1.3785363         hard 21    1
22        2.9451529 intermediate 22    1
23        2.3722482 intermediate 23    1
24        2.1812877 intermediate 24    1
25        0.1383615         easy 25    1
26        1.3996498         easy 26    1
27        3.7593749         hard 27    1
28        2.0056114         hard 28    1
29        3.2195714         hard 29    1
30        2.1481248         easy 30    1
31        3.2546741 intermediate 31    2
32        2.4221608         hard 32    2
33        2.0465687 intermediate 33    2
34        1.7649423         easy 34    2
35        1.7338255         hard 35    2
Related