본문 바로가기
데이터 분석/공공 데이터

2013년 ~ 2018년 3월 타슈 데이터 전처리

by 레드홀스 2018. 12. 6.
대전시 공영자전거 타슈 데이터 전처리 (Tashu data Pre-processing)




1. Introduction

이 문서는 2013년 1월 1일부터 2018년 3월 31일까지 대전시 공영 자전거 타슈 이용 데이터를 전처리하는 과정을 담았습니다.

먼저 데이터에 대해 설명하고 문제점이 무엇인지 확인 후 어떻게 처리할 것인지 방향성을 잡고 dplyr를 비롯한 여러 Package들을 이용해서 전처리 하는 순서로 구성되어 있습니다.


Data 설명

타슈는 이 링크(공공데이터 포털)Excel 혹은 csv 형식으로 공개되어 있는 정형 데이터지만 자세히 살펴보면 각 데이터별로 변수명과 값(Element)들의 입력방식, 속성들이 다릅니다. (아마 담당자가 바뀌면서 저장 방식도 각각 다른 것이 원인인 것 같습니다.)

이런 경우 분석 자체가 불가능하기 때문에 깔끔하게 정돈된 데이터로 변환하도록 하겠습니다.

데이터 이름이 한글이라 R로 불러들일때 오류가 일어나서 미리 데이터 이름을 바꾸고 확장자를 모두 csv로 맞춰놨음을 알려드립니다.

전처리가 끝난 데이터는 .csv가 아니라 .RData로 저장했으며 필요하신 분들은 댓글로 요청 해주시기 바랍니다.

공개되어 있는 데이터는 2015년 하반기 까지인데 2018년 4월에 해당 데이터 담당자분께 연락드려서 개인 메일로 받아서 작업했음을 알려드립니다.

2017년 3분기(7월 ~ 10월) 데이터가 2분기와 똑같은데 파일 명만 다른 오류가 있음을 발견했습니다. 관계자분께 다시 데이터 받아서 작업 후 수정하도록 하겠습니다.




2. Groundwork : 준비작업

분석하기 전에 작업 폴더를 지정하고 필요한 데이터를 불러오는 등의 준비작업들을 먼저 하겠습니다.


2.1 Working directory setting, Data input : 작업폴더 지정, 데이터 불러오기

rm(list = ls())

setwd("C:/Users/LG/Documents/Selfstudy/Data/raw_data_tashu")
getwd()
## [1] "C:/Users/LG/Documents/Selfstudy/Data/raw_data_tashu"
# 2013년도 
raw.tashu.13.1 <- read.csv("1301_06.csv", header = T)
raw.tashu.13.2 <- read.csv("1306_12.csv", header = T)

# 2014년도
raw.tashu.14.1 <- read.csv("1401_06.csv", header = T)
raw.tashu.14.2 <- read.csv("1407_12.csv", header = T)

# 2015년도
raw.tashu.15.1 <- read.csv("1501.csv", header = T)
raw.tashu.15.2 <- read.csv("1504.csv", header = T)
raw.tashu.15.3 <- read.csv("1507.csv", header = T)
raw.tashu.15.4 <- read.csv("1510.csv", header = T)

# 2016년도
raw.tashu.16.1 <- read.csv("1601.csv", header = T)
raw.tashu.16.2 <- read.csv("1604.csv", header = T)
raw.tashu.16.3 <- read.csv("1607.csv", header = T)
raw.tashu.16.4 <- read.csv("1610.csv", header = T)

# 2017년도
raw.tashu.17.1 <- read.csv("1701.csv", header = T)
raw.tashu.17.2 <- read.csv("1704.csv", header = T)
raw.tashu.17.3 <- read.csv("1707.csv", header = T)
raw.tashu.17.4 <- read.csv("1710.csv", header = T)

# 2018년도
raw.tashu.18 <- read.csv("1801.csv", header = T)


2.2 Packages : 필요한 패키지 장착

전처리에 필요한 패키지들을 장착합니다. 각 목적별로 주석을 달아놨으니 참고하시기 바랍니다.

# Data assesment : 데이터 확인 
library(DT)         # DT::datatable() : 웹 형식으로 전체 데이터 확인 

# Data Pre-processing : 전처리 
library(dplyr)      # Used in almost data handling : 거의 모든 전처리에 사용
library(lubridate)  # Time series data handling : 시계열 데이터 전처리 




3. Data assesment, Problem setting, Pre-processing

EDA단계에서는 dim()과 같은 간단한 함수들만을 사용해서 아래 사항들을 확인할 것입니다.

  • 관측치, 변수의 개수
  • 각 데이터의 변수의 이름, 속성
  • 이상치, 결측치

이후 과정을 보시면 아시겠지만 데이터들의 변수명과 입력 형태가 달라서 모두 하나로 묶어서 한 번에 전처리를 하지 못 합니다.

따라서 각각의 데이터들을 전처리 한 후 같은 형태를 맞춰준 뒤에 하나로 bind 하는 흐름으로 갈 것 입니다.


3.1 2013년 상반기

데이터 확인

head(raw.tashu.13.1)
##   IS_MEMBER RENT_STATION   RENT_DATE RETURN_STATION RETURN_DATE
## 1        No           43 2.01301e+13             34 2.01301e+13
## 2        No            2 2.01301e+13             10 2.01301e+13
## 3        No          106 2.01301e+13            105 2.01301e+13
## 4       Yes            4 2.01301e+13              4 2.01301e+13
## 5        No           21 2.01301e+13            105 2.01301e+13
## 6        No           90 2.01301e+13             91 2.01301e+13

관측치 413066, 변수 5개로 이루어진 데이터임을 알 수 있습니다.


# 지수표현을 정수로 바꿔주기 위해서 아래 옵션을 설정한다. 
options("scipen" = 100)

# 관측치, 변수 개수, 변수명, 값 입력형태 확인 
str(raw.tashu.13.1)
## 'data.frame':    413066 obs. of  5 variables:
##  $ IS_MEMBER     : Factor w/ 2 levels "No","Yes": 1 1 1 2 1 1 1 2 2 2 ...
##  $ RENT_STATION  : int  43 2 106 4 21 90 13 1 1 1 ...
##  $ RENT_DATE     : num  20130101055603 20130101060406 20130101105305 20130101112223 20130101113953 ...
##  $ RETURN_STATION: int  34 10 105 4 105 91 30 1 2 2 ...
##  $ RETURN_DATE   : num  20130101060217 20130101061859 20130101105743 20130101121753 20130101114943 ...

각 변수의 의미는 아래 표와 같습니다.

변수명 의미 전처리 의견
IS_MEMBER 타슈 회원인지(YES) 아닌지(NO) 다른 데이터에는 없는 변수이므로 제거
RENT_STATION 대여한 장소 factor로 변환
RENT_DATE 대여 일시(년, 월, 일, 시, 분, 초) Date로 변환
RETURN_STATION 반납한 장소 factor로 변환
RETURN_DATE 반납 일시(년, 월, 일, 시, 분, 초) Date로 변환


각 변수별로 결측치가 있는지, 있다면 그 비율이 얼마나 되는지 아래 코드를 통해 확인해보도록 하겠습니다.

raw.tashu.13.1 %>%
  summarize_all(funs(sum(is.na(.))/n()))
##   IS_MEMBER RENT_STATION RENT_DATE RETURN_STATION RETURN_DATE
## 1         0            0         0              0           0

다행히 2013년 상반기 데이터에는 결측치가 아예 존재하지 않습니다. 그럼 이제 바로 전처리를 하도록 하겠습니다.


전처리

회원구분을 나타내는 RENT_STATION은 의미없는 변수이면서 2014년부터 관리하지 않은 변수이므로 제거합니다.

각 변수의 이름을 모두 소문자로 변환해주고 대여, 반납한 장소는 factor로 바꿔줍니다.

14자리 숫자로 구성되어있는 대여시간과 반납시간은 lubridate패키지를 이용해서 시간 (Date 혹은 POSIXct) 변수로 변환합니다.

그리고 위의 두 변수들을 이용해서 대여 ~ 반납까지 기간이 얼마나 되는지 나타내는 length.rent.return 파생변수를 생성하고 당일날 반납하지 않은 관측치(이상치, Outlier)들을 모두 제거합니다.

그런 뒤 초기에 주어진 변수들 중 대여장소, 대여일시, 반납장소, 반납일시 4개의 변수만 선택해서 저장합니다.

tashu.13.1 <- raw.tashu.13.1[, -1] %>%
  # 첫 번째 변수(회원구분) 제거
  # 변수명 변경 - mutate 사용하면 파생변수 생김.
  rename(rent.station   = RENT_STATION,
         rent.date      = RENT_DATE,
         return.station = RETURN_STATION,
         return.date    = RETURN_DATE) %>%
  # 대여소 factor 속성으로 변환 
  mutate(rent.station = factor(rent.station),
         return.station = factor(return.station),
         # 대여일시, 반납일시 년, 월, 일, 시, 분, 초까지 다 추출
         rent.date = lubridate::ymd_hms(rent.date),
         return.date = lubridate::ymd_hms(return.date),
         # 대여일시, 반납일시에서 년, 월. 일만 추출 
         rent.ymd = lubridate::ymd(substr(.$rent.date, 1, 8)),
         return.ymd = lubridate::ymd(substr(.$return.date, 1, 8)),
         # 반납 - 대여 차이나는 일수만 계산해서 파생변수 생성 
         length.rent.return = days(return.ymd - rent.ymd)@day) %>%
  # 반납까지 하루 이상 걸리는 이상치들 모두 제거 - 3분위수가 1이기 때문 
  filter(length.rent.return < 1) %>%
  # 필요한 변수들만 선택해서 Data frame 으로 저장 
  select("rent.station", "rent.date",
         "return.station", "return.date")

전처리 후 데이터 확인

str(tashu.13.1)
## 'data.frame':    398326 obs. of  4 variables:
##  $ rent.station  : Factor w/ 114 levels "1","2","3","4",..: 43 2 106 4 21 90 13 1 1 1 ...
##  $ rent.date     : POSIXct, format: "2013-01-01 05:56:03" "2013-01-01 06:04:06" ...
##  $ return.station: Factor w/ 118 levels "1","2","3","4",..: 34 10 105 4 105 91 30 1 2 2 ...
##  $ return.date   : POSIXct, format: "2013-01-01 06:02:17" "2013-01-01 06:18:59" ...
summary(tashu.13.1)
##   rent.station      rent.date                   return.station  
##  3      : 23722   Min.   :2013-01-01 05:56:03   3      : 24311  
##  56     : 13545   1st Qu.:2013-03-30 09:25:47   56     : 13484  
##  17     : 12214   Median :2013-05-05 14:33:51   17     : 12297  
##  31     : 11852   Mean   :2013-04-28 19:34:17   31     : 11972  
##  33     : 10254   3rd Qu.:2013-06-01 17:53:17   33     : 10333  
##  32     :  9919   Max.   :2013-06-30 23:55:14   32     :  9977  
##  (Other):316820                                 (Other):315952  
##   return.date                 
##  Min.   :2013-01-01 06:02:17  
##  1st Qu.:2013-03-30 09:46:07  
##  Median :2013-05-05 15:21:42  
##  Mean   :2013-04-28 20:02:44  
##  3rd Qu.:2013-06-01 18:26:11  
##  Max.   :2013-06-30 23:59:56  
## 

나머지 데이터들도 모두 위와 같은 형태로 전처리 할 것입니다.


3.2 2013년 하반기

데이터 확인

head(raw.tashu.13.2)
##   IS_MEMBER RENT_STATION      RENT_DATE RETURN_STATION    RETURN_DATE
## 1       Yes           81 20130601000009             22 20130601004902
## 2        No            3 20130601000011              3 20130601000309
## 3       Yes           81 20130601000114             22 20130601004911
## 4       Yes           91 20130601000152             84 20130628165406
## 5       Yes           68 20130601000440             32 20130601010141
## 6       Yes           68 20130601000505             32 20130601010130
str(raw.tashu.13.2)
## 'data.frame':    621975 obs. of  5 variables:
##  $ IS_MEMBER     : Factor w/ 2 levels "No","Yes": 2 1 2 2 2 2 2 2 2 2 ...
##  $ RENT_STATION  : int  81 3 81 91 68 68 68 79 79 79 ...
##  $ RENT_DATE     : num  20130601000009 20130601000011 20130601000114 20130601000152 20130601000440 ...
##  $ RETURN_STATION: int  22 3 22 84 32 32 32 22 22 22 ...
##  $ RETURN_DATE   : num  20130601004902 20130601000309 20130601004911 20130628165406 20130601010141 ...
summary(raw.tashu.13.2)
##  IS_MEMBER     RENT_STATION      RENT_DATE              RETURN_STATION  
##  No :382834   Min.   :  1.00   Min.   :20130601000000   Min.   :  1.00  
##  Yes:239141   1st Qu.: 17.00   1st Qu.:20130717214600   1st Qu.: 17.00  
##               Median : 39.00   Median :20130905203400   Median : 39.00  
##               Mean   : 46.41   Mean   :20130880755800   Mean   : 46.42  
##               3rd Qu.: 66.00   3rd Qu.:20131020174400   3rd Qu.: 66.00  
##               Max.   :201.00   Max.   :20131231235900   Max.   :201.00  
##               NA's   :31                                NA's   :1184    
##   RETURN_DATE            
##  Min.   :20130601000300  
##  1st Qu.:20130717223900  
##  Median :20130905220100  
##  Mean   :20130884923600  
##  3rd Qu.:20131020183600  
##  Max.   :20140430163800  
##  NA's   :19

summary()결과를 보시면 아시겠지만 2013년 하반기 데이터인데 대여시간에 2013년 상반기 데이터가 섞여있습니다. 때문에 이런 잡음(Noise)들도 모두 제거 해줘야 합니다.


결측치 확인

raw.tashu.13.2 %>%
  summarize_all(funs(sum(is.na(.))/n()))
##   IS_MEMBER  RENT_STATION RENT_DATE RETURN_STATION   RETURN_DATE
## 1         0 0.00004984123         0    0.001903613 0.00003054785


전처리

# 결측치 제거 
raw.tashu.13.2 <- na.omit(raw.tashu.13.2)

tashu.13.2 <- raw.tashu.13.2[, -1] %>%
  # 첫 번째 변수(회원구분) 제거, 변수 이름 변경 
  rename(rent.station   = RENT_STATION,
         rent.date      = RENT_DATE,
         return.station = RETURN_STATION,
         return.date    = RETURN_DATE) %>%
  # 대여소 factor 속성으로 변환 
  mutate(rent.station = factor(rent.station),
         return.station = factor(return.station),
         # 대여일시, 반납일시 년, 월, 일, 시, 분, 초까지 다 추출
         rent.date = lubridate::ymd_hms(rent.date),
         return.date = lubridate::ymd_hms(return.date),
         # 대여일시, 반납일시에서 년, 월. 일만 추출 
         rent.ymd = lubridate::ymd(substr(.$rent.date, 1, 8)),
         return.ymd = lubridate::ymd(substr(.$return.date, 1, 8)),
         # 반납 - 대여 차이나는 일수만 계산해서 파생변수 생성 
         length.rent.return = days(return.ymd - rent.ymd)@day) %>%
  # 2013년 하반기이므로 상반기와 중복되는 값들은 제거해줌. - 약 1만건 정도 됨.
  filter(rent.date >= lubridate::ymd_hms("2013-07-01 00:00:00"),
         # 반납까지 하루 이상 걸리는 이상치들 모두 제거 - 3분위수가 1이기 때문 
         length.rent.return < 1) %>%
  # 필요한 변수들만 선택해서 Data frame 으로 저장 
  select("rent.station", "rent.date",
         "return.station", "return.date")

# 2013년 상, 하반기 데이터 병합 
tashu.2013 <- rbind(tashu.13.1, tashu.13.2)

# 작업이 끝난 데이터 제거
rm(list = c("raw.tashu.13.1", "raw.tashu.13.2", "tashu.13.1", "tashu.13.2"))

전처리 후 데이터 확인

str(tashu.2013)
## 'data.frame':    891635 obs. of  4 variables:
##  $ rent.station  : Factor w/ 145 levels "1","2","3","4",..: 43 2 106 4 21 90 13 1 1 1 ...
##  $ rent.date     : POSIXct, format: "2013-01-01 05:56:03" "2013-01-01 06:04:06" ...
##  $ return.station: Factor w/ 145 levels "1","2","3","4",..: 34 10 105 4 105 91 30 1 2 2 ...
##  $ return.date   : POSIXct, format: "2013-01-01 06:02:17" "2013-01-01 06:18:59" ...
summary(tashu.2013)
##   rent.station      rent.date                   return.station  
##  3      : 47247   Min.   :2013-01-01 05:56:03   3      : 48438  
##  56     : 28948   1st Qu.:2013-05-11 20:45:18   56     : 28899  
##  17     : 25470   Median :2013-07-18 10:57:43   17     : 25544  
##  31     : 24853   Mean   :2013-07-18 19:05:59   31     : 24947  
##  33     : 22009   3rd Qu.:2013-09-30 21:40:41   33     : 22176  
##  32     : 21552   Max.   :2013-12-31 23:58:16   32     : 21637  
##  (Other):721556                                 (Other):719994  
##   return.date                 
##  Min.   :2013-01-01 06:02:17  
##  1st Qu.:2013-05-11 21:26:22  
##  Median :2013-07-18 11:22:36  
##  Mean   :2013-07-18 19:33:13  
##  3rd Qu.:2013-09-30 22:10:55  
##  Max.   :2013-12-31 23:59:41  
## 


3.3 2014년 상반기

데이터 확인

head(raw.tashu.14.1)
##   회원구분 대여.스테이션.정보       대여일시 반납.스테이션.정보
## 1       No                105 20140101000005                105
## 2      Yes                 71 20140101000411                 45
## 3      Yes                 73 20140101000916                 80
## 4      Yes                 14 20140101001203                 43
## 5      Yes                 14 20140101001205                 25
## 6      Yes                 14 20140101001219                 17
##         반납일시 총대여시간
## 1 20140101000304          0
## 2 20140101002733          0
## 3 20140101001447          0
## 4 20140101002139          0
## 5 20140101011345          0
## 6 20140101001453          0

변수명이 한글로 되어있는게 보입니다. 따라서 2013년 데이터와 같은 변수명으로 변환해줘야 합니다.

그리고 갑자기 총대여시간이라는 변수가 보입니다. 아래 summary() 결과를 보시면 아시겠지만 정수로 나와있는데 이것의 단위가 불분명하고 위에서 만든 파생변수 length.rent.return가 더 자세하게 나와있기에 필요없습니다.

str(raw.tashu.14.1)
## 'data.frame':    587353 obs. of  6 variables:
##  $ 회원구분          : Factor w/ 2 levels "No","Yes": 1 2 2 2 2 2 2 2 2 2 ...
##  $ 대여.스테이션.정보: int  105 71 73 14 14 14 14 60 60 60 ...
##  $ 대여일시          : num  20140101000005 20140101000411 20140101000916 20140101001203 20140101001205 ...
##  $ 반납.스테이션.정보: int  105 45 80 43 25 17 25 32 56 56 ...
##  $ 반납일시          : num  20140101000304 20140101002733 20140101001447 20140101002139 20140101011345 ...
##  $ 총대여시간        : int  0 0 0 0 0 0 0 0 0 0 ...
summary(raw.tashu.14.1)
##  회원구분     대여.스테이션.정보    대여일시             
##  No :341673   Min.   :  1.00     Min.   :20140101000000  
##  Yes:245680   1st Qu.: 19.00     1st Qu.:20140323194800  
##               Median : 46.00     Median :20140430200100  
##               Mean   : 54.37     Mean   :20140436241900  
##               3rd Qu.: 86.00     3rd Qu.:20140530163300  
##               Max.   :146.00     Max.   :20140630235900  
##               NA's   :87                                 
##  반납.스테이션.정보    반납일시                총대여시간      
##  Min.   :  1.0      Min.   :20140101000300   Min.   :    0.00  
##  1st Qu.: 19.0      1st Qu.:20140323205400   1st Qu.:    0.00  
##  Median : 46.0      Median :20140430205000   Median :   12.00  
##  Mean   : 54.4      Mean   :20140436568200   Mean   :   23.33  
##  3rd Qu.: 86.0      3rd Qu.:20140530173000   3rd Qu.:   34.00  
##  Max.   :146.0      Max.   :20150323104500   Max.   :61919.00  
##  NA's   :1533       NA's   :10               NA's   :10
raw.tashu.14.1 %>%
  summarize_all(funs(sum(is.na(.))/n()))
##   회원구분 대여.스테이션.정보 대여일시 반납.스테이션.정보      반납일시
## 1        0       0.0001481222        0        0.002610015 0.00001702554
##      총대여시간
## 1 0.00001702554


전처리

# 사용할 변수들만 선택, 추출 
tashu.14.1 <- raw.tashu.14.1[, 2:5]

# 한글로 되어있는 변수명 변경 
names(tashu.14.1) <- c("rent.station", "rent.date",
                       "return.station", "return.date")

# 결측치 모두 제거 
tashu.14.1 <- na.omit(tashu.14.1)

# dplyr 패키지를 이용한 본격적인 전처리 
tashu.14.1 <- tashu.14.1 %>%
  # 대여소 factor 속성으로 변환 
  mutate(rent.station = factor(rent.station),
         return.station = factor(return.station),
         # 대여일시, 반납일시 년, 월, 일, 시, 분, 초까지 다 추출
         rent.date = lubridate::ymd_hms(rent.date),
         return.date = lubridate::ymd_hms(return.date),
         # 대여일시, 반납일시에서 년, 월. 일만 추출 
         rent.ymd = lubridate::ymd(substr(.$rent.date, 1, 8)),
         return.ymd = lubridate::ymd(substr(.$return.date, 1, 8)),
         # 반납 - 대여 차이나는 일수만 계산해서 파생변수 생성 
         length.rent.return = days(return.ymd - rent.ymd)@day) %>%
  # 반납까지 하루 이상 걸리는 이상치들 모두 제거 - 3분위수가 1이기 때문 
  filter(length.rent.return < 1) %>%
  # 필요한 변수들만 선택 추출 
  select("rent.station", "rent.date",
         "return.station", "return.date")


3.4 2014년 하반기

데이터 확인

head(raw.tashu.14.2)
##   대여.스테이션.정보        대여일시 반납.스테이션.정보        반납일시
## 1                103 2014-07-01 0:00                 97 2014-07-01 0:33
## 2                 89 2014-07-01 0:00                122 2014-07-01 1:11
## 3                  1 2014-07-01 0:00                  4 2014-07-01 0:05
## 4                 52 2014-07-01 0:00                 41 2014-07-01 0:21
## 5                103 2014-07-01 0:00                 97 2014-07-01 0:33
## 6                 60 2014-07-01 0:00                106 2014-07-01 0:39

2014년 하반기 데이터는 담당자가 바뀌신건지 자세한 이유는 모르겠지만 회원구분총대여시간 변수가 없습니다.

그리고 대여일시와 반납일시가 숫자가 아니라 YYYY-MM-DD HH:MM 형식의 문자로 되어있음을 알 수 있습니다.

str(raw.tashu.14.2)
## 'data.frame':    610062 obs. of  4 variables:
##  $ 대여.스테이션.정보: int  103 89 1 52 103 60 56 1 103 60 ...
##  $ 대여일시          : Factor w/ 162788 levels "2014-07-01 0:00",..: 1 1 1 1 1 1 1 1 2 2 ...
##  $ 반납.스테이션.정보: int  97 122 4 41 97 106 56 4 97 21 ...
##  $ 반납일시          : Factor w/ 163019 levels "2014-07-01 0:04",..: 20 45 2 9 20 24 10 1 20 22 ...
summary(raw.tashu.14.2)
##  대여.스테이션.정보             대여일시      반납.스테이션.정보
##  Min.   :  1.00     2014-08-12 23:35:    23   Min.   :  1.00    
##  1st Qu.: 19.00     2014-09-13 19:30:    23   1st Qu.: 19.00    
##  Median : 46.00     2014-08-22 22:24:    22   Median : 46.00    
##  Mean   : 54.21     2014-07-16 22:05:    21   Mean   : 54.21    
##  3rd Qu.: 86.00     2014-07-21 22:18:    21   3rd Qu.: 86.00    
##  Max.   :149.00     2014-08-09 20:08:    21   Max.   :149.00    
##                     (Other)         :609931                     
##              반납일시     
##  2014-07-15 23:53:    26  
##  2014-09-27 19:53:    24  
##  2014-09-27 21:55:    24  
##  2014-07-10 23:03:    23  
##  2014-07-15 18:37:    23  
##  2014-07-16 21:39:    23  
##  (Other)         :609919
raw.tashu.14.2 %>%
  summarize_all(funs(sum(is.na(.))/n()))
##   대여.스테이션.정보 대여일시 반납.스테이션.정보 반납일시
## 1                  0        0                  0        0

결측치는 없으므로 바로 변수명을 변환해주고 파생변수를 생성합니다.


전처리

# 변수명 변환 
colnames(raw.tashu.14.2) <- c("rent.station", "rent.date",
                              "return.station", "return.date")

tashu.14.2 <- raw.tashu.14.2 %>%
  mutate(rent.station = factor(rent.station),
         return.station = factor(return.station),
         # 대여일시, 반납일시에서 년, 월. 일만 추출 
         rent.ymd = lubridate::ymd(substr(.$rent.date, 1, 10)),
         return.ymd = lubridate::ymd(substr(.$return.date, 1, 10)),
         # 반납 - 대여 차이나는 일수만 계산해서 파생변수 생성 
         length.rent.return = days(return.ymd - rent.ymd)@day) %>%
  # 대여일시, 반납일시 년, 월, 일, 시, 분, 초까지 다 추출
  mutate(rent.date = lubridate::ymd_hm(rent.date),
         return.date = lubridate::ymd_hm(return.date)) %>%
  # 반납까지 하루 이상 걸리는 이상치들 모두 제거 - 3분위수가 1이기 때문 
  filter(length.rent.return < 1) %>%
  # 필요한 변수들만 선택 추출 
  select("rent.station", "rent.date",
         "return.station", "return.date")

# 2014년 상, 하반기 데이터 병합 
tashu.2014 <- rbind(tashu.14.1, tashu.14.2)

# 작업이 끝난 데이터 제거
rm(list = c("raw.tashu.14.1", "raw.tashu.14.2", "tashu.14.1", "tashu.14.2"))


전처리 후 데이터 확인

str(tashu.2014)
## 'data.frame':    1150499 obs. of  4 variables:
##  $ rent.station  : Factor w/ 148 levels "1","2","3","4",..: 105 71 73 14 14 14 14 60 60 60 ...
##  $ rent.date     : POSIXct, format: "2014-01-01 00:00:05" "2014-01-01 00:04:11" ...
##  $ return.station: Factor w/ 149 levels "1","2","3","4",..: 105 45 80 43 25 17 25 32 56 56 ...
##  $ return.date   : POSIXct, format: "2014-01-01 00:03:04" "2014-01-01 00:27:33" ...
summary(tashu.2014)
##   rent.station      rent.date                   return.station  
##  3      : 59191   Min.   :2014-01-01 00:00:05   3      : 60734  
##  56     : 30429   1st Qu.:2014-05-01 17:36:37   56     : 30411  
##  31     : 27999   Median :2014-07-04 07:53:00   31     : 28164  
##  17     : 27413   Mean   :2014-07-05 18:59:47   17     : 27369  
##  32     : 25888   3rd Qu.:2014-09-16 07:40:30   32     : 26037  
##  33     : 23787   Max.   :2014-12-31 23:49:00   33     : 23880  
##  (Other):955792                                 (Other):953904  
##   return.date                 
##  Min.   :2014-01-01 00:03:04  
##  1st Qu.:2014-05-01 18:05:30  
##  Median :2014-07-04 08:08:00  
##  Mean   :2014-07-05 19:26:39  
##  3rd Qu.:2014-09-16 07:53:00  
##  Max.   :2014-12-31 23:59:00  
## 


3.5 2015년

데이터 확인

head(raw.tashu.15.1)
##   대여스테이션       대여일시 반납스테이션       반납일시
## 1          112 20150101050320          138 20150101091830
## 2           21 20150101070401           32 20150101071518
## 3           16 20150101083429           16 20150101083501
## 4           16 20150101083528          110 20150101084858
## 5           35 20150101085910           20 20150101091033
## 6           28 20150101091433           45 20150101094240
head(raw.tashu.15.2)
##   대여스테이션       대여일시 반납스테이션       반납일시
## 1          138 20150401000043          134 20150401000931
## 2           57 20150401055342           57 20150401063411
## 3           49 20150401064745            5 20150401070209
## 4           49 20150401064845            5 20150401070223
## 5           13 20150401065225           45 20150401065931
## 6           29 20150401070245           43 20150401071450
head(raw.tashu.15.3)
##   대여스테이션       대여일시 반납스테이션       반납일시
## 1           48 20150701000117           58 20150701003054
## 2           11 20150701050158          152 20150701054347
## 3          139 20150701050158          112 20150701053602
## 4          155 20150701050209           79 20150701051546
## 5            9 20150701050250            9 20150701060120
## 6           45 20150701050343           94 20150701054331
head(raw.tashu.15.4)
##   대여스테이션       대여일시 반납스테이션       반납일시
## 1          106 20151001000013           23 20151001000623
## 2          149 20151001000030          108 20151001001232
## 3           19 20151001000036           13 20151001000909
## 4           41 20151001053203           38 20151001053822
## 5            8 20151001065004           18 20151001065858
## 6          159 20151001081946          159 20151001082000

다행히도 2015년부터는 변수명과 입력방식이 모두 동일합니다.

따라서 4분기로 나뉘어져 있는 데이터들을 모두 하나로 합쳐서 전처리를 하도록 하겠습니다.


# 분기별 데이터 4개 하나로 bind 
tashu.2015 <- rbind(raw.tashu.15.1, raw.tashu.15.2,
                    raw.tashu.15.3, raw.tashu.15.4)
str(tashu.2015)
## 'data.frame':    844282 obs. of  4 variables:
##  $ 대여스테이션: num  112 21 16 16 35 28 9 10 51 12 ...
##  $ 대여일시    : num  20150101050320 20150101070401 20150101083429 20150101083528 20150101085910 ...
##  $ 반납스테이션: num  138 32 16 110 20 45 17 1 14 16 ...
##  $ 반납일시    : num  20150101091830 20150101071518 20150101083501 20150101084858 20150101091033 ...
summary(tashu.2015)
##   대여스테이션       대여일시               반납스테이션   
##  Min.   :  1.00   Min.   :20150101050300   Min.   :  1.00  
##  1st Qu.: 19.00   1st Qu.:20150514105000   1st Qu.: 19.00  
##  Median : 48.00   Median :20150716231500   Median : 48.00  
##  Mean   : 59.44   Mean   :20150716848300   Mean   : 59.63  
##  3rd Qu.:104.00   3rd Qu.:20150922120800   3rd Qu.:105.00  
##  Max.   :200.00   Max.   :20151230235600   Max.   :200.00  
##                                            NA's   :2679    
##     반납일시             
##  Min.   :20150101071500  
##  1st Qu.:20150514113200  
##  Median :20150716235100  
##  Mean   :20150716952400  
##  3rd Qu.:20150922123000  
##  Max.   :20151231102500  
##  NA's   :1
tashu.2015 %>%
  summarize_all(funs(sum(is.na(.))/n()))
##   대여스테이션 대여일시 반납스테이션       반납일시
## 1            0        0   0.00317311 0.000001184438


전처리

# 변수명 변환 
colnames(tashu.2015) <- c("rent.station", "rent.date",
                          "return.station", "return.date")

# 결측치 제거 
tashu.2015 <- na.omit(tashu.2015)

# 변수 속성 변환, 파생변수 생성 
tashu.2015 <- tashu.2015 %>%
  mutate(rent.station = factor(rent.station),
         return.station = factor(return.station),
         # 대여일시, 반납일시 년, 월, 일, 시, 분, 초까지 다 추출
         rent.date = lubridate::ymd_hms(rent.date),
         return.date = lubridate::ymd_hms(return.date),
         # 대여일시, 반납일시에서 년, 월. 일만 추출 
         rent.ymd = lubridate::ymd(substr(.$rent.date, 1, 8)),
         return.ymd = lubridate::ymd(substr(.$return.date, 1, 8)),
         # 반납 - 대여 차이나는 일수만 계산해서 파생변수 생성 
         length.rent.return = days(return.ymd - rent.ymd)@day) %>%
  # 반납까지 하루 이상 걸리는 이상치들 모두 제거 - 3분위수가 1이기 때문 
  filter(length.rent.return < 1) %>%
  # 필요한 변수들만 선택 추출 
  select("rent.station", "rent.date",
         "return.station", "return.date")


전처리 후 데이터 확인

head(tashu.2015)
##   rent.station           rent.date return.station         return.date
## 1          112 2015-01-01 05:03:20            138 2015-01-01 09:18:30
## 2           21 2015-01-01 07:04:01             32 2015-01-01 07:15:18
## 3           16 2015-01-01 08:34:29             16 2015-01-01 08:35:01
## 4           16 2015-01-01 08:35:28            110 2015-01-01 08:48:58
## 5           35 2015-01-01 08:59:10             20 2015-01-01 09:10:33
## 6           28 2015-01-01 09:14:33             45 2015-01-01 09:42:40
str(tashu.2015)
## 'data.frame':    814461 obs. of  4 variables:
##  $ rent.station  : Factor w/ 191 levels "1","2","3","4",..: 112 21 16 16 35 28 9 10 51 12 ...
##  $ rent.date     : POSIXct, format: "2015-01-01 05:03:20" "2015-01-01 07:04:01" ...
##  $ return.station: Factor w/ 193 levels "1","2","3","4",..: 138 32 16 110 20 45 17 1 14 16 ...
##  $ return.date   : POSIXct, format: "2015-01-01 09:18:30" "2015-01-01 07:15:18" ...
summary(tashu.2015)
##   rent.station      rent.date                   return.station  
##  3      : 41656   Min.   :2015-01-01 05:03:20   3      : 52365  
##  17     : 20719   1st Qu.:2015-05-13 18:53:40   31     : 21993  
##  31     : 19827   Median :2015-07-16 22:47:44   56     : 21326  
##  56     : 19566   Mean   :2015-07-17 09:39:59   17     : 21149  
##  32     : 18131   3rd Qu.:2015-09-22 19:56:27   32     : 18895  
##  33     : 15748   Max.   :2015-12-30 23:55:11   105    : 17807  
##  (Other):678814                                 (Other):660926  
##   return.date                 
##  Min.   :2015-01-01 07:15:18  
##  1st Qu.:2015-05-13 19:26:35  
##  Median :2015-07-16 23:20:59  
##  Mean   :2015-07-17 10:09:11  
##  3rd Qu.:2015-09-22 20:28:37  
##  Max.   :2015-12-30 23:57:29  
## 
rm(list = c("raw.tashu.15.1", "raw.tashu.15.2", "raw.tashu.15.3", "raw.tashu.15.4"))


3.6 2016년

데이터 확인

tashu.2016 <- rbind(raw.tashu.16.1, raw.tashu.16.2,
                    raw.tashu.16.3, raw.tashu.16.4)

str(tashu.2016)
## 'data.frame':    715137 obs. of  4 variables:
##  $ 대여스테이션: num  46 152 133 133 39 92 92 91 90 88 ...
##  $ 대여일시    : num  20160101050015 20160101050629 20160101052416 20160101052919 20160101053244 ...
##  $ 반납스테이션: num  17 82 172 172 57 55 55 135 192 133 ...
##  $ 반납일시    : num  20160101050451 20160101053753 20160101055647 20160101055734 20160101054033 ...
summary(tashu.2016)
##   대여스테이션       대여일시               반납스테이션   
##  Min.   :  1.00   Min.   :20160101050000   Min.   :  1.00  
##  1st Qu.: 27.00   1st Qu.:20160428121800   1st Qu.: 28.00  
##  Median : 57.00   Median :20160625200600   Median : 58.00  
##  Mean   : 79.14   Mean   :20160671116300   Mean   : 79.34  
##  3rd Qu.:135.00   3rd Qu.:20160911131800   3rd Qu.:134.00  
##  Max.   :226.00   Max.   :20161231236000   Max.   :226.00  
##                                            NA's   :4398    
##     반납일시             
##  Min.   :20160101050500  
##  1st Qu.:20160428124400  
##  Median :20160625204300  
##  Mean   :20160671424300  
##  3rd Qu.:20160911134900  
##  Max.   :20170101164300  
## 
tashu.2016 %>%
  summarize_all(funs(sum(is.na(.))/n()))
##   대여스테이션 대여일시 반납스테이션 반납일시
## 1            0        0  0.006149871        0


전처리

# 변수명 변환 
colnames(tashu.2016) <- c("rent.station", "rent.date",
                          "return.station", "return.date")

# 결측치 제거 
tashu.2016 <- na.omit(tashu.2016)

# 파생변수 생성, 변수 속성 변환 등등 
tashu.2016 <- tashu.2016 %>%
  mutate(rent.station = factor(rent.station),
         return.station = factor(return.station),
         # 대여일시, 반납일시 년, 월, 일, 시, 분, 초까지 다 추출
         rent.date = lubridate::ymd_hms(rent.date),
         return.date = lubridate::ymd_hms(return.date),
         # 대여일시, 반납일시에서 년, 월. 일만 추출 
         rent.ymd = lubridate::ymd(substr(.$rent.date, 1, 8)),
         return.ymd = lubridate::ymd(substr(.$return.date, 1, 8)),
         # 반납 - 대여 차이나는 일수만 계산해서 파생변수 생성 
         length.rent.return = days(return.ymd - rent.ymd)@day) %>%
  # 반납까지 하루 이상 걸리는 이상치들 모두 제거 - 3분위수가 1이기 때문 
  filter(length.rent.return < 1) %>%
  # 필요한 변수들만 선택 추출 
  select("rent.station", "rent.date",
         "return.station", "return.date")


전처리 후 데이터 확인

head(tashu.2016)
##   rent.station           rent.date return.station         return.date
## 1           46 2016-01-01 05:00:15             17 2016-01-01 05:04:51
## 2          152 2016-01-01 05:06:29             82 2016-01-01 05:37:53
## 3          133 2016-01-01 05:24:16            172 2016-01-01 05:56:47
## 4          133 2016-01-01 05:29:19            172 2016-01-01 05:57:34
## 5           39 2016-01-01 05:32:44             57 2016-01-01 05:40:33
## 6           92 2016-01-01 05:36:21             55 2016-01-01 08:21:42
str(tashu.2016)
## 'data.frame':    686723 obs. of  4 variables:
##  $ rent.station  : Factor w/ 226 levels "1","2","3","4",..: 46 152 133 133 39 92 92 91 90 88 ...
##  $ rent.date     : POSIXct, format: "2016-01-01 05:00:15" "2016-01-01 05:06:29" ...
##  $ return.station: Factor w/ 226 levels "1","2","3","4",..: 17 82 172 172 57 55 55 135 192 133 ...
##  $ return.date   : POSIXct, format: "2016-01-01 05:04:51" "2016-01-01 05:37:53" ...
summary(tashu.2016)
##   rent.station      rent.date                   return.station  
##  3      : 24247   Min.   :2016-01-01 05:00:15   3      : 28050  
##  186    : 20722   1st Qu.:2016-04-26 18:49:38   186    : 23254  
##  56     : 15047   Median :2016-06-25 15:16:26   56     : 15825  
##  17     : 14377   Mean   :2016-07-03 02:24:02   31     : 15334  
##  31     : 13010   3rd Qu.:2016-09-11 17:17:22   17     : 15097  
##  33     : 12074   Max.   :2016-12-31 23:54:15   33     : 13843  
##  (Other):587246                                 (Other):575320  
##   return.date                 
##  Min.   :2016-01-01 05:04:51  
##  1st Qu.:2016-04-26 19:19:30  
##  Median :2016-06-25 15:45:06  
##  Mean   :2016-07-03 02:52:18  
##  3rd Qu.:2016-09-11 17:54:56  
##  Max.   :2016-12-31 23:59:49  
## 
rm(list = c("raw.tashu.16.1", "raw.tashu.16.2", "raw.tashu.16.3", "raw.tashu.16.4"))


3.7 2017년

데이터 확인

# 바인딩 
tashu.2017 <- rbind(raw.tashu.17.1, raw.tashu.17.2,
                    raw.tashu.17.3, raw.tashu.17.4)

str(tashu.2017)
## 'data.frame':    668736 obs. of  4 variables:
##  $ 대여스테이션: num  55 113 55 46 17 117 168 117 40 127 ...
##  $ 대여일시    : num  20170101000025 20170101050130 20170101050132 20170101050236 20170101050308 ...
##  $ 반납스테이션: num  55 87 59 52 17 208 174 208 46 125 ...
##  $ 반납일시    : num  20170101002927 20170101051018 20170101050859 20170101050647 20170101051906 ...
summary(tashu.2017)
##   대여스테이션       대여일시               반납스테이션  
##  Min.   :  1.00   Min.   :20170101000000   Min.   :  1.0  
##  1st Qu.: 30.00   1st Qu.:20170416203900   1st Qu.: 30.0  
##  Median : 61.00   Median :20170519232200   Median : 61.0  
##  Mean   : 87.83   Mean   :20170572972500   Mean   : 87.5  
##  3rd Qu.:151.00   3rd Qu.:20170619092500   3rd Qu.:150.0  
##  Max.   :249.00   Max.   :20171231235900   Max.   :249.0  
##                                            NA's   :2906   
##     반납일시             
##  Min.   :20170101002900  
##  1st Qu.:20170416212000  
##  Median :20170519235100  
##  Mean   :20170573171600  
##  3rd Qu.:20170619094900  
##  Max.   :20180101002200  
## 
tashu.2017 %>%
  summarize_all(funs(sum(is.na(.))/n()))
##   대여스테이션 대여일시 반납스테이션 반납일시
## 1            0        0  0.004345512        0


전처리

# 변수명 변환 
colnames(tashu.2017) <- c("rent.station", "rent.date", 
                          "return.station", "return.date")

# 결측치 제거 
tashu.2017 <- na.omit(tashu.2017)

# 그 외 작업 
tashu.2017 <- tashu.2017 %>%
  mutate(rent.station = factor(rent.station),
         return.station = factor(return.station),
         # 대여일시, 반납일시 년, 월, 일, 시, 분, 초까지 다 추출
         rent.date = lubridate::ymd_hms(rent.date),
         return.date = lubridate::ymd_hms(return.date),
         # 대여일시, 반납일시에서 년, 월. 일만 추출 
         rent.ymd = lubridate::ymd(substr(.$rent.date, 1, 8)),
         return.ymd = lubridate::ymd(substr(.$return.date, 1, 8)),
         # 반납 - 대여 차이나는 일수만 계산해서 파생변수 생성 
         length.rent.return = days(return.ymd - rent.ymd)@day) %>%
  # 반납까지 하루 이상 걸리는 이상치들 모두 제거 - 3분위수가 1이기 때문 
  filter(length.rent.return < 1) %>%
  # 필요한 변수들만 선택해서 Data frame 으로 저장 
  select("rent.station", "rent.date",
         "return.station", "return.date")


전처리 후 데이터 확인

summary(tashu.2017)
##   rent.station      rent.date                   return.station  
##  3      : 17574   Min.   :2017-01-01 00:00:25   3      : 20409  
##  186    : 16138   1st Qu.:2017-04-16 18:31:12   186    : 18406  
##  56     : 13586   Median :2017-05-19 20:45:15   31     : 14710  
##  31     : 12810   Mean   :2017-06-03 03:41:20   56     : 14534  
##  17     : 12706   3rd Qu.:2017-06-19 08:34:30   17     : 13696  
##  33     : 12149   Max.   :2017-12-31 23:55:01   33     : 12766  
##  (Other):560246                                 (Other):550688  
##   return.date                 
##  Min.   :2017-01-01 00:29:27  
##  1st Qu.:2017-04-16 19:11:06  
##  Median :2017-05-19 21:20:19  
##  Mean   :2017-06-03 04:10:31  
##  3rd Qu.:2017-06-19 08:52:03  
##  Max.   :2017-12-31 23:58:44  
## 
str(tashu.2017)
## 'data.frame':    645209 obs. of  4 variables:
##  $ rent.station  : Factor w/ 249 levels "1","2","3","4",..: 55 113 55 46 17 117 168 117 40 127 ...
##  $ rent.date     : POSIXct, format: "2017-01-01 00:00:25" "2017-01-01 05:01:30" ...
##  $ return.station: Factor w/ 249 levels "1","2","3","4",..: 55 87 59 52 17 208 174 208 46 125 ...
##  $ return.date   : POSIXct, format: "2017-01-01 00:29:27" "2017-01-01 05:10:18" ...
head(tashu.2017)
##   rent.station           rent.date return.station         return.date
## 1           55 2017-01-01 00:00:25             55 2017-01-01 00:29:27
## 2          113 2017-01-01 05:01:30             87 2017-01-01 05:10:18
## 3           55 2017-01-01 05:01:32             59 2017-01-01 05:08:59
## 4           46 2017-01-01 05:02:36             52 2017-01-01 05:06:47
## 5           17 2017-01-01 05:03:08             17 2017-01-01 05:19:06
## 6          117 2017-01-01 05:03:20            208 2017-01-01 05:38:37
rm(list = c("raw.tashu.17.1", "raw.tashu.17.2", "raw.tashu.17.3", "raw.tashu.17.4"))


3.8 2018년 1분기

데이터 확인

head(raw.tashu.18)
##   대여스테이션       대여일시 반납스테이션       반납일시
## 1           19 20180101051852           19 20180101052703
## 2           46 20180101052620          157 20180101060719
## 3          100 20180101054039          103 20180101054458
## 4          157 20180101054641          152 20180101060333
## 5           41 20180101054909          187 20180101060201
## 6          212 20180101055710          212 20180101060231


전처리

tashu.18 <- na.omit(raw.tashu.18)

colnames(tashu.18) <- c("rent.station", "rent.date", "return.station", "return.date")

tashu.18 <- tashu.18 %>%
  mutate(rent.station = factor(rent.station),
         return.station = factor(return.station),
         # 대여일시, 반납일시 년, 월, 일, 시, 분, 초까지 다 추출
         rent.date = lubridate::ymd_hms(rent.date),
         return.date = lubridate::ymd_hms(return.date),
         # 대여일시, 반납일시에서 년, 월. 일만 추출 
         rent.ymd = lubridate::ymd(substr(.$rent.date, 1, 8)),
         return.ymd = lubridate::ymd(substr(.$return.date, 1, 8)),
         # 반납 - 대여 차이나는 일수만 계산해서 파생변수 생성 
         length.rent.return = days(return.ymd - rent.ymd)@day) %>%
  # 반납까지 하루 이상 걸리는 이상치들 모두 제거 - 3분위수가 1이기 때문 
  filter(length.rent.return < 1) %>%
  # 필요한 변수들만 선택해서 Data frame 으로 저장 
  select("rent.station", "rent.date",
         "return.station", "return.date")


전처리 후 데이터 확인

summary(tashu.18)
##   rent.station     rent.date                   return.station 
##  3      : 2279   Min.   :2018-01-01 05:18:52   3      : 2610  
##  186    : 1972   1st Qu.:2018-02-14 23:32:36   186    : 2261  
##  31     : 1119   Median :2018-03-08 18:49:05   31     : 1270  
##  56     : 1098   Mean   :2018-03-01 20:52:28   33     : 1204  
##  33     : 1093   3rd Qu.:2018-03-23 13:11:30   56     : 1195  
##  19     : 1061   Max.   :2018-03-31 23:53:00   32     : 1100  
##  (Other):55281                                 (Other):54263  
##   return.date                 
##  Min.   :2018-01-01 05:27:03  
##  1st Qu.:2018-02-14 23:47:46  
##  Median :2018-03-08 19:18:24  
##  Mean   :2018-03-01 21:21:48  
##  3rd Qu.:2018-03-23 13:45:55  
##  Max.   :2018-03-31 23:59:06  
## 
head(tashu.18)
##   rent.station           rent.date return.station         return.date
## 1           19 2018-01-01 05:18:52             19 2018-01-01 05:27:03
## 2           46 2018-01-01 05:26:20            157 2018-01-01 06:07:19
## 3          100 2018-01-01 05:40:39            103 2018-01-01 05:44:58
## 4          157 2018-01-01 05:46:41            152 2018-01-01 06:03:33
## 5           41 2018-01-01 05:49:09            187 2018-01-01 06:02:01
## 6          212 2018-01-01 05:57:10            212 2018-01-01 06:02:31
# 데이터 삭제 
rm("raw.tashu.18")


3.9 Data binding ~ save

2013년부터 2018년까지 연별 타슈 이용데이터 전처리가 끝났습니다.

이제는 6개 데이터를 모두 하나로 합친 후 추가로 파생변수를 만들고 구조를 확인한 후 .RData로 저장하겠습니다.

Binding

tashu.2013.2018 <- rbind(tashu.2013, tashu.2014, tashu.2015,
                         tashu.2016, tashu.2017, tashu.18)


Pre-processing : 추가 전처리(년, 월, 일, 계절 파생변수 생성)

tashu <- tashu.2013.2018 %>%
  # 파생변수 생성 : 위에서부터 순서대로 년, 월, 일, 계절
  mutate(year   = factor(lubridate::year(tashu.2013.2018$rent.date), ordered = T),
         month  = lubridate::month(tashu.2013.2018$rent.date),
         day    = lubridate::wday(tashu.2013.2018$rent.date),
         season = case_when(month %in% c(3:5) ~ "Spring",
                            month %in% c(6:8) ~ "Summer",
                            month %in% c(9:11) ~ "Fall",
                            month %in% c(1, 2, 12) ~ "Winter"),
         season = factor(season,
                         levels = c("Spring", "Summer", "Fall", "Winter"),
                         ordered = T))

tashu$month <- lubridate::month(tashu$month, label = T)

tashu$day <- lubridate::wday(tashu$rent.date, label = T)


Save

save(tashu, file = "./tashu.RData")




5. Data assesment after that Pre-processing : 전처리 후 데이터 확인

5.2 str()

str(tashu)
## 'data.frame':    4252430 obs. of  8 variables:
##  $ rent.station  : Factor w/ 250 levels "1","2","3","4",..: 43 2 106 4 21 90 13 1 1 1 ...
##  $ rent.date     : POSIXct, format: "2013-01-01 05:56:03" "2013-01-01 06:04:06" ...
##  $ return.station: Factor w/ 250 levels "1","2","3","4",..: 34 10 105 4 105 91 30 1 2 2 ...
##  $ return.date   : POSIXct, format: "2013-01-01 06:02:17" "2013-01-01 06:18:59" ...
##  $ year          : Ord.factor w/ 6 levels "2013"<"2014"<..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ month         : Ord.factor w/ 12 levels "1"<"2"<"3"<"4"<..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ day           : Ord.factor w/ 7 levels "일"<"월"<"화"<..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ season        : Ord.factor w/ 4 levels "Spring"<"Summer"<..: 4 4 4 4 4 4 4 4 4 4 ...


5.3 summary()

summary(tashu)
##   rent.station       rent.date                   return.station   
##  3      : 192194   Min.   :2013-01-01 05:56:03   3      : 212606  
##  56     : 108674   1st Qu.:2014-04-01 17:44:40   56     : 112190  
##  17     : 101649   Median :2015-03-25 08:17:40   31     : 106418  
##  31     :  99618   Mean   :2015-04-29 03:15:39   17     : 103912  
##  32     :  89332   3rd Qu.:2016-06-20 21:20:16   32     :  92475  
##  33     :  86860   Max.   :2018-03-31 23:53:00   33     :  91516  
##  (Other):3574103                                 (Other):3533313  
##   return.date                    year             month        
##  Min.   :2013-01-01 06:02:17   2013: 891635   5      : 622649  
##  1st Qu.:2014-04-01 18:16:10   2014:1150499   6      : 557445  
##  Median :2015-03-25 08:33:58   2015: 814461   4      : 482542  
##  Mean   :2015-04-29 03:43:40   2016: 686723   10     : 446864  
##  3rd Qu.:2016-06-20 21:48:06   2017: 645209   9      : 390416  
##  Max.   :2018-03-31 23:59:06   2018:  63903   7      : 378502  
##                                               (Other):1374012  
##  day            season       
##  일:638850   Spring:1448774  
##  월:575717   Summer:1272001  
##  화:564104   Fall  :1092284  
##  수:580677   Winter: 439371  
##  목:584276                   
##  금:633407                   
##  토:675399