forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
404 lines (404 loc) · 13.9 KB
/
.Rhistory
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
dim(diamonds)
library("ggplot2")
data("diamonds")
ggplot(diamonds, aes(x = carat, y = price, color = clarity)) + geom_point()
ggplot(diamonds, aes(x = carat, y = price, color = clarity)) + geom_point() + geom_smooth()
ggplot(diamonds, aes(x = carat, y = price)) + geom_point() + geom_smooth()
ggplot(diamonds, aes(x = carat, y = price)) + geom_point() + ggtitle("Nhu cc")
ggplot(diamonds, aes(x = carat, y = price)) + geom_point() + ggtitle("Nhu cc") + xlim(0, 3)
ggplot(diamonds, aes(x = carat, y = price)) + geom_point() + ggtitle("Nhu cc") + scale_y_log10()
ggplot(diamonds, aes(x = carat, y = price)) + geom_point() + ggtitle("Nhu cc") + scale_y_log10() + scale_x_log10()
ggplot(diamonds, aes(x = price)) + geom_histogram()
ggplot(diamonds, aes(x = price)) + geom_histogram() + facet_wrap(~clarity, scale = 'free_y')
rm(diamonds)
data("WorldPhones")
View(WorldPhones)
install.packages("reshape2")
install.packages("reshape2")
library(reshape2)
WorldPhones.m = melt(WorldPhones)
View(WorldPhones.m)
colNames(WorldPhones.m) = c("Year", "Continent", "Phones")
colNames(WorldPhones.m) <- c("Year", "Continent", "Phones")
colnames(WorldPhones.m) <- c("Year", "Continent", "Phones")
View(WorldPhones.m)
swirl()
library(swirl)
swirl()
View(iris)
iris$Species
iris[3,]
head(iris, 5)
rm(list1, list2, iris)
library(ggplot2)
data(diamonds)
View(diamonds)
ggplot(diamonds, x = z, y = price) + geom_point()
ggplot(diamonds, aes(x = z, y = price)) + geom_point()
ggplot(diamonds, aes(x = z, y = price, color = cut)) + geom_point()
ggplot(diamonds, aes(x = z, y = price, color = cut)) + geom_point() + geom_smooth()
data(Orange)
View(Orange)
ggplot(Orange, aes(x = age, y = circumference)) + geom_point()
ggplot(Orange, aes(x = age, y = circumference, color = tree)) + geom_point()
ggplot(Orange, aes(x = age, y = circumference, color = tree)) + geom_point()
ggplot(Orange, aes(x = age, y = circumference, color = Tree)) + geom_point()
ggplot(Orange, aes(x = age, y = circumference, color = Tree)) + geom_point() + geom_smooth(se = F)
ggplot(diamonds, aes(x = z, y = price)) + geom_point() + facet_wrap(~cut)
ggplot(diamonds, aes(x = z, y = price)) + geom_point() + facet_wrap(color~cut)
ggplot(diamonds, aes(x = z, y = price)) + geom_point() + facet_wrap(cut~color)
ggplot(diamonds, aes(x = z, y = price)) + geom_point() + facet_grid(cut~color)
ggplot(diamonds, aes(x = z, y = price)) + geom_point() + ggtitle("My Graph") + ylim(0, 10000)
ggplot(Orange, aes(x = age, y = circumference)) + geom_point() + facet_wrap(~Tree)
ggplot(diamonds, aes(x = weight)) + geom_histogram()
ggplot(diamonds, aes(x = carat)) + geom_histogram()
ggplot(diamonds, aes(x = carat)) + geom_histogram(binwidth = .5)
ggplot(diamonds, aes(x = carat)) + geom_density()
ggplot(diamonds, aes(x = carat)) + geom_density() + facet_wrap(~clarity)
ggplot(diamonds, aes(x = carat, color = clarity)) + geom_density()
data(iris)
View(iris)
ggplot(iris, aes(x = Species, y = Petal.Length)) + geom_boxplot()
ggplot(iris, aes(x = Species, y = Petal.Length)) + geom_violin()
qplot(diamonds, carat, price)
qplot(diamonds$carat, diamonds$price)
library(reshape2)
data(french_fries)
View(french_fries)
ggplot(french_fries, aes(x=buttery, y = treatment)) + geom_boxplot()
ggplot(french_fries, aes(x=buttery)) + geom_boxplot()
ggplot(french_fries, aes(x=buttery, y = treatment)) + geom_boxplot()
french_fries.m = melt(french_fries)
ggplot(french_fries, aes(x=treatment, y = buttery)) + geom_boxplot()
french_fries.m = melt(french_fries)
?melt
french_fries.m = melt(french_fries, id = c(time, treatment, subject))
french_fries.m = melt(french_fries, id = c("time", "treatment", "subject"))
info()
french_fries.m = melt(french_fries, id = c("row.names","time", "treatment", "subject"))
french_fries.m = melt(french_fries, id = c("time", "treatment", "subject", "rep"))
View(french_fries.m)
ggplot(french_fries.m, aes(x=treatments, y = value)) + geom_boxplot() + facet_wrap(~variable)
ggplot(french_fries.m, aes(x=treatment, y = value)) + geom_boxplot() + facet_wrap(~variable)
View(iris)
p <- ggplot(iris, aes(x=Petal.Length, y=Petal.Width)) + geom_point()
ggsave("petalsize.jpeg")
data(mtcars)
View(mtcars)
library(ggplot2)
ggplot(mtcars, aes(x=am, y=mpg)) +geom_boxplot()
ggplot(mtcars, aes(x=factor(am), y=mpg)) +geom_boxplot()
ggplot(mtcars, aes(x=am, y=mpg)) +geom_boxplot()
ggplot(mtcars, aes(x=factor(am), y=mpg)) +geom_boxplot()
mtcars$am
factor(mtcars$am)
tt = t.test(mpg~am, mtcars)
tt
names(tt)
cor.test(mtcars$mpg, mtcars$wt)
data(mtcars)
View(mtcars)
head(mtcars)
library(ggplot2)
ggplot(mtcars, aes(x = factor(am), y = mpg)) + geom_boxplot()
t.test(mpg~am, data = mtcars)
tt = t.test(mpg~am, data = mtcars)
tt$conf.int
rm(tt)
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
cor.test(mtcars$mpg, mtcars$wt)
fit <- lm(mpg~wt, mtcars)
summary(fit)
mfit <- lm(mpg~wt + disp + cyl, mtcars)
mfit
summary(mfit)
rm(ls= list())
rm(list= ls
)
rm(list= ls())
library(swirl)
swirl()
data(CO2)
View(CO2)
ggplot(CO2, aes(x = factor(Treatment), y = uptake)) + geom_boxplot()
ggplot(CO2, aes(x = factor(Treatment), y = uptake)) + geom_boxplot()
ggplot(CO2, aes(x = Treatment, y = uptake)) + geom_boxplot()
t.test(uptake~Treatment, CO2)
0.003107
ggplot(CO2, aes(x = Type, y = uptake)) + geom_boxplot()
t.test(uptake~Type, CO2)
tt = t.test(uptake~Type, CO2)
tt$p.value
tt$conf.int
tt$conf.int[2]
tt$conf.int[1]
8.839475
rm(CO2, tt)
data(iris)
View(iris)
ggplot(iris, aes(x= Petal.Length, y= Petal.Width)) + geom_boxplot()
ggplot(iris, aes(x= Petal.Length, y= Petal.Width)) + geom_point()
ct = cor.test(iris$Petal.Length, iris$Petal.Width)
ct
ct$estimate
ct$p.value
ct$conf.int
View(CO2)
ggplot(CO2, aes(x= conc, y= uptake)) + geom_point()
fit = lm(uptake~conc, CO2)
summary(fit)
0.017731
2.91e-06
ggplot(CO2, aes(x= conc, y= uptake)) + geom_point() + scale_x_log10()
fit = lm(uptake~log(conc), CO2)
lfit = lm(uptake~log(conc), CO2)
lfit = lm(uptake ~ log(conc), CO2)
rm(fit, lfit)
lfit = lm(uptake ~ log(conc), CO2)
lfit = lm(uptake ~ log(conc), CO2)
info()
rm(ct, fit, lfit)
rm(fit)
lfit = lm(uptake ~ log(conc), CO2)
info()
skip()
lfit = lm(uptake ~ log(conc), data=CO2)
lfit = lm(uptake ~ log(conc), data=CO2)
info()
skip()
info()
main()
View(CO2)
ggplot(CO2, aes(x=conc, y=uptake)) + geom_point() + scale_x_log10()
ggplot(CO2, aes(x=conc, y=uptake, color=Type)) + geom_point() + scale_x_log10()
mfit = lm(uptake ~ log(conc) + Type)
mfit = lm(uptake ~ log(conc) + Type, data= CO2)
summary(mfit)
ggplot(CO2, aes(x=conc, y=uptake, color=Type, shape=Treatment)) + geom_point() + scale_x_log10()
mfit2 = lm(uptake ~ log(conc) + Type + Treatment, data= CO2)
summary(mfit2)
1.11e-08
lfit = lm(uptake ~ log(conc), data=CO2)
summary(lfit)
install.packages("RMySQL")
library(httr)
oauth_endpoints("github")
myapp <- oauth_app("github", "e59d24c4b89cc747216c")
github_token <- oauth2.0_token(oauth_endpoints("github"), myapp)
gtoken <- config(token = github_token)
rm(list=ls())
library(swirl)
install_from_swirl("Getting and Cleaning Data")
swirl()
?read.csv
mydf <- read.csv(path2csv, stringAsFactors=FALSE)
mydf <- read.csv(path2csv, strinsgAsFactors=FALSE)
mydf <- read.csv(path2csv, stringsAsFactors=FALSE)
dim(mydf)
head(mydg)
head(mydf)
library(dplyr)
packageVersion("dplyr")
cran <- tbl_df(mydf)
rm("mydf")
?tbl_df
print(cran)
cran
?select
select(cran, ip_id, package, country)
5:20
select(cran, r_arch:country)
select(cran, country:r_arch)
cran
select(cran, -time)
select(cran, -(X:size))
-5:20
-(5:20)
select(cran, -(X:size))
filter(cran, package == "swirl")
filter(cran, r_version == "3.1.1", country == "US")
?Comparison
filter(cran, country == "IN", r_version <= "3.0.2")
filter(cran, country == "IN" | country == "US")
filter(cran, size>100500, r_os == "linux-gnu")
in.na(c(3,5,NA,10))
is.na(c(3,5,NA,10))
!is.na(c(3,5,NA,10))
filter(cran, !is.na(r_version))
cran2 <- select(cran, size:ip_id)
arrange(cran2, ip_id)
arrange(cran2, desc(ip_id)
)
arrange(cran2, package, ip_id)
arrange(cran2, country, desc(r_version), ip_id)
cran3 <- select(cran, ip_id, package, size)
cran3
mutate(cran3, size_mb = size/2^20)
mutate(cran3, size_gb = size_mb/2^10)
mutate(cran3, size_mb = size/2^20, size_gb = size_mb/2^10)
mutate(cran3, correct_size = size + 1000)
summarize(cran, avg_bytes = mean(size))
library(dplyr)
cran <- tbl_df(mydf)
rm(mydf)
rm("mydf")
cran
?group_by
by_package <- group_by(cran, package)
by_package
summarize(by_package, mean(size))
?n
pack_sum <- summarize(by_package, count = n(), unique = n_distinct(ip_id), countries = n_distinc(country), avg_bytes = mean(size))
pack_sum <- summarize(by_package, count = n(), unique = n_distinct(ip_id), countries = n_distinct(country), avg_bytes = mean(size))
source('/var/folders/zf/70bcw_rd5k7b80xs93lc17kw0000gn/T//RtmpgP1h1V/summarize1.R')
submit()
pack_sum
quantile(pack_sum$count, probs = 0.99)
tail(pack_sum)
filter(pack_sum, count>679)
top_counts <- filter(pack_sum, count>679)
top_counts
View(top_counts)
arrange(top_counts, count)
arrange(top_counts, desc(count))
top_counts_sorted <- arrange(top_counts, desc(count))
View(top_counts_sorted)
quantile(pack_sum$unique, probs=0.99)
top_unique <- filter(pack_sum, unique>465)
View(top_unique)
top_unique_sorted <- arrange(top_unique, desc(unique))
View(top_unique_sorted)
submit()
submit()
?chain
submit()
View(result3)
submit()
submit()
submit()
submit()
submit()
infro()
info()
skip()
library(datasets)
data(airquality)
qplot(Wind, Ozone, data=airquality, facets = .~factor(Month))
library(lattice)
library(datasets)
data(airquality)
qplot(Wind, Ozone, data=airquality, facets = .~factor(Month))
library(ggplot2)
qplot(Wind, Ozone, data = airquality, facets = . ~ factor(Month))
airquality = transform(airquality, Month = factor(Month))
qplot(Wind, Ozone, data = airquality, facets = . ~ Month)
data(airquality)
qplot(Wind, Ozone, data = airquality, geom = "smooth")
qplot(Wind, Ozone, data = airquality, geom = "smooth") + facets(airquality, factor(Month))
dt <- read.csv("activity.csv", header=T, stringsAsFactors=F)
library(ggplot2)
library(dplyr)
## Loading and preprocessing the data
```{r}
dt <- read.csv("activity.csv", header=T, stringsAsFactors=F)
library(ggplot2)
library(dplyr)
```
## What is mean total number of steps taken per day?
```{r}
# caculate total number of steps by date
sum <- summarise(group_by(dt, date), steps=sum(steps))
sum
# histogram
ggplot(sum, aes(x=steps)) + geom_histogram()
# mean and median
summary(sum$steps)
```
## What is the average daily activity pattern?
```{r}
# average steps per interval
int <- summarise(group_by(dt, interval), steps=mean(steps, na.rm=T))
# convert integer interval to time format interval
# example: "105"" -> "0105""
int$interval <- sprintf("%04d", int$interval)
# plot
#ggplot(int, aes(x=strptime(int$interval, format="%H%M"), y=steps)) + geom_line()
plot(strptime(int$interval, format="%H%M"), int$steps, type="l", xlab="interval", ylab="average steps")
# which interval contains maximum num of steps
filter(int, steps==max(int$steps))
```
## Imputing missing values
```{r}
# number of missing value
nrow(dt[!complete.cases(dt), ])
# filling in missing value
filled.dt <- dt
# use mean of steps per interval for missing value
filled.dt[!complete.cases(filled.dt), ]$steps <- int$steps
# histogram total number of steps taken each day
filled.sum <- summarise(group_by(filled.dt, date), steps=sum(steps))
ggplot(filled.sum, aes(x=steps)) + geom_histogram()
# mean and median
summary(filled.sum$steps)
```
There you can see the median is now equal to the mean, which is the result of filling in missing value
## Are there differences in activity patterns between weekdays and weekends?
```{r}
# convert string date to Date object
filled.dt$date <- as.Date(filled.dt$date)
# add another column representing weekday or weekend
weekdays <- weekdays(filled.dt$date)
isWeekend <- factor(weekdays == "Saturday" | weekdays == "Sunday")
levels(isWeekend) <- c("weekday", "weekend")
filled.dt <- mutate(filled.dt, day=isWeekend)
head(filled.dt)
# multiplot
weekday <- filled.dt %>% filter(day=="weekday") %>% group_by(interval) %>% summarise(steps=mean(steps))
weekend <- filled.dt %>% filter(day=="weekend") %>% group_by(interval) %>% summarise(steps=mean(steps))
par(mfrow=c(1,1))
```
dt <- read.csv("activity.csv", header=T, stringsAsFactors=F)
library(ggplot2)
library(dplyr)
setwd("~/Desktop/Study/RepData_PeerAssessment1")
dt <- read.csv("activity.csv", header=T, stringsAsFactors=F)
library(ggplot2)
library(dplyr)
# number of missing value
nrow(dt[!complete.cases(dt), ])
# filling in missing value
filled.dt <- dt
# use mean of steps per interval for missing value
filled.dt[!complete.cases(filled.dt), ]$steps <- int$steps
# average steps per interval
int <- summarise(group_by(dt, interval), steps=mean(steps, na.rm=T))
# convert integer interval to time format interval
# example: "105"" -> "0105""
int$interval <- sprintf("%04d", int$interval)
nrow(dt[!complete.cases(dt), ])
# filling in missing value
filled.dt <- dt
# use mean of steps per interval for missing value
filled.dt[!complete.cases(filled.dt), ]$steps <- int$steps
# histogram total number of steps taken each day
filled.sum <- summarise(group_by(filled.dt, date), steps=sum(steps))
# convert string date to Date object
filled.dt$date <- as.Date(filled.dt$date)
# add another column representing weekday or weekend
weekdays <- weekdays(filled.dt$date)
isWeekend <- factor(weekdays == "Saturday" | weekdays == "Sunday")
levels(isWeekend) <- c("weekday", "weekend")
filled.dt <- mutate(filled.dt, day=isWeekend)
head(filled.dt)
# multiplot
weekday <- filled.dt %>% filter(day=="weekday") %>% group_by(interval) %>% summarise(steps=mean(steps))
weekend <- filled.dt %>% filter(day=="weekend") %>% group_by(interval) %>% summarise(steps=mean(steps))
View(weekday)
View(filled.dt)
View(weekend)
View(filled.dt)
plot(weekday$interval, weekday$steps)
plot(weekday$interval, weekday$steps, type="l")
weekday <- filled.dt %>% filter(day=="weekday") %>% group_by(interval) %>% summarise(steps=mean(steps)) %>% mutate(interval=sprintf("%04d", interval))
weekend <- filled.dt %>% filter(day=="weekend") %>% group_by(interval) %>% summarise(steps=mean(steps)) %>% mutate(interval=sprintf("%04d", interval))
View(weekday)