1. filter()
- filter()를 이용하면 값을 기준으로 데이터를 서브셋가능
- 첫 번째 인수 : 데이터프레임 이름
- 두 번째 이후의 인수 : 데이터프레임을 필터링하는 표현식
ex) 1월 1일 항공편 모두선택하는 방식
filter(flights, month==1,day ==1)
# A tibble: 842 × 19
year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier
<int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> <chr>
1 2013 1 1 517 515 2 830 819 11 UA
2 2013 1 1 533 529 4 850 830 20 UA
3 2013 1 1 542 540 2 923 850 33 AA
4 2013 1 1 544 545 -1 1004 1022 -18 B6
5 2013 1 1 554 600 -6 812 837 -25 DL
6 2013 1 1 554 558 -4 740 728 12 UA
7 2013 1 1 555 600 -5 913 854 19 B6
8 2013 1 1 557 600 -3 709 723 -14 EV
9 2013 1 1 557 600 -3 838 846 -8 B6
10 2013 1 1 558 600 -2 753 745 8 AA
# ℹ 832 more rows
# ℹ 9 more variables: flight <int>, tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>,
# distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
# ℹ Use `print(n = ...)` to see more rows
-dplyr : 필터링 연산을 실행하고 새로운 데이터프레임을 반환
-dplyr 함수들은 입력을 절대 수정하지 않기 때문에 결과를 저장하려면 할당 연산자 <- 를 사용해야 함
c <- filter(flights, month==1,day ==1)
2.비교 연산자
비교연산자 : >,>=,<,<=,!=,==
cf) 부동소수점 숫자
sqrt(2) ^2 ==2
[1] FALSE
- 의문이 있을거다 왜 해당식은 FALSE인가?
- 컴퓨터는 유한 정밀도 산술을 사용하므로 눈 앞에 보이는 숫자는 근사값에 불가하다.
3. 논리 연산자
- & : 'and'
- | : 'or'
- ! : 'not'
ex) 11월이나 12월에 출바한 항공편 모두 찾기
filter(flights, month==11 |month ==12)
# A tibble: 55,403 × 19
year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier
<int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> <chr>
1 2013 11 1 5 2359 6 352 345 7 B6
2 2013 11 1 35 2250 105 123 2356 87 B6
3 2013 11 1 455 500 -5 641 651 -10 US
4 2013 11 1 539 545 -6 856 827 29 UA
5 2013 11 1 542 545 -3 831 855 -24 AA
6 2013 11 1 549 600 -11 912 923 -11 UA
7 2013 11 1 550 600 -10 705 659 6 US
8 2013 11 1 554 600 -6 659 701 -2 US
9 2013 11 1 554 600 -6 826 827 -1 DL
10 2013 11 1 554 600 -6 749 751 -2 DL
# ℹ 55,393 more rows
# ℹ 9 more variables: flight <int>, tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>,
# distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
# ℹ Use `print(n = ...)` to see more rows
- 하지만 filter(filghts, month ==(11 | 12))로 사용하면 틀린다.
- 11|12 --> 1로 return되기 때문에 1월인 데이터만 도출한다.
-이 땐 %in% 로 해결할수 있다.
filter(flights, month %in% c(11,12))
A tibble: 55,403 × 19
year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier
<int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> <chr>
1 2013 11 1 5 2359 6 352 345 7 B6
2 2013 11 1 35 2250 105 123 2356 87 B6
3 2013 11 1 455 500 -5 641 651 -10 US
4 2013 11 1 539 545 -6 856 827 29 UA
5 2013 11 1 542 545 -3 831 855 -24 AA
6 2013 11 1 549 600 -11 912 923 -11 UA
7 2013 11 1 550 600 -10 705 659 6 US
8 2013 11 1 554 600 -6 659 701 -2 US
9 2013 11 1 554 600 -6 826 827 -1 DL
10 2013 11 1 554 600 -6 749 751 -2 DL
# ℹ 55,393 more rows
# ℹ 9 more variables: flight <int>, tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>,
# distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
4.NA
- NA : not available
- NA는 모르는 값을 나타내므로 결측값은 대부분 NA이다.
-값이 결측인지 확인하고 싶으면 is.na()를 사용하면 된다.
5. 연습 문제
Q1) 다음 조건을 만족하는 항공편을 모두 찾아라.
a) 2시간 이상 도착 지연
filter(flights,dep_delay>=2)
# A tibble: 120,382 × 19
year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier
<int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> <chr>
1 2013 1 1 517 515 2 830 819 11 UA
2 2013 1 1 533 529 4 850 830 20 UA
3 2013 1 1 542 540 2 923 850 33 AA
4 2013 1 1 608 600 8 807 735 32 MQ
5 2013 1 1 611 600 11 945 931 14 UA
6 2013 1 1 613 610 3 925 921 4 B6
7 2013 1 1 623 610 13 920 915 5 AA
8 2013 1 1 632 608 24 740 728 12 EV
9 2013 1 1 644 636 8 931 940 -9 UA
10 2013 1 1 702 700 2 1058 1014 44 B6
# ℹ 120,372 more rows
# ℹ 9 more variables: flight <int>, tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>,
# distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
# ℹ Use `print(n = ...)` to see more rows
b) 휴스턴(IAH or HOU)으로 운항
filter(flights,dest == "IAH" | dest == "HOU")
# A tibble: 9,313 × 19
year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier flight tailnum origin dest air_time distance hour
<int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> <chr> <int> <chr> <chr> <chr> <dbl> <dbl> <dbl>
1 2013 1 1 517 515 2 830 819 11 UA 1545 N14228 EWR IAH 227 1400 5
2 2013 1 1 533 529 4 850 830 20 UA 1714 N24211 LGA IAH 227 1416 5
3 2013 1 1 623 627 -4 933 932 1 UA 496 N459UA LGA IAH 229 1416 6
4 2013 1 1 728 732 -4 1041 1038 3 UA 473 N488UA LGA IAH 238 1416 7
5 2013 1 1 739 739 0 1104 1038 26 UA 1479 N37408 EWR IAH 249 1400 7
6 2013 1 1 908 908 0 1228 1219 9 UA 1220 N12216 EWR IAH 233 1400 9
7 2013 1 1 1028 1026 2 1350 1339 11 UA 1004 N76508 LGA IAH 237 1416 10
8 2013 1 1 1044 1045 -1 1352 1351 1 UA 455 N667UA EWR IAH 229 1400 10
9 2013 1 1 1114 900 134 1447 1222 145 UA 1086 N76502 LGA IAH 248 1416 9
10 2013 1 1 1205 1200 5 1503 1505 -2 UA 1461 N39418 EWR IAH 221 1400 12
# ℹ 9,303 more rows
# ℹ 2 more variables: minute <dbl>, time_hour <dttm>
# ℹ Use `print(n = ...)` to see more rows
c) 유나이티드항공(United), 아메리칸항공(American),델타항공(Delta)이 운항
filter(flights,carrier %in% c('AA','UA','DL'))
# A tibble: 139,504 × 19
year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier flight tailnum origin dest air_time distance hour
<int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> <chr> <int> <chr> <chr> <chr> <dbl> <dbl> <dbl>
1 2013 1 1 517 515 2 830 819 11 UA 1545 N14228 EWR IAH 227 1400 5
2 2013 1 1 533 529 4 850 830 20 UA 1714 N24211 LGA IAH 227 1416 5
3 2013 1 1 542 540 2 923 850 33 AA 1141 N619AA JFK MIA 160 1089 5
4 2013 1 1 554 600 -6 812 837 -25 DL 461 N668DN LGA ATL 116 762 6
5 2013 1 1 554 558 -4 740 728 12 UA 1696 N39463 EWR ORD 150 719 5
6 2013 1 1 558 600 -2 753 745 8 AA 301 N3ALAA LGA ORD 138 733 6
7 2013 1 1 558 600 -2 924 917 7 UA 194 N29129 JFK LAX 345 2475 6
8 2013 1 1 558 600 -2 923 937 -14 UA 1124 N53441 EWR SFO 361 2565 6
9 2013 1 1 559 600 -1 941 910 31 AA 707 N3DUAA LGA DFW 257 1389 6
10 2013 1 1 559 600 -1 854 902 -8 UA 1187 N76515 EWR LAS 337 2227 6
# ℹ 139,494 more rows
# ℹ 2 more variables: minute <dbl>, time_hour <dttm>
# ℹ Use `print(n = ...)` to see more rows
d) 여름(7,8,9)월에 출발
filter(flights,month %in% c(7,8,9))
# A tibble: 86,326 × 19
year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier flight tailnum origin dest air_time distance hour
<int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> <chr> <int> <chr> <chr> <chr> <dbl> <dbl> <dbl>
1 2013 7 1 1 2029 212 236 2359 157 B6 915 N653JB JFK SFO 315 2586 20
2 2013 7 1 2 2359 3 344 344 0 B6 1503 N805JB JFK SJU 200 1598 23
3 2013 7 1 29 2245 104 151 1 110 B6 234 N348JB JFK BTV 66 266 22
4 2013 7 1 43 2130 193 322 14 188 B6 1371 N794JB LGA FLL 143 1076 21
5 2013 7 1 44 2150 174 300 100 120 AA 185 N324AA JFK LAX 297 2475 21
6 2013 7 1 46 2051 235 304 2358 186 B6 165 N640JB JFK PDX 304 2454 20
7 2013 7 1 48 2001 287 308 2305 243 VX 415 N627VA JFK LAX 298 2475 20
8 2013 7 1 58 2155 183 335 43 172 B6 425 N535JB JFK TPA 140 1005 21
9 2013 7 1 100 2146 194 327 30 177 B6 1183 N531JB JFK MCO 126 944 21
10 2013 7 1 100 2245 135 337 135 122 B6 623 N663JB JFK LAX 304 2475 22
# ℹ 86,316 more rows
# ℹ 2 more variables: minute <dbl>, time_hour <dttm>
# ℹ Use `print(n = ...)` to see more rows
e) 2시간 이상 지연 도착헀지만, 지연 출발하지는 않음
filter(flights,arr_delay>=20 & dep_delay<=0)
# A tibble: 8,141 × 19
year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier flight tailnum origin dest air_time distance hour
<int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> <chr> <int> <chr> <chr> <chr> <dbl> <dbl> <dbl>
1 2013 1 1 559 600 -1 941 910 31 AA 707 N3DUAA LGA DFW 257 1389 6
2 2013 1 1 624 630 -6 909 840 29 EV 4626 N11107 EWR MSP 190 1008 6
3 2013 1 1 628 630 -2 1016 947 29 UA 1665 N33289 EWR LAX 366 2454 6
4 2013 1 1 635 635 0 1028 940 48 AA 711 N3GKAA LGA DFW 248 1389 6
5 2013 1 1 656 705 -9 1007 940 27 MQ 4534 N722MQ LGA XNA 233 1147 7
6 2013 1 1 724 730 -6 1111 1040 31 AA 715 N541AA LGA DFW 254 1389 7
7 2013 1 1 739 739 0 1104 1038 26 UA 1479 N37408 EWR IAH 249 1400 7
8 2013 1 1 754 755 -1 1103 1030 33 WN 733 N789SW LGA DEN 279 1620 7
9 2013 1 1 812 814 -2 1040 1017 23 EV 4537 N17108 EWR MEM 180 946 8
10 2013 1 1 833 835 -2 1134 1102 32 F9 835 N203FR LGA DEN 257 1620 8
# ℹ 8,131 more rows
# ℹ 2 more variables: minute <dbl>, time_hour <dttm>
# ℹ Use `print(n = ...)` to see more rows
Q2) 다른 유용한 dplyt 필터링 도우미로 bewteen()이 있다. 어떤 일을 하는가?
- 주어진 범위 내에 있는 값을 필터링 하는 데 사용된다.
between(x, left, right, ...)
ex)
filter(flights,between(month, 7,9))
# A tibble: 86,326 × 19
year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier flight tailnum origin dest air_time distance hour
<int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> <chr> <int> <chr> <chr> <chr> <dbl> <dbl> <dbl>
1 2013 7 1 1 2029 212 236 2359 157 B6 915 N653JB JFK SFO 315 2586 20
2 2013 7 1 2 2359 3 344 344 0 B6 1503 N805JB JFK SJU 200 1598 23
3 2013 7 1 29 2245 104 151 1 110 B6 234 N348JB JFK BTV 66 266 22
4 2013 7 1 43 2130 193 322 14 188 B6 1371 N794JB LGA FLL 143 1076 21
5 2013 7 1 44 2150 174 300 100 120 AA 185 N324AA JFK LAX 297 2475 21
6 2013 7 1 46 2051 235 304 2358 186 B6 165 N640JB JFK PDX 304 2454 20
7 2013 7 1 48 2001 287 308 2305 243 VX 415 N627VA JFK LAX 298 2475 20
8 2013 7 1 58 2155 183 335 43 172 B6 425 N535JB JFK TPA 140 1005 21
9 2013 7 1 100 2146 194 327 30 177 B6 1183 N531JB JFK MCO 126 944 21
10 2013 7 1 100 2245 135 337 135 122 B6 623 N663JB JFK LAX 304 2475 22
# ℹ 86,316 more rows
# ℹ 2 more variables: minute <dbl>, time_hour <dttm>
# ℹ Use `print(n = ...)` to see more rows
'DS Study > R4DS(R언어)' 카테고리의 다른 글
[R4DS] [2-4] select() (0) | 2024.03.31 |
---|---|
[R4DS] [2-3] arrange() (0) | 2024.03.31 |
[R4DS] [2-1] 데이터 변형 (nycflights13, tidyverse) (0) | 2024.03.30 |
[R4DS] [1-8] 그래프 레이어 문법 (0) | 2024.03.30 |
[R4DS] [1-7] 좌표계 (0) | 2024.03.30 |