-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
355 lines (355 loc) · 10.5 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
swirl()
setwd("~/Desktop/R")
swirl()
library("swirl")
swirl()
ls()
class(plants)
dim(plants)
nrow(plants)
ncol(plants)
object.size(plants)
names(plants)
head(plants)
head(plants,10)
tail(plants)
tail(plants,15)
summary(plants)
table(plants$Active_Growth_Period)
str(plants)
?sample
sample(1:6,4,replace=T)
sample(1:6,4,replace=TRUE)
sample(1:6,4,replace=TRUE)
sample(1:20,10)
LETTERS
sample(LETTERS)
flips<-sample(c(0,1),100,prob=c(0.3,0.7))
flips<-sample(c(0,1),size=100,replace=FALSE,prob=c(0.3,0.7))
flips<-sample(x=c(0,1),size=100,replace=FALSE,prob=c(0.3,0.7))
flips<-sample(x=c(0,1),size=100,replace=TRUE,prob=c(0.3,0.7))
flips
sum(flips)
?rbinom
rbinom(1,size=100,prob=0.7)
flips<-rbinom(1,size=100,prob=0.7)
flips<-pbinom(1,size=100,prob=0.7)
flips2<-rbinom(1,size=100,prob=0.7)
flips2<-rbinom(100,size=1,prob=0.7)
flips2
rbinom(1,size=100,prob=0.7)
sum(flips2)
?rnorm
rnorm(100)
rnorm(10)
rnorm(10,mean=100,sd=25)
?rpois
rpois(5)
rpois(5,lambda = 10)
my_pois<-replicate(100,rpois(5,10))
my_pois
colMeans(my_pois)
cm<-colMeans(my_pois)
hist(cm)
data(cars)
?cars
head(cars)
plot(cars)
?plot
plot(x=cars$speed,y=cars$dist)
plot(dist ~ speed, cars)
plot(x = cars$dist, y = cars$speed)
plot(y = cars$dist, x = cars$speed)
plot(x=cars$speed,y=cars$dist,xlab="Speed")
plot(x=cars$speed,y=cars$dist,xlab="Speed",ylab="Stopping Distance")
plot(x=cars$speed,y=cars$dist,ylab="Stopping Distance")
plot(x=cars$speed,y=cars$dist,xlab="Speed",ylab="Stopping Distance")
plot(cars, main="My Plot")
plot(cars, submain="My Plot")
?plot
plot(cars, sub="My Plot")
plot(cars, sub="My Plot Subtitle")
plot(cars, col=2)
plot(cars,xlim=c(10,15))
plot(cars,pch=2)
data(mtcars)
?boxplot
boxplot(mpg ~ cyl, data=mtcars)
boxplot(formula=mpg ~ cyl, data=mtcars)
hist(mtcars$cyl)
hist(mtcars$mpg)
system.time(mean(c(1,2,3,4)))
set.seed(1)
rpois(5,2)
?set.seed
library(datasets)
Rprof()
fit<-lm(y~x1+x2)
Rprof(NULL)
x1<-1
x2<-2
fit<-lm(y~x1+x2)
y<-4
fit<-lm(y~x1+x2)
summaryRprof()
summaryRprof(lm)
summaryRprof(Rprof(lm))
summaryRprof(Rprof(lm(y~x1+x2)))
Rprof(NULL)
summaryRprof()
library(datasets)
Rprof()
fit <- lm(y ~ x1 + x2)
Rprof(NULL)
summaryRprof()
outcome<-read.csv("rprog-data-ProgAssignment3-data/outcome-of-care-measures.csv",colClasses = "character")
outcome<-read.csv("rprog-data-ProgAssignment3-data/outcome-of-care-measures.csv",colClasses = "character")
head(outcome)
names(outcome)
outcome[, 11] <- as.numeric(outcome[, 11])
hist(outcome[, 11])
sum(is.na(outcome[,11]))
nrow(outcome)
sum(is.na(outcome[,4]))
str(outcome$State)
sum(!is.na(outcome["AL",11]))
levels(outcome$State)
table(outcome$State)
sum(!is.na(outcome["GU",11]))
!is.na(outcome["GU",11])
!is.na(outcome["IL",11])
outcome["IL",11]
outcome["IL",11]
outcome[IL,11]
outcome["IL",1:3]
outcome$State=="IL"
[outcome$State=="IL",11]
outcome[outcome$State=="IL",11]
sum(!is.na(outcome[outcome$State==state,11]))
sum(!is.na(outcome[outcome$State=="IL",11]))
names(outcome)
str(outcome[,11])
split<-split(outcome[outcome$State=="IL",11],outcome$Hospital.Name)
min(outcome[,11])
min(outcome[,11],rm.na=T)
?min
min(outcome[,11],na.rm=T)
source(best.R)
source("best.R")
best<-function(state,outcome){
## Read outcome data
data<-read.csv("rprog-data-ProgAssignment3-data/outcome-of-care-measures.csv",colClasses = "character")
## Check that state and outcome are valid
col<-if(outcome=="heart attack"){11}
else if(outcome=="heart failure"){17}
else{23}
if(sum(is.na(data[data$State==state,col]))!=0){
## Return hospital name in that state with lowest 11
hospital-id<-which(min(data[data$State=state,col],na.rm=T))
return data[hospital-id,data$Hospital.Name]
}
else
stop("invalid state")
}
best<-function(state,outcome){
## Read outcome data
data<-read.csv("rprog-data-ProgAssignment3-data/outcome-of-care-measures.csv",colClasses = "character")
## Check that state and outcome are valid
col<-if(outcome=="heart attack"){11}
else if(outcome=="heart failure"){17}
else{23}
if(sum(is.na(data[data$State==state,col]))!=0){
## Return hospital name in that state with lowest 11
hospital-id<-which(min(data[data$State=state,col],na.rm=T))
return data[hospital-id,data$Hospital.Name]
}
else{stop("invalid state")}
}
# Function to find the best hospital in US
best<-function(state,outcome){
## Read outcome data
data<-read.csv("rprog-data-ProgAssignment3-data/outcome-of-care-measures.csv",colClasses = "character")
## Check that state and outcome are valid
col<-if(outcome=="heart attack"){11}
else if(outcome=="heart failure"){17}
else{23}
if(sum(is.na(data[data$State==state,col]))!=0){
## Return hospital name in that state with lowest 11
hospital_id<-which(min(data[data$State=state,col],na.rm=T))
return
data[hospital_id,data$Hospital.Name]
}
else{stop("invalid state")}
}
# Function to find the best hospital in US
best<-function(state,outcome){
## Read outcome data
data<-read.csv("rprog-data-ProgAssignment3-data/outcome-of-care-measures.csv",colClasses = "character")
## Check that state and outcome are valid
col<-if(outcome=="heart attack"){11}
else if(outcome=="heart failure"){17}
else{23}
if(sum(is.na(data[data$State==state,col]))!=0){
## Return hospital name in that state with lowest 11
hospital_id<-which(min(data[data$State==state,col],na.rm=T))
return
data[hospital_id,data$Hospital.Name]
}
else{stop("invalid state")}
}
source("best.R")
source("best.R")
best("TX","heart attack")
best<-function(state,outcome){
## Read outcome data
data<-read.csv("rprog-data-ProgAssignment3-data/outcome-of-care-measures.csv",colClasses = "character")
## Check that state and outcome are valid
col<-{if(outcome=="heart attack"){11}
else if(outcome=="heart failure"){17}
else{23}}
if(sum(is.na(data[data$State==state,col]))!=0){
## Return hospital name in that state with lowest 11
hospital_id<-which(min(data[data$State==state,col],na.rm=T))
return
data[hospital_id,data$Hospital.Name]
}
else{stop("invalid state")}
}
best("TX","heart attack")
source("best.R")
best("TX","heart attack")
# Function to find the best hospital in US
best<-function(state,outcome){
## Read outcome data
data<-read.csv("rprog-data-ProgAssignment3-data/outcome-of-care-measures.csv",colClasses = "character")
## Check that state and outcome are valid
col<-{if(outcome=="heart attack"){11}
else if(outcome=="heart failure"){17}
else{23}}
if(sum(!is.na(data[data$State==state,col]))==0){
## Return hospital name in that state with lowest 11
hospital_id<-which(min(data[data$State==state,col],na.rm=T))
return
data[hospital_id,data$Hospital.Name]
}
else{stop("invalid state")}
}
source("best.R")
best("TX","heart attack")
sum(!is.na(data[data$State==TX,11]))
sum(!is.na(data[data$State=="TX",11]))
best<-function(state,outcome){
data<-read.csv("rprog-data-ProgAssignment3-data/outcome-of-care-measures.csv",colClasses = "character")
col<-{if(outcome=="heart attack"){11}
else if(outcome=="heart failure"){17}
else{23}}
if(sum(!is.na(data[data$State==state,col]))==0){
}
}
data<-read.csv("rprog-data-ProgAssignment3-data/outcome-of-care-measures.csv",colClasses = "character")
data[data$State=="TX",11]
is.na(data[data$State=="TX",11])
sum(is.na(data[data$State=="TX",11]))
as.numberic(data[data$State=="TX",11])
as.numeric(data[data$State=="TX",11])
!is.na(as.numeric(data[data$State=="TX",11]))
sum(!is.na(as.numeric(data[data$State=="TX",11])))
# Function to find the best hospital in US
best<-function(state,outcome){
## Read outcome data
data<-read.csv("rprog-data-ProgAssignment3-data/outcome-of-care-measures.csv",colClasses = "character")
## Check that state and outcome are valid
col<-{if(outcome=="heart attack"){11}
else if(outcome=="heart failure"){17}
else{23}}
if(sum(!is.na(as.numeric(data[data$State==state,col]))==0){
## Return hospital name in that state with lowest 11
hospital_id<-which(min(data[data$State==state,col],na.rm=T))
return data[hospital_id,data$Hospital.Name]
}
else{stop("invalid state")}
}
# Function to find the best hospital in US
best<-function(state,outcome){
## Read outcome data
data<-read.csv("rprog-data-ProgAssignment3-data/outcome-of-care-measures.csv",colClasses = "character")
## Check that state and outcome are valid
col<-{if(outcome=="heart attack"){11}
else if(outcome=="heart failure"){17}
else{23}}
if(sum(!is.na(as.numeric(data[data$State==state,col]))==0)){
## Return hospital name in that state with lowest 11
hospital_id<-which(min(data[data$State==state,col],na.rm=T))
return data[hospital_id,data$Hospital.Name]
}
else{stop("invalid state")}
}
# Function to find the best hospital in US
best<-function(state,outcome){
## Read outcome data
data<-read.csv("rprog-data-ProgAssignment3-data/outcome-of-care-measures.csv",colClasses = "character")
## Check that state and outcome are valid
col<-{if(outcome=="heart attack"){11}
else if(outcome=="heart failure"){17}
else{23}}
if(sum(!is.na(as.numeric(data[data$State==state,col]))==0)){
## Return hospital name in that state with lowest 11
hospital_id<-which(min(data[data$State==state,col],na.rm=T))
return
data[hospital_id,data$Hospital.Name]
}
else{stop("invalid state")}
}
source("best.R")
best("TX","heart attack")
min(data[data$State=="TX",11])
min(data[data$State=="TX",11],na.rm = T)
which(min(data[data$State=="TX",11],na.rm = T))
?which
which.min(data[data$State=="TX",11],na.rm = T))
which.min(data[data$State=="TX",11],na.rm = T)
which.min(data[data$State=="TX",11])
# Function to find the best hospital in US
best<-function(state,outcome){
## Read outcome data
data<-read.csv("rprog-data-ProgAssignment3-data/outcome-of-care-measures.csv",colClasses = "character")
## Check that state and outcome are valid
col<-{if(outcome=="heart attack"){11}
else if(outcome=="heart failure"){17}
else{23}}
if(sum(!is.na(as.numeric(data[data$State==state,col]))==0)){
## Return hospital name in that state with lowest 11
hospital_id<-which.min(data[data$State==state,col])
return
data[hospital_id,data$Hospital.Name]
}
else{stop("invalid state")}
}
source("best.R")
best("TX","heart attack")
which.min(data[data$State=="TX",11])
id<-which.min(data[data$State=="TX",11])
id
data[id,data$Hospital.Name]
data$Hospital.Name[id]
# Function to find the best hospital in US
best<-function(state,outcome){
## Read outcome data
data<-read.csv("rprog-data-ProgAssignment3-data/outcome-of-care-measures.csv",colClasses = "character")
## Check that state and outcome are valid
col<-{if(outcome=="heart attack"){11}
else if(outcome=="heart failure"){17}
else{23}}
if(sum(!is.na(as.numeric(data[data$State==state,col]))==0)){
## Return hospital name in that state with lowest 11
hospital_id<-which.min(data[data$State==state,col])
return
data$Hospital.Name[hospital_id]
}
else{stop("invalid state")}
}
source("best.R")
best("TX", "heart attack")
soure("best.R")
source("best.R")
best("TX", "heart attack")