Home > OS >  Create dataframe with repeating string from scratch in R
Create dataframe with repeating string from scratch in R

Time:07-03

I would like to create a dataframe that essentially would look something like this enter image description here

Repeating the period from 1 to 10 and assigning the ID 42,574 times so that I would end up with a 425,740 row dataframe.

I tried to create a dataframe using the following code

periodstring <- as.numeric(gl(10, 42574))
periods <- as.data.frame(periodstring)

but that sorts the numbers and other approaches did not quiete work. Is there a simple way to do this?

Thanks in advance.

CodePudding user response:

Another option using rep:

data.frame(Period=rep(1:10,times=42574),
           ID=rep(1:42574,each=10))

Output sample:

    Period ID
1        1  1
2        2  1
3        3  1
4        4  1
5        5  1
6        6  1
7        7  1
8        8  1
9        9  1
10      10  1
11       1  2
12       2  2
13       3  2
14       4  2
15       5  2
16       6  2
17       7  2
18       8  2
19       9  2
20      10  2
  • Related