Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

r - Naming of each date with unique id number

I have this Data set

date1<-c(rep(c("2020-06-01"),times=20))
date2<-c(rep(c("2020-06-02"),times=15))
date3<-c(rep(c("2020-06-03"),times=5))
date4<-c(rep(c("2020-06-04"),times=10))
date5<-c(rep(c("2020-06-05"),times=2))
date<-c(date1,date2,date3,date4,date5)

I want to make a data frame which will read each date as 1,1,1,2,2,2,3,3,4,4....

The preferable out put would be

    date     number of dates
1  2020-06-01       1
2  2020-06-01       1
3  2020-06-01       1     
4  2020-06-01       1
5  2020-06-01       1
6  2020-06-01       1
7  2020-06-01       1
8  2020-06-01       1
9  2020-06-01       1 
10 2020-06-01       1
11 2020-06-01       1
12 2020-06-01       1
13 2020-06-01       1
14 2020-06-01       1
15 2020-06-01       1
16 2020-06-01       1
17 2020-06-01       1
18 2020-06-01       1
19 2020-06-01       1
20 2020-06-01       1
21 2020-06-02       2
22 2020-06-02       2
23 2020-06-02       2
24 2020-06-02       2
25 2020-06-02       2
26 2020-06-02       2
27 2020-06-02       2
28 2020-06-02       2
29 2020-06-02       2
30 2020-06-02       2
31 2020-06-02       2
32 2020-06-02       2
33 2020-06-02       2
34 2020-06-02       2
35 2020-06-02       2
36 2020-06-03       3
37 2020-06-03       3
38 2020-06-03       3
39 2020-06-03       3
40 2020-06-03       3
41 2020-06-04       4
42 2020-06-04       4
43 2020-06-04       4
44 2020-06-04       4
45 2020-06-04       4
46 2020-06-04       4   
47 2020-06-04       4
48 2020-06-04       4
49 2020-06-04       4
50 2020-06-04       4
51 2020-06-05       5
52 2020-06-05       5
question from:https://stackoverflow.com/questions/66064031/naming-of-each-date-with-unique-id-number

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Convert date to a factor and then numeric to get the factor codes:

data.frame(date, day_no = as.numeric(factor(date)))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...