R language-converts string data into DateTime, week, month, etc., including build date _r language

Source: Internet
Author: User

#假设已经有字符串如下:

D1<-c ("2011-01-02", "2011-01-09", "2011-01-16", "2011-01-23", "2011-01-30"
, "2011-02-06", "2011-02-13", "2011-02-20", "2011-02-27", "2011-03-06"
, "2011-03-13", "2011-03-20", "2011-03-27", "2011-04-03", "2011-04-10"
, "2011-04-17", "2011-04-24", "2011-05-01", "2011-05-08", "2011-05-15"
, "2011-05-22", "2011-05-29", "2011-06-05", "2011-06-12", "2011-06-19"
, "2011-06-26", "2011-07-03", "2011-07-10", "2011-07-17", "2011-07-24"
, "2011-07-31", "2011-08-07")

#转换成日期:

Dmonth<-as. Date (D1, "%y-%m-%d") #有的人日期喜欢写成 "2011/07/31", this time only need to change to/that as. Date (D1, "%y/%m/%d")

#转换成星期, Month:

Month1<-months (Dmonth) #转化为月份
Week1<-weekdays (Dmonth) #转化为星期

#时间比较 (in fact, R automatically sorted the time, interested students can try this sentence as.numeric (dmonth) < convert time into data, can be counted as R is the day as the first day of >)

#假如说 already have dataset data

Data1<-data[dmonth<= "2011-12-30"] #提取数据集data中11年之前的数据

#生成日期数据

#刚才说了怎么将时间转为数字, it must be possible to convert numbers to dates in turn.

D2<-as. Date (23768, origin = "1900-01-01") #感兴趣的朋友可以知道R的origin是多少了吧.

#利用seq函数

date1<-seq (from=as. Date (' 2006/01/01 '), To=as. Date (' 2016/08/03 '), by= ' 1 day ') #想生成月度或者星期, just replace day with month or week

#返回当前日期或系统时间:

Sys.date () #日期

Date () #具体时间


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.