web123456

R language - convert string data into date and time, to week, month, etc., including generation date

#Suppose there is already a string as follows:

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" )

#Convert to date:

dmonth<-(d1,"%Y-%m-%d")# Some people like to write dates as "2011/07/31", and at this time, they only need to change - to / i.e. (d1,"%Y/%m/%d")

#Convert to week and month:

month1<-months(dmonth)# converted to month
week1<-weekdays(dmonth)# converted to week

#Time comparison (In fact, R automatically sorts the time. Interested students can try this sentence (dmonth) <Convert time into data, and it can be calculated that R regards which day it is the first day>)

#If there is already a data set

data1<-data[dmonth<="2011-12-30"]# Extract data from data set 11 years ago

#Generate date data

#I just mentioned how to convert time into numbers, and it is definitely OK to convert numbers into dates

d2<-(23768, origin = "1900-01-01")# Friends who are interested can know what origin is R, right?

#Use seq function

date1<- seq(from=('2006/01/01'), to=('2016/08/03'), by='1 day')#If you want to generate a month or week, just change day to month or week

#Return the current date or system time:

()#date

date()#Specific time