R기초; 데이터프레임 인덱싱 - 1
# 미국 50개 주 정보불러오기
?state
state.abb
state.area
state.name
state.region
us.state <- data.frame(state.abb, state.name, state.region, state.area, stringsAsFactors=FALSE)
us.state
state.abb state.name state.region state.area
1 AL Alabama South 51609
2 AK Alaska West 589757
3 AZ Arizona West 113909
4 AR Arkansas South 53104
5 CA California West 158693
6 CO Colorado West 104247
7 CT Connecticut Northeast 5009
8 DE Delaware South 2057
9 FL Florida South 58560
10 GA Georgia South 58876
11 HI Hawaii West 6450
12 ID Idaho West 83557
13 IL Illinois North Central 56400
14 IN Indiana North Central 36291
15 IA Iowa North Central 56290
16 KS Kansas North Central 82264
17 KY Kentucky South 40395
18 LA Louisiana South 48523
19 ME Maine Northeast 33215
20 MD Maryland South 10577
21 MA Massachusetts Northeast 8257
22 MI Michigan North Central 58216
23 MN Minnesota North Central 84068
24 MS Mississippi South 47716
25 MO Missouri North Central 69686
26 MT Montana West 147138
27 NE Nebraska North Central 77227
28 NV Nevada West 110540
29 NH New Hampshire Northeast 9304
30 NJ New Jersey Northeast 7836
31 NM New Mexico West 121666
32 NY New York Northeast 49576
33 NC North Carolina South 52586
34 ND North Dakota North Central 70665
35 OH Ohio North Central 41222
36 OK Oklahoma South 69919
37 OR Oregon West 96981
38 PA Pennsylvania Northeast 45333
39 RI Rhode Island Northeast 1214
40 SC South Carolina South 31055
41 SD South Dakota North Central 77047
42 TN Tennessee South 42244
43 TX Texas South 267339
44 UT Utah West 84916
45 VT Vermont Northeast 9609
46 VA Virginia South 40815
47 WA Washington West 68192
48 WV West Virginia South 24181
49 WI Wisconsin North Central 56154
50 WY Wyoming West 97914
str(us.state)
us.state[[2]]
[1] "Alabama" "Alaska" "Arizona" "Arkansas" "California"
[6] "Colorado" "Connecticut" "Delaware" "Florida" "Georgia"
[11] "Hawaii" "Idaho" "Illinois" "Indiana" "Iowa"
[16] "Kansas" "Kentucky" "Louisiana" "Maine" "Maryland"
[21] "Massachusetts" "Michigan" "Minnesota" "Mississippi" "Missouri"
[26] "Montana" "Nebraska" "Nevada" "New Hampshire" "New Jersey"
[31] "New Mexico" "New York" "North Carolina" "North Dakota" "Ohio"
[36] "Oklahoma" "Oregon" "Pennsylvania" "Rhode Island" "South Carolina"
[41] "South Dakota" "Tennessee" "Texas" "Utah" "Vermont"
[46] "Virginia" "Washington" "West Virginia" "Wisconsin" "Wyoming"
str(us.state[[2]])
chr [1:50] "Alabama" "Alaska" "Arizona" "Arkansas" "California" "Colorado" "Connecticut" ...
us.state[2]
state.name
1 Alabama
2 Alaska
3 Arizona
4 Arkansas
5 California
6 Colorado
7 Connecticut
8 Delaware
9 Florida
10 Georgia
11 Hawaii
12 Idaho
13 Illinois
14 Indiana
15 Iowa
16 Kansas
17 Kentucky
18 Louisiana
19 Maine
20 Maryland
21 Massachusetts
22 Michigan
23 Minnesota
24 Mississippi
25 Missouri
26 Montana
27 Nebraska
28 Nevada
29 New Hampshire
30 New Jersey
31 New Mexico
32 New York
33 North Carolina
34 North Dakota
35 Ohio
36 Oklahoma
37 Oregon
38 Pennsylvania
39 Rhode Island
40 South Carolina
41 South Dakota
42 Tennessee
43 Texas
44 Utah
45 Vermont
46 Virginia
47 Washington
48 West Virginia
49 Wisconsin
50 Wyoming
us.state[c(2, 4)]
state.name state.area
1 Alabama 51609
2 Alaska 589757
3 Arizona 113909
4 Arkansas 53104
5 California 158693
6 Colorado 104247
7 Connecticut 5009
8 Delaware 2057
9 Florida 58560
10 Georgia 58876
11 Hawaii 6450
12 Idaho 83557
13 Illinois 56400
14 Indiana 36291
15 Iowa 56290
16 Kansas 82264
17 Kentucky 40395
18 Louisiana 48523
19 Maine 33215
20 Maryland 10577
21 Massachusetts 8257
22 Michigan 58216
23 Minnesota 84068
24 Mississippi 47716
25 Missouri 69686
26 Montana 147138
27 Nebraska 77227
28 Nevada 110540
29 New Hampshire 9304
30 New Jersey 7836
31 New Mexico 121666
32 New York 49576
33 North Carolina 52586
34 North Dakota 70665
35 Ohio 41222
36 Oklahoma 69919
37 Oregon 96981
38 Pennsylvania 45333
39 Rhode Island 1214
40 South Carolina 31055
41 South Dakota 77047
42 Tennessee 42244
43 Texas 267339
44 Utah 84916
45 Vermont 9609
46 Virginia 40815
47 Washington 68192
48 West Virginia 24181
49 Wisconsin 56154
50 Wyoming 97914
us.state[, 2]
[1] "Alabama" "Alaska" "Arizona" "Arkansas" "California"
[6] "Colorado" "Connecticut" "Delaware" "Florida" "Georgia"
[11] "Hawaii" "Idaho" "Illinois" "Indiana" "Iowa"
[16] "Kansas" "Kentucky" "Louisiana" "Maine" "Maryland"
[21] "Massachusetts" "Michigan" "Minnesota" "Mississippi" "Missouri"
[26] "Montana" "Nebraska" "Nevada" "New Hampshire" "New Jersey"
[31] "New Mexico" "New York" "North Carolina" "North Dakota" "Ohio"
[36] "Oklahoma" "Oregon" "Pennsylvania" "Rhode Island" "South Carolina"
[41] "South Dakota" "Tennessee" "Texas" "Utah" "Vermont"
[46] "Virginia" "Washington" "West Virginia" "Wisconsin" "Wyoming"
us.state[, 2, drop=FALSE]
state.name
1 Alabama
2 Alaska
3 Arizona
4 Arkansas
5 California
us.state[, c(2, 4)]
state.name state.area
1 Alabama 51609
2 Alaska 589757
3 Arizona 113909
4 Arkansas 53104
5 California 158693
us.state[["state.name"]]
us.state$state.name
us.state[, "state.name"]
[1] "Alabama" "Alaska" "Arizona" "Arkansas" "California"
[6] "Colorado" "Connecticut" "Delaware" "Florida" "Georgia"
[11] "Hawaii" "Idaho" "Illinois" "Indiana" "Iowa"
[16] "Kansas" "Kentucky" "Louisiana" "Maine" "Maryland"
[21] "Massachusetts" "Michigan" "Minnesota" "Mississippi" "Missouri"
[26] "Montana" "Nebraska" "Nevada" "New Hampshire" "New Jersey"
[31] "New Mexico" "New York" "North Carolina" "North Dakota" "Ohio"
[36] "Oklahoma" "Oregon" "Pennsylvania" "Rhode Island" "South Carolina"
[41] "South Dakota" "Tennessee" "Texas" "Utah" "Vermont"
[46] "Virginia" "Washington" "West Virginia" "Wisconsin" "Wyoming"
us.state[c("state.name", "state.area")]
us.state[, c("state.name", "state.area")]
state.name state.area
1 Alabama 51609
2 Alaska 589757
3 Arizona 113909
4 Arkansas 53104
5 California 158693
# 미국 50개 주의 정보
state.x77
Population Income Illiteracy Life Exp Murder HS Grad Frost Area
Alabama 3615 3624 2.1 69.05 15.1 41.3 20 50708
Alaska 365 6315 1.5 69.31 11.3 66.7 152 566432
Arizona 2212 4530 1.8 70.55 7.8 58.1 15 113417
Arkansas 2110 3378 1.9 70.66 10.1 39.9 65 51945
California 21198 5114 1.1 71.71 10.3 62.6 20 156361
Colorado 2541 4884 0.7 72.06 6.8 63.9 166 103766
states <- data.frame(state.x77)
row.names(states)
state$Name <- row.names(states)
row.names(states) <- NULL
head(states)
population income illiteracy LifeExp Murder HS.Grad Frost Area Name
1 3615 3624 2.1 69.05 15.1 41.3 20 50708 1
2 365 6315 1.5 69.31 11.3 66.7 152 566432 2
3 2212 4530 1.8 70.55 7.8 58.1 15 113417 3
4 2110 3378 1.9 70.66 10.1 39.9 65 51945 4
5 21198 5114 1.1 71.71 10.3 62.6 20 156361 5
6 2541 4884 0.7 72.06 6.8 63.9 166 103766 6
# 소득 5000USD을 초과하는 주를 출력
rich.states <- states[states$Income > 5000, c("Name", "Income")]
rich.states
Name Income
2 2 6315
5 5 5114
7 7 5348
13 13 5107
20 20 5299
28 28 5149
30 30 5237
34 34 5087
# Extracting Large States
large.states <- states[states$Area > 100000, c("Name", "Area")]
large.states
Name Area
2 2 566432
3 3 113417
5 5 156361
6 6 103766
26 26 145587
28 28 109889
31 31 121412
43 43 262134
# 두 데이터프레임 결합하기
# 소득이 5000USD 초과이고, 주 크기가 100,000 초과인 주 추출
merge(rich.states, large.states)
Name Income Area
1 2 6315 566432
2 28 5149 109889
3 5 5114 156361
# 소득이 5000USD 초과이거나, 주 크기가 100,000 초과인 주 추출
merge(rich.states, large.states, all=TRUE)
Name Income Area
1 13 5107 NA
2 2 6315 566432
3 20 5299 NA
4 26 NA 145587
5 28 5149 109889
6 3 NA 113417
7 30 5237 NA
8 31 NA 121412
9 34 5087 NA
10 43 NA 262134
11 5 5114 156361
12 6 NA 103766
13 7 5348 NA
'공부 > R Programming' 카테고리의 다른 글
R 기초; 데이터프레임 인덱싱-3 (0) | 2021.01.10 |
---|---|
R기초; 데이터프레임 인덱싱 - 2 (0) | 2021.01.10 |
R 기초; 데이터프레임 확장 (0) | 2021.01.09 |
R기초; 데이터프레임 생성 (0) | 2021.01.06 |
R 기초; 리스트 인덱싱-2 (0) | 2021.01.06 |
댓글