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
667 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 Reply

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

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

...