1

I'm looking to recode a column, say the following:

df <- data.frame(col1 = rep(3, 100), 
                 col2 = rep(NA, 100))

I want to recode col2 as 1 for rows 1:5, 2 for rows 6:10, 3 for 11:15, etc. So, every five rows I would add +1 to the assigned value. Any way to automate this process to avoid manually recoding 100 rows?

AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
juanjedi
  • 140
  • 1
  • 7

3 Answers3

3

There are lot of ways to do that. Here are couple of them -

  1. Using rep :
df$col2 <- rep(1:nrow(df), each = 5, length.out = nrow(df))
  1. Using ceiling
df$col2 <- ceiling(seq(nrow(df))/5)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
2

dplyr way

df %>% mutate(col2 = ((row_number()-1) %/% 5)+1)

OR

A simple for loop

for(i in 0:((nrow(df)/5)-1)){
  df[0:nrow(df) %/% 5 == i,2] <- i+1
}

> df
    col1 col2
1      3    1
2      3    1
3      3    1
4      3    1
5      3    1
6      3    2
7      3    2
8      3    2
9      3    2
10     3    2
11     3    3
12     3    3
13     3    3
14     3    3
15     3    3
16     3    4
17     3    4
18     3    4
19     3    4
20     3    4
21     3    5
22     3    5
23     3    5
24     3    5
25     3    5
26     3    6
27     3    6
28     3    6
29     3    6
30     3    6
31     3    7
32     3    7
33     3    7
34     3    7
35     3    7
36     3    8
37     3    8
38     3    8
39     3    8
40     3    8
41     3    9
42     3    9
43     3    9
44     3    9
45     3    9
46     3   10
47     3   10
48     3   10
49     3   10
50     3   10
51     3   11
52     3   11
53     3   11
54     3   11
55     3   11
56     3   12
57     3   12
58     3   12
59     3   12
60     3   12
61     3   13
62     3   13
63     3   13
64     3   13
65     3   13
66     3   14
67     3   14
68     3   14
69     3   14
70     3   14
71     3   15
72     3   15
73     3   15
74     3   15
75     3   15
76     3   16
77     3   16
78     3   16
79     3   16
80     3   16
81     3   17
82     3   17
83     3   17
84     3   17
85     3   17
86     3   18
87     3   18
88     3   18
89     3   18
90     3   18
91     3   19
92     3   19
93     3   19
94     3   19
95     3   19
96     3   20
97     3   20
98     3   20
99     3   20
100    3   20
AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
1

As there is a pattern (each 5th row) you can use rep(row_number()) length.out = n() takes into account the length of column.

Learned here dplyr: Mutate a new column with sequential repeated integers of n time in a dataframe from Ronak!!! Thanks to Ronak!

df %>% mutate(col2 = rep(row_number(), each=5, length.out = n()))
TarJae
  • 72,363
  • 6
  • 19
  • 66