<- "2/11/1962"
d1 d1
[1] "2/11/1962"
Dr. Peng Zhao (✉ peng.zhao@xjtlu.edu.cn)
Department of Health and Environmental Sciences
Xi’an Jiaotong-Liverpool University
Character:
<- "2/11/1962"
d1 d1
[1] "2/11/1962"
+ 1 d1
Advantages:
Disadvantages:
Numeric:
Advantages:
Disadvantages:
Date/time format:
<- Sys.Date()
d2 d2
[1] "2023-03-02"
<- Sys.time()
t2 t2
[1] "2023-03-02 11:22:59 CST"
+ 1 d2
[1] "2023-03-03"
+ 1 t2
[1] "2023-03-02 11:23:00 CST"
class(d2)
[1] "Date"
class(t2)
[1] "POSIXct" "POSIXt"
Paired functions: as.Date()
and format()
.
<- as.Date("2/11/1962", format="%d/%m/%Y" )
d3 as.numeric(d3)
+ 2617
d3
<- as.Date( "2/11/1962", format="%m/%d/%Y" )
d4
format(d3, '%Y %m %d')
format(d3, "%A %d %B %Y")
library('Epi')
data("diet")
help(diet)
str(diet)
<- diet$dox[1]
bdat format(bdat, format="%A %d %B %Y")
$dox2 <- format(diet$dox, format="%A %d %B %Y")
diet
+ 1
bdat max(diet$dox)
range(diet$dox)
mean(diet$dox)
median(diet$dox)
diff(range(diet$dox))
difftime(min(diet$dox), max(diet$dox), units = "weeks")
::cal.yr(bdat)
Epi<- Epi::cal.yr(diet)
diet2 str(diet2)
Universal functions: strptime()
and format()
:
<- '1994-09-22 20:30:00'
bd class(bd)
<- strptime(x = bd, format = '%Y-%m-%d %H:%M:%S', tz = "Asia/Shanghai")
bdtime
class(bdtime)
$wday
bdtimeunclass(bdtime)
format(bdtime, format = '%d.%m.%Y')
Other functions:
date()
Sys.Date()
Sys.time()
+ 1
bdtime <- strptime(
bdtime2 '1995-09-01 7:30', format = '%Y-%m-%d %H:%M',
tz = 'Asia/Shanghai')
- bdtime
bdtime2 difftime(time1 = bdtime2, time2 = bdtime, units = 'secs')
mean(c(bdtime, bdtime2))
Which day of the year is it?
Convert it as “Year, Month in English and Date, Weekday in English”, such as “2023, January 1, Sunday”.
Suppose we get to know each other at 15:00, September 13, 2022. How many days have we known each other until now? How many hours? How many seconds?
The anniversary is September 13, 2023. What day is it? How about in 2024? Plot a graph for September 13 of each year, with the weekday as x and the year as y.
Create a new column “Date”, showing the date in the format of “Year-month-day”, such as “2023-01-31”.
Plot a graph for each atmospheric variable (i.e. ozone in ppb, solar radiation in W m-2, wind speed in m s-1, air temperature in °C) against the date.
Create a new column “Weekday”, showing the day in the week (1 as Monday and 7 as Sunday).
Calculate the mean values of each atmospheric variable for each weekday.
Plot a graph for each atmospheric variable with the mean values against the weekdays.