1.select()
- 변수가 수백,수천 개인 데이터셋을 자주 만나게 될 것이다.
- 실제로 관심있는 변수들로 좁혀 신속하게 zoom in해준다.
ex) 이름으로 열 선택
select(flights,year,month,day)
# A tibble: 336,776 × 3
year month day
<int> <int> <int>
1 2013 1 1
2 2013 1 1
3 2013 1 1
4 2013 1 1
5 2013 1 1
6 2013 1 1
7 2013 1 1
8 2013 1 1
9 2013 1 1
10 2013 1 1
# ℹ 336,766 more rows
# ℹ Use `print(n = ...)` to see more rows
ex) year과 day사이의 (경계포함) 열 모두 선택
select(flights,year:day)
# A tibble: 336,776 × 3
year month day
<int> <int> <int>
1 2013 1 1
2 2013 1 1
3 2013 1 1
4 2013 1 1
5 2013 1 1
6 2013 1 1
7 2013 1 1
8 2013 1 1
9 2013 1 1
10 2013 1 1
# ℹ 336,766 more rows
# ℹ Use `print(n = ...)` to see more rows
ex) year~day까지의 열들을 모두 제외한 열 모두 선택
select(flights,-(year:day))
# A tibble: 336,776 × 16
dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier flight tailnum origin dest air_time distance hour minute
<int> <int> <dbl> <int> <int> <dbl> <chr> <int> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 517 515 2 830 819 11 UA 1545 N14228 EWR IAH 227 1400 5 15
2 533 529 4 850 830 20 UA 1714 N24211 LGA IAH 227 1416 5 29
3 542 540 2 923 850 33 AA 1141 N619AA JFK MIA 160 1089 5 40
4 544 545 -1 1004 1022 -18 B6 725 N804JB JFK BQN 183 1576 5 45
5 554 600 -6 812 837 -25 DL 461 N668DN LGA ATL 116 762 6 0
6 554 558 -4 740 728 12 UA 1696 N39463 EWR ORD 150 719 5 58
7 555 600 -5 913 854 19 B6 507 N516JB EWR FLL 158 1065 6 0
8 557 600 -3 709 723 -14 EV 5708 N829AS LGA IAD 53 229 6 0
9 557 600 -3 838 846 -8 B6 79 N593JB JFK MCO 140 944 6 0
10 558 600 -2 753 745 8 AA 301 N3ALAA LGA ORD 138 733 6 0
# ℹ 336,766 more rows
# ℹ 1 more variable: time_hour <dttm>
# ℹ Use `print(n = ...)` to see more rows
2. rename()
- 변수명을 변경하고 싶을때 사용 가능
-ex)
r <- select(flights,year:day)
rename(r,년도 = year)
# A tibble: 336,776 × 3
년도 month day
<int> <int> <int>
1 2013 1 1
2 2013 1 1
3 2013 1 1
4 2013 1 1
5 2013 1 1
6 2013 1 1
7 2013 1 1
8 2013 1 1
9 2013 1 1
10 2013 1 1
# ℹ 336,766 more rows
# ℹ Use `print(n = ...)` to see more rows
3. everthing()
-몇 개의 변수를 데이터프레임의 시작 부분으로 옮기고 싶을 때 유용
select(flights,time_hour,air_time,everything())
# A tibble: 336,776 × 19
time_hour air_time year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier flight tailnum origin dest
<dttm> <dbl> <int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> <chr> <int> <chr> <chr> <chr>
1 2013-01-01 05:00:00 227 2013 1 1 517 515 2 830 819 11 UA 1545 N14228 EWR IAH
2 2013-01-01 05:00:00 227 2013 1 1 533 529 4 850 830 20 UA 1714 N24211 LGA IAH
3 2013-01-01 05:00:00 160 2013 1 1 542 540 2 923 850 33 AA 1141 N619AA JFK MIA
4 2013-01-01 05:00:00 183 2013 1 1 544 545 -1 1004 1022 -18 B6 725 N804JB JFK BQN
5 2013-01-01 06:00:00 116 2013 1 1 554 600 -6 812 837 -25 DL 461 N668DN LGA ATL
6 2013-01-01 05:00:00 150 2013 1 1 554 558 -4 740 728 12 UA 1696 N39463 EWR ORD
7 2013-01-01 06:00:00 158 2013 1 1 555 600 -5 913 854 19 B6 507 N516JB EWR FLL
8 2013-01-01 06:00:00 53 2013 1 1 557 600 -3 709 723 -14 EV 5708 N829AS LGA IAD
9 2013-01-01 06:00:00 140 2013 1 1 557 600 -3 838 846 -8 B6 79 N593JB JFK MCO
10 2013-01-01 06:00:00 138 2013 1 1 558 600 -2 753 745 8 AA 301 N3ALAA LGA ORD
# ℹ 336,766 more rows
# ℹ 3 more variables: distance <dbl>, hour <dbl>, minute <dbl>
# ℹ Use `print(n = ...)` to see more rows
4. 연습문제
Q1) select() 호출에서 한 변수 이름을 여러 번 포함하면 어떻게 되는가?
select(r,year,year)
# A tibble: 336,776 × 1
year
<int>
1 2013
2 2013
3 2013
4 2013
5 2013
6 2013
7 2013
8 2013
9 2013
10 2013
# ℹ 336,766 more rows
# ℹ Use `print(n = ...)` to see more rows
- 두 개가 출력되진 않는다. 즉 중복이 안된다.
'DS Study > R4DS(R언어)' 카테고리의 다른 글
[R4DS] [2-6] summarize() (0) | 2024.04.01 |
---|---|
[R4DS] [2-5] mutate() (0) | 2024.03.31 |
[R4DS] [2-3] arrange() (0) | 2024.03.31 |
[R4DS] [2-2] filter() (0) | 2024.03.31 |
[R4DS] [2-1] 데이터 변형 (nycflights13, tidyverse) (0) | 2024.03.30 |