forked from jaimyoung/ipds-kr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_data_processing.Rmd
145 lines (102 loc) · 2.83 KB
/
1_data_processing.Rmd
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
---
title: "1_data_processing"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(gapminder)
```
library(gapminder)
```{r}
# 행과 열 선택
gapminder[gapminder$country=='Korea, Rep.', c('pop', 'gdpPercap')]
# 행 선택
gapminder[gapminder$country=='Korea, Rep.', ]
gapminder[gapminder$year==2007, ]
gapminder[gapminder$country=='Korea, Rep.' & gapminder$year==2007, ]
gapminder[1:10,]
head(gapminder, 10)
# 정렬
gapminder[order(gapminder$year, gapminder$country),]
# 변수 선택:
gapminder[, c('pop', 'gdpPercap')]
gapminder[, 1:3]
# 변수 이름 바꾸기: gdpPercap 를 gdp_per_cap 으로 변경
f2 = gapminder
names(f2)
names(f2)[6] = 'gdp_per_cap'
# 변수변환과 변수 생성
f2 = gapminder
f2$total_gdp = f2$pop * f2$gdpPercap
# 요약통계량 계산
median(gapminder$gdpPercap)
apply(gapminder[,4:6], 2, mean)
summary(gapminder)
```
```{r}
#----------------------------
library(dplyr)
# tbl_df() 와 glimpse()
i2 <- tbl_df(iris)
class(i2)
i2
glimpse(i2)
iris %>% head
iris %>% head(10)
filter(gapminder, country=='Korea, Rep.')
filter(gapminder, year==2007)
filter(gapminder, country=='Korea, Rep.' & year==2007)
gapminder %>% filter(country=='Korea, Rep.')
gapminder %>% filter(year==2007)
gapminder %>% filter(country=='Korea, Rep.' & year==2007)
arrange(gapminder, year, country)
gapminder %>% arrange(year, country)
select(gapminder, pop, gdpPercap)
gapminder %>% select(pop, gdpPercap)
gapminder %>%
mutate(total_gdp = pop * gdpPercap,
le_gdp_ratio = lifeExp / gdpPercap,
lgrk = le_gdp_ratio * 100)
gapminder %>%
summarize(n_obs = n(),
n_countries = n_distinct(country),
n_years = n_distinct(year),
med_gdpc = median(gdpPercap),
max_gdppc = max(gdpPercap))
sample_n(gapminder, 10)
sample_frac(gapminder, 0.01)
distinct(select(gapminder, country))
distinct(select(gapminder, year))
gapminder %>% select(country) %>% distinct()
gapminder %>% select(year) %>% distinct()
gapminder %>%
filter(year == 2007) %>%
group_by(continent) %>%
summarize(median(lifeExp))
# 함수형 프로그래밍의 장점 예시
d1 = filter(gapminder, year == 2007)
d2 = group_by(d1, continent)
d3 = summarize(d2, lifeExp = median(lifeExp))
arrange(d3, -lifeExp)
arrange(
summarize(
group_by(
filter(gapminder, year==2007), continent
), lifeExp=median(lifeExp)
), -lifeExp
)
gapminder %>%
filter(year == 2007) %>%
group_by(continent) %>%
summarize(lifeExp = median(lifeExp)) %>%
arrange(-lifeExp)
# 조인 연산자; inner, left, right, full(outer) join
(df1 <- data_frame(x = c(1, 2), y = 2:1))
(df2 <- data_frame(x = c(1, 3), a = 10, b = "a"))
df1 %>% inner_join(df2)
df1 %>% left_join(df2)
df1 %>% right_join(df2)
df1 %>% full_join(df2)
```