R Language Learning notes: Date processing

Source: Internet
Author: User
Tags month name

1. Take out the current date

Sys.date ()

[1] "2014-10-29"

Date () #注意: This method returns a string type

[1] "Wed Oct 29 20:36:07 2014"

2, in R the date is actually double type, is the number of days since January 1, 1970

typeof (Sys.date ())

[1] "double"

3. Convert to date

With AS. Date () can convert a string to a date value, and the default format is YYYY-MM-DD.

As. Date ("2007-02-01") #得到 "2007-02-01", which is displayed as a string, but is actually stored with a double

As.double (AS. Date ("1970-01-01")) #结果为0, is the number of days since January 1, 1970.

You can convert a custom date string to a date type

As. Date ("February 1, 2007", "%y year%m month%d Day")

[1] "2007-02-01"

Format

Significance

%Y

Year, expressed as four digits, 2007

%m

month, expressed in numbers, from 01 to 12

%d

Number of days in the month, from 01 to 31

%b

month, abbreviation, FEB

%B

month, full month name, English, February

%y

Year, expressed as two digits, 07

4. Output the date value as a string

Today <-Sys.date ()

Format (today, "%y year%m month%d Day")

[1] "October 29, 2014"

5. Calculation Date Difference

Because the date inside is the number of days stored in double, it can be subtracted.

Today <-Sys.date ()

GTD <-as. Date ("2011-07-01") #我是从2011年7月开始实践GTD的

Today-gtd

Time difference of 1216 days

Originally I have been practicing GTD for 1216 days.

Use the Difftime () function to calculate the number of seconds, minutes, hours, days, weeks, and months

Difftime (Today, GTD, units= "weeks") #还可以是 "secs", "mins", "hours", "Days"

Time difference of 173.7143 weeks

Continue to solve one of my practical problems with the R language: Count the data in the Fitbit pedometer and draw

# Read the Fitbit stats in CSV format

Fitbit <-read.csv ("Fitbit.csv")

# Date to convert

Fitbit$date <-as. Date (Fitbit$date, "%y year%m month%d Day")

# drawing, drawing only lines

Plot (Fitbit$date, Fitbit$step, type= "L")

# only statistics on the movement of the August

Fitbit8 <-fitbit[months (fitbit$date) = = "August",]
Plot (Fitbit8$date, Fitbit8$step, type= "L")

R language Learning Note: Date processing

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.