Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

r - Convert integer as "20160119" to different columns of "day" "year" "month"

How can I convert a column of integers as dates:

       DATE PRCP
1: 19490101   25
2: 19490102    5
3: 19490118   18
4: 19490119  386
5: 19490202   38

to a table like this:

days   month   years   PRCP
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

We can use extract

library(tidyr)
extract(df, DATE, into=c('YEAR', 'MONTH', 'DAY'), 
         '(.{4})(.{2})(.{2})', remove=FALSE)
#       DATE YEAR MONTH DAY PRCP
#1 19490101 1949    01  01   25
#2 19490102 1949    01  02    5
#3 19490118 1949    01  18   18
#4 19490119 1949    01  19  386
#5 19490202 1949    02  02   38

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...