简体   繁体   中英

Create dataframe with repeating string from scratch in R

I would like to create a dataframe that essentially would look something like this在此处输入图像描述

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM