This document was written in Korean.
본내용은 R 패키지 내용 중 dplyr에 대한 내용이며, 사용방법에 대한 간략한 예제와 활용방향에 대해서 서술하겠습니다.
저는 아래 내용을 여러 참조 사이트들로 부터 코딩 및 확인을 통해 RPubs.com에 업로드하여 두었습니다.
먼저 dplyr은 데이터 가공 Packages로 C++로 작성되어 불필요한 함수를 불러오지 않아 매우.. 매우 빠른 처리 성능을 자랑합니다.
무엇보다 “tbl %>%” 형태의 chaining이 dplyr의 꽃으로 아래 예제를 통해서 차근히 보겠습니다.
간략히 예제로는 hflights 를 사용하겠습니다.
This dataset contains all flights departing from Houston airports IAH (George Bush Intercontinental) and HOU (Houston Hobby).
The data comes from the Research and Innovation Technology Administration at the Bureau of Transporation statistics: http://www.transtats.bts.gov/DatabaseInfo.asp?DB_ID=120&Link=0
위 내용을 토대로 데이터 가공에 대해서 설명을 진행할것이며, SQL 사용자에게 보다 친숙한 이해를 돕기 위해서
모든 내용은 SQL 정의를 어떻게 구현하지에 대해서 설명을하겠습니다.
Rmarkdown을 사용해서 처음으로 쓰는 문서라 어휘나 문맥이 부드럽진 않습니다. 이점 양해 부탁 드립니다.
혹시 추가로 궁금하신점을 아래 Comment를 사용 부탁 드립니다.
아래 문서가 몇일만에 만들어 질진 모르지만 열심히 업데이트해 나가겠습니다.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(hflights)
dim(hflights)
## [1] 227496 21
head(hflights)
## Year Month DayofMonth DayOfWeek DepTime ArrTime UniqueCarrier
## 5424 2011 1 1 6 1400 1500 AA
## 5425 2011 1 2 7 1401 1501 AA
## 5426 2011 1 3 1 1352 1502 AA
## 5427 2011 1 4 2 1403 1513 AA
## 5428 2011 1 5 3 1405 1507 AA
## 5429 2011 1 6 4 1359 1503 AA
## FlightNum TailNum ActualElapsedTime AirTime ArrDelay DepDelay Origin
## 5424 428 N576AA 60 40 -10 0 IAH
## 5425 428 N557AA 60 45 -9 1 IAH
## 5426 428 N541AA 70 48 -8 -8 IAH
## 5427 428 N403AA 70 39 3 3 IAH
## 5428 428 N492AA 62 44 -3 5 IAH
## 5429 428 N262AA 64 45 -7 -1 IAH
## Dest Distance TaxiIn TaxiOut Cancelled CancellationCode Diverted
## 5424 DFW 224 7 13 0 0
## 5425 DFW 224 6 9 0 0
## 5426 DFW 224 5 17 0 0
## 5427 DFW 224 9 22 0 0
## 5428 DFW 224 9 9 0 0
## 5429 DFW 224 6 13 0 0
The dplyr package contains five key data manipulation functions, also called verbs:
select(), which returns a subset of the columns,
filter(), that is able to return a subset of the rows,
arrange(), that reorders the rows according to single or multiple variables,
mutate(), used to add columns from existing data,
summarise(), which reduces each group to a single row by calculating aggregate measures.
Below we explore each one in details.
보통 SQL은아래 Syntax를 기본 구조로 합니다. 이를 어떻게 dplyr로 구현 되는지 알아 보겠습니다.
SELECT *
FROM Tables
WHERE ...
Group by ...
Having
가장 먼저 테이블 선택 입니다. 보통 R에서는 Data 사용이 attach 를 하지 않는 이상은
함수호출시에 data=“tables” 형태로 입력을 받습니다.
두가지 호출 방법
dplyr에는 빠른 속도를 위해 dplyr용 data frame이 존재합니다. 이 syntax는 tbl_df(data)
#Data Frame 생성 방법
hflights <- tbl_df(hflights)
#요약정보 보기
glimpse(hflights)
## Observations: 227,496
## Variables: 21
## $ Year (int) 2011, 2011, 2011, 2011, 2011, 2011, 2011, 20...
## $ Month (int) 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,...
## $ DayofMonth (int) 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1...
## $ DayOfWeek (int) 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6,...
## $ DepTime (int) 1400, 1401, 1352, 1403, 1405, 1359, 1359, 13...
## $ ArrTime (int) 1500, 1501, 1502, 1513, 1507, 1503, 1509, 14...
## $ UniqueCarrier (chr) "AA", "AA", "AA", "AA", "AA", "AA", "AA", "A...
## $ FlightNum (int) 428, 428, 428, 428, 428, 428, 428, 428, 428,...
## $ TailNum (chr) "N576AA", "N557AA", "N541AA", "N403AA", "N49...
## $ ActualElapsedTime (int) 60, 60, 70, 70, 62, 64, 70, 59, 71, 70, 70, ...
## $ AirTime (int) 40, 45, 48, 39, 44, 45, 43, 40, 41, 45, 42, ...
## $ ArrDelay (int) -10, -9, -8, 3, -3, -7, -1, -16, 44, 43, 29,...
## $ DepDelay (int) 0, 1, -8, 3, 5, -1, -1, -5, 43, 43, 29, 19, ...
## $ Origin (chr) "IAH", "IAH", "IAH", "IAH", "IAH", "IAH", "I...
## $ Dest (chr) "DFW", "DFW", "DFW", "DFW", "DFW", "DFW", "D...
## $ Distance (int) 224, 224, 224, 224, 224, 224, 224, 224, 224,...
## $ TaxiIn (int) 7, 6, 5, 9, 9, 6, 12, 7, 8, 6, 8, 4, 6, 5, 6...
## $ TaxiOut (int) 13, 9, 17, 22, 9, 13, 15, 12, 22, 19, 20, 11...
## $ Cancelled (int) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ CancellationCode (chr) "", "", "", "", "", "", "", "", "", "", "", ...
## $ Diverted (int) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
tbl_df로 생성된 data frame을 호출시에 기본 10건만 호출이 됩니다. 이를 변경하고자 하면 아래와 같이 변경이 가능합니다.
options(dplyr.print_max = Inf)
options(dplyr.print_max = 10)