read.table()
: 테이블 포맷으로 되어 있는 파일을 읽어 데이터프레임을 반환한다.read.csv()
: 값이 comma 로 구분되어 있는 파일(“.csv”)을 읽어 데이터프레임을 반환한다.# Read tabular data into R as a dataframe
commute <- read.table("data/commute.txt", header = TRUE, sep = "\t", stringsAsFactors = F)
# 첫 6행 읽기
head(commute)
## Date Day Depart Arrive
## 1 2010-06-15 T 5:37 6:05
## 2 2010-06-16 W 5:08 5:35
## 3 2010-06-17 H 4:58 5:17
## 4 2010-06-18 F 5:01 5:17
## 5 2010-06-21 M 5:17 5:41
## 6 2010-06-25 F 5:21 5:38
commute.txt 데이터를 통해 요일별 출근소요시간을 파악해보자. 이 과정에서 데이터 자료형 변환과 날짜시간 자료형에 대해 함께 살펴보기로 한다.
# commute data frame 구조 확인
str(commute)
## 'data.frame': 63 obs. of 4 variables:
## $ Date : chr "2010-06-15" "2010-06-16" "2010-06-17" "2010-06-18" ...
## $ Day : chr "T" "W" "H" "F" ...
## $ Depart: chr "5:37" "5:08" "4:58" "5:01" ...
## $ Arrive: chr "6:05" "5:35" "5:17" "5:17" ...
R의 날짜 자료형으로는 “Date” 객체 이외에 “POSIXct”, “POSIXlt” 등이 있으며, as.POSIXct 함수를 통해 문자 변수를 변환할 수 있다.
# Date 와 Depart, Date 와 Arrive 컬럼을 각각 합쳐서 POSIXct 형으로 변환
?POSIXct
as.POSIXct("2018/11/06 4:20")
## [1] "2018-11-06 04:20:00 KST"
as.POSIXlt("2018/11/06 4:20")
## [1] "2018-11-06 04:20:00 KST"
Dep <- as.POSIXct(paste(commute$Date, commute$Depart)) # paste() : 문자열 합치기
Arr <- as.POSIXct(paste(commute$Date, commute$Arrive))
# POSIXct 형으로 변환한 이후에는 기본 연산으로 시간차이를 계산할 수 있음
Arr - Dep
## Time differences in mins
## [1] 28 27 19 16 24 17 21 21 17 17 19 21 23 17 18 27 17 20 21 21 22 34 29
## [24] 29 26 22 29 28 16 19 36 27 21 22 22 19 12 21 25 27 23 19 20 29 27 26
## [47] 23 28 19 15 21 21 23 28 21 18 19 32 18 18 25 19 21
# 시간차이 계산한 것을 Diff 변수에 할당
Diff <- Arr - Dep
# commute 데이터프레임에 Diff 변수 합치기
new_commute <- cbind(commute, Diff) # cbind : column 기준으로 합치기
head(new_commute)
## Date Day Depart Arrive Diff
## 1 2010-06-15 T 5:37 6:05 28 mins
## 2 2010-06-16 W 5:08 5:35 27 mins
## 3 2010-06-17 H 4:58 5:17 19 mins
## 4 2010-06-18 F 5:01 5:17 16 mins
## 5 2010-06-21 M 5:17 5:41 24 mins
## 6 2010-06-25 F 5:21 5:38 17 mins
“difftime” 자료형으로 생성된 Diff 컬럼을 다시 숫자형으로 변경하여 Boxplot 그래프를 그린다.
# new_commute$Diff 의 데이터타입 확인 : difftime class 는 그래프를 그릴 수 없음
class(new_commute$Diff)
## [1] "difftime"
# Diff 벡터를 숫자형으로 변환하여 new_commute 데이터프레임에 열 추가
Diff <- as.numeric(Diff)
new_commute <- cbind(commute, Diff)
str(new_commute)
## 'data.frame': 63 obs. of 5 variables:
## $ Date : chr "2010-06-15" "2010-06-16" "2010-06-17" "2010-06-18" ...
## $ Day : chr "T" "W" "H" "F" ...
## $ Depart: chr "5:37" "5:08" "4:58" "5:01" ...
## $ Arrive: chr "6:05" "5:35" "5:17" "5:17" ...
## $ Diff : num 28 27 19 16 24 17 21 21 17 17 ...
# 상자그림 그리기
boxplot(Diff~Day, new_commute,
main = "요일별 출근 소요시간",
ylab = "min")
# Day 컬럼의 자료형을 요인으로 바꾸고 레벨의 순서를 지정
new_commute$Day <- factor(new_commute$Day, levels = c("M", "T", "W", "H", "F"))
str(new_commute)
## 'data.frame': 63 obs. of 5 variables:
## $ Date : chr "2010-06-15" "2010-06-16" "2010-06-17" "2010-06-18" ...
## $ Day : Factor w/ 5 levels "M","T","W","H",..: 2 3 4 5 1 5 1 4 5 1 ...
## $ Depart: chr "5:37" "5:08" "4:58" "5:01" ...
## $ Arrive: chr "6:05" "5:35" "5:17" "5:17" ...
## $ Diff : num 28 27 19 16 24 17 21 21 17 17 ...
# 상자그림 그리기
boxplot(Diff~Day, new_commute,
main = "요일별 출근 소요시간 (요일순)",
ylab = "min")
전달력을 높이기 위해 M, T, W, H, F 를 Monday, Tuesday… 로 변경함.
levels(new_commute$Day)[levels(new_commute$Day)=="M"] <- "Monday"
levels(new_commute$Day)[levels(new_commute$Day)=="T"] <- "Tuesday"
levels(new_commute$Day)[levels(new_commute$Day)=="W"] <- "Wednesday"
levels(new_commute$Day)[levels(new_commute$Day)=="H"] <- "Thursday"
levels(new_commute$Day)[levels(new_commute$Day)=="F"] <- "Friday"
boxplot(Diff~Day, new_commute,
main = "요일별 출근 소요시간 (요일순)",
ylab = "min")
[Dr.Breheny 의 저녁 출근 소요시간 패턴읽기]
class(new_commute$Date)
## [1] "character"
new_commute$Date <- as.Date(new_commute$Date)
str(new_commute)
## 'data.frame': 63 obs. of 5 variables:
## $ Date : Date, format: "2010-06-15" "2010-06-16" ...
## $ Day : Factor w/ 5 levels "Monday","Tuesday",..: 2 3 4 5 1 5 1 4 5 1 ...
## $ Depart: chr "5:37" "5:08" "4:58" "5:01" ...
## $ Arrive: chr "6:05" "5:35" "5:17" "5:17" ...
## $ Diff : num 28 27 19 16 24 17 21 21 17 17 ...
library(googleVis)
## Creating a generic function for 'toJSON' from package 'jsonlite' in package 'googleVis'
##
## Welcome to googleVis version 0.6.2
##
## Please read Google's Terms of Use
## before you start using the package:
## https://developers.google.com/terms/
##
## Note, the plot method of googleVis will by default use
## the standard browser to display its output.
##
## See the googleVis package vignettes for more details,
## or visit http://github.com/mages/googleVis.
##
## To suppress this message use:
## suppressPackageStartupMessages(library(googleVis))
t1 <- gvisMotionChart(new_commute, idvar="Date", timevar="Diff",
option=list(width=1000,height=500))
plot(t1)
## starting httpd help server ...
## done
as.Date("2018-09-01")
## [1] "2018-09-01"
as.Date("2018/09/01")
## [1] "2018-09-01"
as.Date("09/01/2018") # "0009-01-20" 으로 변환됨
## [1] "0009-01-20"
as.Date("09/01/2018", format = "%m/%d/%Y")
## [1] "2018-09-01"
as.Date("09/01/18", format = "%m/%d/%y" )
## [1] "2018-09-01"
그 외의 형식으로 되어 있는 문자열을 날짜로 변환하려면?
# ISOdate 함수로 POSIXct 객체로 변환
ISOdate(2018, 9, 1)
## [1] "2018-09-01 12:00:00 GMT"
# Date 객체로 변환
as.Date(ISOdate(2018, 9, 1))
## [1] "2018-09-01"
Sys.time() 함수를 통해 현재 시스템 시간정보를 받아내면 POSIXct 클래스가 생성된다.
문자열을 받아 as.POSIXlt(), as.POSIXct() 함수를 사용해서 시간자료형으로 변환시킨다. POSIXlt 자료형으로 변환시킨 경우 리스트로 저장되어 있어 시, 분, 초, 요일등 하위 원소값으로 뽑아낼 수 있다.
Sys.Date() # 현재 날짜를 반환하는 함수
## [1] "2018-11-05"
class(Sys.Date()) # Sys.Date() 함수는 Date 객체를 반환
## [1] "Date"
as.POSIXlt(Sys.Date())$wday
## [1] 1
Sys.time() # 현재 날짜시간을 반환하는 함수
## [1] "2018-11-05 14:11:05 KST"
class(Sys.time()) # Sys.time() 함수는 POSIXct 객체를 반환
## [1] "POSIXct" "POSIXt"
names(unclass(as.POSIXlt(Sys.time()))) # unclass로 리스트의 이름을
## [1] "sec" "min" "hour" "mday" "mon" "year" "wday"
## [8] "yday" "isdst" "zone" "gmtoff"
as.POSIXlt(Sys.time())$mon # 월 (0-11, 0 = 1월)
## [1] 10
as.POSIXlt(Sys.time())$mday # 해당 달의 몇번째 날 (1-31)
## [1] 5
as.POSIXlt(Sys.time())$wday # 해당 주의 몇번째 날 (0=6, 0= 일요일)
## [1] 1
as.POSIXlt(Sys.time())$yday # 해당 해의 몇번째 날 (0-365, 0 = 1월1일)
## [1] 308
when <- as.Date(ISOdate(2018, 9, 1))
as.POSIXlt(when)$mon # 월 (0-11, 0 = 1월)
## [1] 8
as.POSIXlt(when)$mday # 해당 달의 몇번째 날 (1-31)
## [1] 1
as.POSIXlt(when)$wday # 해당 주의 몇번째 날 (0=6, 0= 일요일)
## [1] 6
as.POSIXlt(when)$yday # 해당 해의 몇번째 날 (0-365, 0 = 1월1일)
## [1] 243
KST(Korea Standard Time, 한국 표준시) = UTC (Universal Time Coordinated, 세계협정시) +9
Sys.timezone()
## [1] "Asia/Seoul"
as.POSIXlt(Sys.time(), "GMT")
## [1] "2018-11-05 05:11:05 GMT"
as.POSIXlt(Sys.time(), "EST5EDT") # the current time in New York
## [1] "2018-11-05 00:11:05 EST"
as.POSIXlt(Sys.time(), "EST" ) # ditto, ignoring DST
## [1] "2018-11-05 00:11:05 EST"
as.POSIXlt(Sys.time(), "HST") # the current time in Hawaii
## [1] "2018-11-04 19:11:05 HST"
as.POSIXlt(Sys.time(), "Australia/Darwin")
## [1] "2018-11-05 14:41:05 ACST"
RMD 파일에서 R code 추출
# library(knitr)
# purl("1-1a.Timedata.Rmd", encoding = "UTF-8")