본문 바로가기
공부/R Programming

R 기초; 날짜와 시간

by 혼밥맨 2021. 1. 15.
반응형

R 기초; 날짜와 시간

 

# 날짜 출력

Sys.Date()

[1] "2021-01-15"

 

# 날짜 + 시간 출력

date()

[1] "Fri Jan 15 09:00:40 2021"

 

# 날짜 + 시간 출력

Sys.time()

[1] "2021-01-15 09:00:44 KST"

 

class(Sys.Date())

[1] "Date"

 

as.Date("2025-12-31")

[1] "2025-12-31"

as.Date("2025/12/31")

[1] "2025-12-31"

as.Date("12/31/2025")

Error in charToDate(x) : 문자열이 표준서식을 따르지 않습니다

 

as.Date("12/31/2025", format("%m/%d/%y))

[1] "2020-12-31"

 

# 도움말 보기

?strptime

 

d <- as.Date("2025-12-31")

d

format(d, format="%m/%d/%y")

[1] "12/31/25"

 

today <- Sys.Date()

today

[1] "2021-01-15"

 

format(today, format="%Y/%m/%d")

[1] "2021/01/15"

format(today, format="%Y/%m/%d" %A")

[1] "2021/01/15 금요일"

format(today, format="%Y/%m/%d" %a")

[1] "2021/01/15 금"

 

d <- as.Date("2025-12-31")

d

 

# 특정 날짜의 요일 알아내기

weekdays(d)

[1] "수요일"

 

d

[1] "2025-12-31"

d+7

[1] "2026-01-07"

d + 1:7

[1] "2026-01-01" "2026-01-02" "2026-01-03" "2026-01-04" "2026-01-05" "2026-01-06" "2026-01-07"

 

weekdays(d + 1:7)

[1] "목요일" "금요일" "토요일" "일요일" "월요일" "화요일" "수요일"

 

start <- as.Date("2025-01-01")

end <- as.Date("2025-01-31")

 

seq(from=start, to=end, by=1)

 [1] "2025-01-01" "2025-01-02" "2025-01-03" "2025-01-04" "2025-01-05" "2025-01-06" "2025-01-07" "2025-01-08" "2025-01-09" "2025-01-10" "2025-01-11"
[12] "2025-01-12" "2025-01-13" "2025-01-14" "2025-01-15" "2025-01-16" "2025-01-17" "2025-01-18" "2025-01-19" "2025-01-20" "2025-01-21" "2025-01-22"
[23] "2025-01-23" "2025-01-24" "2025-01-25" "2025-01-26" "2025-01-27" "2025-01-28" "2025-01-29" "2025-01-30" "2025-01-31"

 

 

seq(from=start, by=1, length.out=7)

[1] "2025-01-01" "2025-01-02" "2025-01-03" "2025-01-04" "2025-01-05" "2025-01-06" "2025-01-07"

 

seq(from=start, by="7 days", length.out=7)

seq(from=start, by="week", length.out=7)

[1] "2025-01-01" "2025-01-08" "2025-01-15" "2025-01-22" "2025-01-29" "2025-02-05" "2025-02-12"

 

seq(from=start, by="month", length.out=12)

[1] "2025-01-01" "2025-02-01" "2025-03-01" "2025-04-01" "2025-05-01" "2025-06-01" "2025-07-01" "2025-08-01" "2025-09-01" "2025-10-01" "2025-11-01"
[12] "2025-12-01"

 

seq(from=start, by="3 months", length.out=4)

[1] "2025-01-01" "2025-04-01" "2025-07-01" "2025-10-01"

 

seq(from=as.Date("2025-01-30"), by="month", length.out=6)

[1] "2025-01-30" "2025-03-02" "2025-03-30" "2025-04-30" "2025-05-30" "2025-06-30"

 

start <- as.Date("2025-01-01")

qrt <- seq(from=start, by="3 months", length.out=4)

months(qrt)

[1] "1월"  "4월"  "7월"  "10월"

quarters(qrt)

[1] "Q1" "Q2" "Q3" "Q4"

 

Sys.getlocale()

[1] "LC_COLLATE=Korean_Korea.949;LC_CTYPE=Korean_Korea.949;LC_MONETARY=Korean_Korea.949;LC_NUMERIC=C;LC_TIME=Korean_Korea.949"

 

Sys.setlocale("LC_TIME", "C")

months(qrt)

 

Sys.setlocale("LC_TIME", "Korean_Korea.949")

[1] "Korean_Korea.949"

months(qrt)

[1] "1월"  "4월"  "7월"  "10월"

Sys.setlocale()

[1] "LC_COLLATE=Korean_Korea.949;LC_CTYPE=Korean_Korea.949;LC_MONETARY=Korean_Korea.949;LC_NUMERIC=C;LC_TIME=Korean_Korea.949"

as.POSIXct("2025/03/15, 15:03:04", format="%Y/%m/%d, %H:%M:%S")

[1] "2025-03-15 15:03:04 KST"

 

pct <- as.POSIXct("2025/03/15, 15:03:04", format="%Y/%m/%d, %H:%M:%S")

pct

[1] "2025-03-15 15:03:04 KST"

class(pct)

[1] "POSIXct" "POSIXt" 

as.integer(pct)

[1] 1742018584

 

plt <- as.POSIXlt("2025/03/15, 15:03:04", format="%Y/%m/%d, %H:%M:%S")

plt

[1] "2025-03-15 15:03:04 KST"

class(plt)

[1] "POSIXlt" "POSIXt" 

unclass(plt)

$sec
[1] 4

$min
[1] 3

$hour
[1] 15

$mday
[1] 15

$mon
[1] 2

$year
[1] 125

$wday
[1] 6

$yday
[1] 73

$isdst
[1] 0

$zone
[1] "KST"

$gmtoff
[1] NA

 

plt$mday

[1] 15

plt$mon

[1] 2

plt$year

[1] 125

plt$wday

[1] 6

plt$hour

[1] 15

 

dposix <- as.Date("2025-12-31")

dposix

[1] "2025-12-31"

as.POSIXlt(dposix)$wday

[1] 3

as.POSIXlt(dposix)$yday

[1] 364

as.POSIXlt(dposix)$year + 1900

as.POSIXlt(dposix)$mon + 1

 

strptime("2025-12-31", format="%Y-%m-%d")

[1] "2025-12-31 KST"

class(strptime("2025-12-31", format="%Y-%m-%d"))

[1] "POSIXlt" "POSIXt" 

 

moon <- as.POSIXct("1969/07/20, 20:17:39", format("%Y/%m/%d, %H:%M:%S", tz="UTC")

moon

foramt(moon, "The time of the Apollo moon landing was %Y/%m/%d, at %H:%M:%S")

 

years <- c(2025, 2026, 2027, 2028)

months <- c(1, 4, 7, 10)

ISOdate(years, months, days)

 

ISOdate(years, months, days)

 

jdate <- as.Date("2025-12-31")

jdate

[1] "2025-12-31"

as.integer(jdate)

[1] 20453

julian(jdate)

[1] 20453
attr(,"origin")
[1] "1970-01-01"

 

as.integer(as.Date("1970-01-01"))

[1] 0

as.integer(as.Date("1969-12-31"))

[1] -1

 

as.Date(as.integer(jdate), origin="1970-01-01")

 

moon

class(moon)

moon + 60*60*2

 

moon + 60*60*24*7

 

today <- Sys.Date()

Dooly <- as.Date("1983-04-22")

difftime(today, Dooly, units="days")

Time difference of 13783 days

difftime(today, Dooly, units="weeks")

Time difference of 1969 weeks

 

반응형

'공부 > R Programming' 카테고리의 다른 글

R 기초; 출력  (0) 2021.01.15
R 기초; 입력  (0) 2021.01.15
R 기초; 웹 스크레이핑 stringr 패키지  (0) 2021.01.15
R 기초; 웹스크레이핑 base 패키지  (0) 2021.01.14
R 기초; 텍스트  (0) 2021.01.13

댓글