generated from tmarumt/r-devcontainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch03_attribute_data_operations.r
101 lines (77 loc) · 2.97 KB
/
ch03_attribute_data_operations.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
library(tidyverse)
library(sf) # Chapter 2 で紹介したベクタデータパッケージ
library(terra) # Chapter 2 で紹介したラスタデータパッケージ
library(dplyr) # データフレーム操作用 tidyverseパッケージ
library(spData) # Chapter 2 で紹介した空間データパッケージ
methods(class = "sf") # sf オブジェクトのメソッド、最初の 12
st_sf(data.frame(n = world$name_long), g = world$geom)
class(world) # sf オブジェクトであり、(tidy) データフレームである
dim(world) # 2次元オブジェクトで、 177 行 11 列
world_df <- st_drop_geometry(world)
class(world_df)
ncol(world_df)
world[1:6, ] # 位置で行を抽出
world[, 1:3] # 位置で列を抽出
world[1:6, 1:3] # 位置で行と列を抽出
world[, c("name_long", "pop")] # 名称で列を抽出
world[, c(T, T, F, F, F, F, F, T, T, F, F)] # 論理値で抽出
world[, 888] # 存在しない列番号
i_small <- world$area_km2 < 10000
summary(i_small) # 論理ベクトル
small_countries <- world[i_small, ]
small_countries <- world[world$area_km2 < 10000, ]
small_countries <- subset(world, area_km2 < 10000)
world1 <- select(world, name_long, pop)
names(world1)
# name_long から pop までの全ての列
world2 <- select(world, name_long:pop)
# subregion と area_km2 以外全ての列
world3 <- select(world, -subregion, -area_km2)
world4 <- select(world, name_long, population = pop)
world5 <- world[, c("name_long", "pop")] # 名称で列を抽出
names(world5)[names(world5) == "pop"] <- "population" # 列めいを変更
pull(world, pop)
world$pop
world[["pop"]]
slice(world, 1:6)
world7 <- filter(world, area_km2 < 10000) # 面積の小さい国
world7 <- filter(world, lifeExp > 82) # 平均寿命が高い
world7 <- world |>
filter(continent == "Asia") |>
select(name_long, continent) |>
slice(1:5)
world8 <- slice(
select(
filter(world, continent == "Asia"),
name_long, continent
),
1:5
)
world9_filtered <- filter(world, continent == "Asia")
world9_selected <- select(world9_filtered, continent)
world9 <- slice(world9_selected, 1:5)
world_agg1 <- aggregate(pop ~ continent,
FUN = sum, data = world,
na.rm = TRUE
)
class(world_agg1)
world_agg2 <- aggregate(world["pop"],
by = list(world$continent), FUN = sum,
na.rm = TRUE
)
class(world_agg2)
nrow(world_agg2)
world_agg3 <- world |>
group_by(continent) |>
summarize(pop = sum(pop, na.rm = TRUE))
world_agg4 <- world |>
group_by(continent) |>
summarize(Pop = sum(pop, na.rm = TRUE), Area = sum(area_km2), N = n())
world_agg5 <- world |>
st_drop_geometry() |> # 速くするためジオメトリを削除
select(pop, continent, area_km2) |> # 関心ある列のみの部分集合
group_by(continent) |> # 大陸でグループ化し要約
summarize(Pop = sum(pop, na.rm = TRUE), Area = sum(area_km2), N = n()) |>
mutate(Density = round(Pop / Area)) |> # 人口密度を計算
slice_max(Pop, n = 3) |> # 上位3件のみ
arrange(desc(N)) # 国数で並べ替え