forked from kosukeimai/qss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intro.Rmd
205 lines (159 loc) · 3.69 KB
/
intro.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
---
title: 'Code for QSS Chapter 1: Introduction'
author: "Kosuke Imai"
date: "First Printing"
output:
pdf_document: default
---
# Section 1.1: Overview of the Book
# Section 1.2: How to Use this Book
```{r, eval = FALSE}
install.packages("swirl") # install the package
library(swirl) # load the package
install_course_github("kosukeimai", "qss-swirl") # install the course
library(swirl)
swirl()
```
# Section 1.3: Introduction to R
## Section 1.3.1: Arithmetic Operations
```{r}
5 + 3
5 - 3
5 / 3
5 ^ 3
5 * (10 - 3)
sqrt(4)
```
## Section 1.3.2: Objects
```{r}
result <- 5 + 3
result
print(result)
result <- 5 - 3
result
kosuke <- "instructor"
kosuke
kosuke <- "instructor and author"
kosuke
Result <- "5"
Result
result
class(result)
Result
class(Result)
class(sqrt)
```
## Section 1.3.3: Vectors
```{r}
world.pop <- c(2525779, 3026003, 3691173, 4449049, 5320817, 6127700, 6916183)
world.pop
pop.first <- c(2525779, 3026003, 3691173)
pop.second <- c(4449049, 5320817, 6127700, 6916183)
pop.all <- c(pop.first, pop.second)
pop.all
world.pop[2]
world.pop[c(2, 4)]
world.pop[c(4, 2)]
world.pop[-3]
pop.million <- world.pop / 1000
pop.million
pop.rate <- world.pop / world.pop[1]
pop.rate
pop.increase <- world.pop[-1] - world.pop[-7]
percent.increase <- (pop.increase / world.pop[-7]) * 100
percent.increase
percent.increase[c(1, 2)] <- c(20, 22)
percent.increase
```
## Section 1.3.4: Functions
```{r}
length(world.pop)
min(world.pop)
max(world.pop)
range(world.pop)
mean(world.pop)
sum(world.pop) / length(world.pop)
year <- seq(from = 1950, to = 2010, by = 10)
year
seq(to = 2010, by = 10, from = 1950)
seq(from = 2010, to = 1950, by = -10)
2008:2012
2012:2008
names(world.pop)
names(world.pop) <- year
names(world.pop)
world.pop
## myfunction <- function(input1, input2, ..., inputN) {
##
## DEFINE `output' USING INPUTS
##
## return(output)
## }
my.summary <- function(x){ # function takes one input
s.out <- sum(x)
l.out <- length(x)
m.out <- s.out / l.out
out <- c(s.out, l.out, m.out) # define the output
names(out) <- c("sum", "length", "mean") # add labels
return(out) # end function by calling output
}
z <- 1:10
my.summary(z)
my.summary(world.pop)
```
## Section 1.3.5: Data Files
```{r, eval = FALSE}
## setwd("qss/INTRO")
## getwd()
```
```{r}
UNpop <- read.csv("UNpop.csv")
class(UNpop)
load("UNpop.RData")
names(UNpop)
nrow(UNpop)
ncol(UNpop)
dim(UNpop)
summary(UNpop)
UNpop$world.pop
UNpop[, "world.pop"] # extract the column called "world.pop"
UNpop[c(1, 2, 3),] # extract the first three rows (and all columns)
UNpop[1:3, "year"] # extract the first three rows of the "year" column
## take elements 1, 3, 5, ... of the "world.pop" variable
UNpop$world.pop[seq(from = 1, to = nrow(UNpop), by = 2)]
world.pop <- c(UNpop$world.pop, NA)
world.pop
mean(world.pop)
mean(world.pop, na.rm = TRUE)
```
## Section 1.3.6: Saving Objects
```{r, eval = FALSE}
## save.image("qss/INTRO/Chapter1.RData")
## save(UNpop, file = "Chapter1.RData")
## save(world.pop, year, file = "qss/INTRO/Chapter1.RData")
## write.csv(UNpop, file = "UNpop.csv")
## load("Chapter1.RData")
```
## Section 1.3.7: Packages
```{r, eval = FALSE}
install.packages("foreign") # install package
library("foreign") # load package
read.dta("UNpop.dta")
read.spss("UNpop.sav")
write.dta(UNpop, file = "UNpop.dta")
```
## Section 1.3.8: Programming and Learning Tips
```{r, eval = FALSE}
source("UNpop.R")
##
## File: UNpop.R
## Author: Kosuke Imai
## The code loads the UN population data and saves it as a STATA file
##
library(foreign)
UNpop <- read.csv("UNpop.csv")
UNpop$world.pop <- UNpop$world.pop / 1000 # population in millions
write.dta(UNpop, file = "UNpop.dta")
library(lintr)
lint("UNpop.R")
```