-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 17 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
plot(x,y)
set.seed(10)
x <- rbinom(100,1,0.5)
x
e <- rnorm(100,0,2)
y <- 0.5 + 2 * x + e
summary(y)
plot(x,y)
set.seed(1)
x <- rnorm(100)
log.mu <- 0.5 + 0.3 * x
y <- rpois(100,exp(log.mu))
summary(y)
plot(x,y)
set.seed(1)
sample(1:10,4)
sample(letters,5)
sample(letters,5)
sample(letters,5)
sample(letters,5)
sample(letters,5)
sample(1:10)
sample(1:10)
sample(1:10,replace = T)
mean(rnorm(1000000))
mean(rnorm(99 ^ 99))
mean(rnorm(99 ** 99))
mean(rnorm(99 ** 99))
mean(rnorm(99))
99 ** 99
x <- 99 ** 99
mean(rnorm(x))
mean(rnorm(999999999999999999999999999999999))
mean(rnorm(9999))
system.time()
system.time(mean(rnorm(999999999999)))
system.time(mean(rnorm(9999)))
system.time(mean(rnorm(99999)))
system.time(mean(rnorm(999999)))
#Elapsed time > user time
system.time(readLines('goal.com'))
#Elapsed time > user time
system.time(readLines('https://goal.com'))
hil <- function (n){
i<- 1:n
1 /outer(i - 1, i , "+")
}
system.time(hil(1000))
x <- hil(1000)
system.time(svd(x))
numeric(100)
numeric(5)
numeric(rnorm(10,10))
numeric(rnorm(10,10,1))
numeric(1:10)
system.time({
n <- 1000
r <- numeric(n)
for (i in 1:n){
x <- rnorm(n)
r[i] <- mean(x)
}
})
Rprof()
summaryRprof()
sample.interval = 1000
str(Rprof)
l
set.seed(1)
rpois(5, 2)
library(datasets)
Rprof()
fit <- lm(y ~ x1 + x2)
getwd()
outcome <- read.csv("C:/Users/kora/Downloads/Documents/outcome-of-care-measures.csv",
colClasses = "character" )
c
outome
outcome <- read.csv("C:/Users/kora/Downloads/Documents/outcome-of-care-measures.csv",
colClasses = "character" )
outcome
head(outcome)
ncol(outcome)
typeof(outcome)
nrow(outcome)
outcome[,11] <- as.numeric(outcome[,11])
hist(outcome[,11])
head(outcome$11)
head(outcome$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack)
head(outcome[,11])
names(outcome)
head(outcome$State)
head(outcome$Hospital.Name)
unique(outcome$Hospital.Name)
grep('Hospital',names(outcome))
names(outome)
names(outcome)
hosp_sort <- function(state,outcome){
#setting NA to be 0
rates[rates == "Not Available"] <- 0
index <- c(grep("^Hospital.*Death*", names(rates)))
mortality_rates <- rates[,c(2,7,index)]
names(mortality_rates)[3:5] <- c("heart attack", "heart failure", "pneumonia")
#Making rates numeric
mortality_rates[,3:5] <- apply(mortality_rates[,3:5],2,as.numeric)
mortality_rates[mortality_rates == 0] <- NA
selected_state <- mortality_rates[mortality_rates$State == state,]
order_selected <- arrange(selected_state, selected_state[,outcome], Hospital.Name, na.last=TRUE)
order_selected <- order_selected[complete.cases(order_selected[,outcome]),]
return(order_selected)
#na.omit(selected_state[order(c(selected_state[,outcome]), na.last = TRUE),])
}
biz[biz=="one"] <- 3
well <- function (){
biz[biz == "tw0"] <- 0
}
hosp_sort()
hosp_sort("AZ",'heart attack')
outcomes
outcome
outcome[outcome == "Not Available"]
outcome[outcome == "Not Availabl"]
nrow(outcome[outcome == "Not Available"] )''
nrow(outcome[outcome == "Not Available"] )
count.fields(outcome[outcome == "Not Available"] )
count.fields()
we <- outcome[outcome == "Not Available"]
we <- 0
grep("^Hospital.*Death*",names(outcome))
grep("Hospital.*Death*",names(outcome))
names(outcome)
apply(1:10, 1, print)
apply(1:10, 1, print())
complete.cases(1:10)
complete.cases(1:10)
complete.cases(NA)
complete.cases([NA,1,2,3,4])
bb <- (NA,1,2,3,4)
bb <- c(NA,1,2,3,4)
complete.cases(bb)
library(dyplt)
library(dyplr)
install.packages(dyplr)
library('dyplr')
install.packages('dyplr')
install.packages("dyplr")
> install_github("hadley/dplyr")
install_github("hadley/dplyr")
devtools::install_github("hadley/dplyr")
devtools::install_github("hadley/dplyr")
devtools::install_github("hadley/dplyr")
install.packages("rlang")
install.packages("rlang")
devtools::install_github("hadley/dplyr")
devtools::install_github("hadley/dplyr")
install.packages('rlabg')
install.packages('rlang')
install.packages("rlang")
devtools::install_github("hadley/dplyr")
devtools::install_github("hadley/dplyr")
install.packages("dplyr")
install.packages("dplyr")
library(dplyr)
chicago <- readRDS('chicago')
chicago <- readRDS('chicago.rds')
dim(chicago)
str(chicago)
names(chicago)
subset <- select(chicago, city:date)
head(subset)
chic.f <- filter(chicago,pm25tmean2 > 30)
str(chic.f)
chic.f <- filter(chicago, pm25tmean2 > 30 & tmpd > 80)
select(chic.f, date, tmpd, pm25tmean2)
chicago <- arrange(chicago, date)
head(select(chicago, date, pm25tmean2), 3)
#sorting the dates in descending order
chicago <- arrange(chicago, desc(date))
chicago <- rename(chicago, dewpoint = dptp, pm25 = pm25tmean2)
head(chicago[, 1:5], 3)
chicago <- mutate(chicago, pm25detrend = pm25 - mean(pm25, na.rm = TRUE))
#with air pollution data, we often want to detrend the data by subtracting the mean from the data.
chicago <- mutate(chicago, pm25detrend = pm25 - mean(pm25, na.rm = TRUE))
arrange(chicago,chicago[,"date"])
arrange(chicago,chicago[,"date"],pm25)
arrange(chicago,chicago[,"date"],pm25,na.last=TRUE)
str(arrange)
summary(arrange)
subOutcomes <- df[sample(nrow(outcomes), 20), ]
subOutcomes <- df[sample(nrow(outcome), 20), ]
subOutcomes <- df[sample(nrow(outcome), 20), ]
typeof(outcome)
sample(nrow(outcome))
sample(nrow(outcome),10)
data.frame(sample(nrow(outcome),10))
outcomeFrame <- as.data.frame(outcome)
View(outcomeFrame)
subOutcomes <- df[sample(nrow(outcomeFrame), 20), ]
subOutcomes <- outcomeFrame[sample(nrow(outcomeFrame), 20), ]
subOutcomes2 <- outcome[sample(nrow(outcome), 20), ]
arrange(subOutcomes,date,state)
subOutcomes2[subOutcomes2 == "Not Available"] <- 0
index <- c(grep("^Hospital.*Death*", names(subOutcomes2)))
mortality_rates <- rates[,c(2,7,index)]
subOutcomes2[subOutcomes2 == "Not Available"] <- 0
index <- c(grep("^Hospital.*Death*", names(subOutcomes2)))
mortality_rates <- outcomes[,c(2,7,index)]
subOutcomes2[subOutcomes2 == "Not Available"] <- 0
index <- c(grep("^Hospital.*Death*", names(subOutcomes2)))
mortality_rates <- subOutcomes2[,c(2,7,index)]
names(mortality_rates)[3:5] <- c("heart attack", "heart failure", "pneumonia")
mortality_rates[,3:5] <- apply(mortality_rates[,3:5],2,as.numeric)
index
mortality_rates[,3:5] <- apply(mortality_rates[,3:5],2,as.numeric)
names(mortality_rates)[3:5] <- c("heart attack", "heart failure", "pneumonia")
mortality_rates[,3:5] <- apply(mortality_rates[,3:5],2,as.numeric)
mortality_rates
mortality_rates[mortality_rates == 0] <- NA
texas <- mortality_rates[mortality_rates$State == "TX"]
texas <- mortality_rates[mortality_rates$State == "TX",]
TEXAS
texas
texas[,"pneumonia"]
arrange(texas,texas[,"TX"],Hospital.Name)
arrange(texas,texas[,"TX"],Hospital.Name,na.last=TRUE)
arrange(texas,texas[,"pneumonia"],Hospital.Name,na.last=TRUE)
arrange(texas,texas[,"pneumonia"],Hospital.Name)
texas
subOutcomes2 <- outcome[sample(nrow(outcome), 100), ]
subOutcomes2[subOutcomes2 == "Not Available"] <- 0
index <- c(grep("^Hospital.*Death*", names(subOutcomes2)))
mortality_rates <- subOutcomes2[,c(2,7,index)]
names(mortality_rates)[3:5] <- c("heart attack", "heart failure", "pneumonia")
mortality_rates[,3:5] <- apply(mortality_rates[,3:5],2,as.numeric)
mortality_rates[mortality_rates == 0] <- NA
texas <- mortality_rates[mortality_rates$State == "TX"]
texas <- mortality_rates[mortality_rates$State == "TX",]
texas
arrange(texas,texas[,"pneumonia"])
arrange(texas,texas[,"pneumonia"],Hospital.Name)
arrange(texas,texas[,"pneumonia"],Hospital.Name,na.last = T)
order_selected <- arrange(texas,texas[,"pneumonia"],Hospital.Name,na.last = T)
order_selected <- order_selected[complete.cases(order_selected[,"heart failure"])]
order_selected <- order_selected[complete.cases(order_selected[,"heart failure"]),]
order_selected
order_selected <- order_selected[complete.cases(order_selected[,"pneumonia"]),]
order_selected
arrange(texas,texas[,"heart failure"],Hospital.Name,na.last = T)
arrange(texas,texas[,"heart attack"],Hospital.Name,na.last = T)
best <- function(state, outcome) {
## Read outcome data
outcomes <- read.csv("C:/Users/kora/Downloads/Documents/outcome-of-care-measures.csv",
colClasses = "character" )
## Check that state and outcome are valid
if (!state %in% unique(outcomes$State))
return("Invalid State")
if (!outcome %in% c("heart attack", "heart failure", "pneumonia"))
return("Invalid Outcome")
## Return hospital name in that state with lowest 30-day death
order_selected <- hosp_sort(state,outcome)
## rate
return(order_selected[1,1])
}
hosp_sort <- function(state,outcome){
#setting NA to be 0
outcomes[outcomes == "Not Available"] <- 0
index <- c(grep("^Hospital.*Death*", names(outcomes)))
#this creates a dataframe for the mortality rates columns
mortality_rates <- outcomes[,c(2,7,index)]
#renaming the titles of the mortality rates in the new list
names(mortality_rates)[3:5] <- c("heart attack", "heart failure", "pneumonia")
#Making rates numeric
mortality_rates[,3:5] <- apply(mortality_rates[,3:5],2,as.numeric)
#Getting the morrality rates for the specified state in the function call.
mortality_rates[mortality_rates == 0] <- NA
selected_state <- mortality_rates[mortality_rates$State == state,]
#This applies they dplyr package method arrange to order the outcomes of the selected states.
#first it sorts by outcomeS by the selected state code and then according to the hospital name.
order_selected <- arrange(selected_state, selected_state[,outcome], Hospital.Name, na.last=TRUE)
order_selected <- order_selected[complete.cases(order_selected[,outcome]),]
return(order_selected)
#na.omit(selected_state[order(c(selected_state[,outcome]), na.last = TRUE),])
}
best("SC", "heart attack")
outcomes <- read.csv("C:/Users/kora/Downloads/Documents/outcome-of-care-measures.csv",
colClasses = "character" )
outcomes <- read.csv("C:/Users/kora/Downloads/Documents/outcome-of-care-measures.csv",
colClasses = "character" )
best("SC", "heart attack")
best("NY", "pneumonia")
best("AK", "pneumonia")
rankhospital("NC", "heart attack", "worst")
rankhospital <- function(state, outcome, num = "best") {
## Read outcome data
outcomes <- read.csv("C:/Users/kora/Downloads/Documents/outcome-of-care-measures.csv",
colClasses = "character" )
## Check that state and outcome are valid -> This check is achieved in the best and worst functions above.
if (num == "best")
return(best(state,outcome))
if (num == "worst")
return(worst(state,outcome))
else {
order_selected <- hosp_sort(state,outcome)
## Return hospital name in that state with the given rank
## 30-day death rate
return(order_selected[num,1])
}
}
rankhospital("NC", "heart attack", "worst")
worst <- function(state, outcome, st = "a") {
if (!state %in% unique(rates$State))
return("Invalid State")
if (!outcome %in% c("heart attack", "heart failure", "pneumonia"))
return("Invalid Outcome")
order_selected <- hosp_sort(state,outcome)
#This function returns the worst for the given outcome and state.
return(order_selected[nrow(order_selected),1])
}
rankhospital("NC", "heart attack", "worst")
rankhospital("NC", "heart attack", "worst")
traceback(())
traceback(
)
debug(rankhospital("NC", "heart attack", "worst"))
debug(rankhospital("NC", "heart attack", "worst"))
worst <- function(state, outcome, st = "a") {
if (!state %in% unique(outcomes$State))
return("Invalid State")
if (!outcome %in% c("heart attack", "heart failure", "pneumonia"))
return("Invalid Outcome")
order_selected <- hosp_sort(state,outcome)
#This function returns the worst for the given outcome and state.
return(order_selected[nrow(order_selected),1])
}
rankhospital("NC", "heart attack", "worst")
rankhospital("WA", "heart attack", 7)
rankhospital("TX", "pneumonia", 10)
rankhospital("NY", "heart attack", 7)
r <- rankall("heart attack", 4)
rankall <- function(outcome, num = "best") {
## Read outcome data
## Check that state and outcome are valid
## For each state, find the hospital of the given rank
## Return a data frame with the hospital names and the
## (abbreviated) state name
results <- unlist(lapply(sort(unique(rates$State)), rankhospital, outcome, num),use.names=FALSE)
hosp <- results[c(TRUE,FALSE)]
state <- results[c(FALSE,TRUE)]
all <- data.frame(hosp,state)
names(all) <- c("Hospital.Name", "state")
all
}
r <- rankall("heart attack", 4)
rankall <- function(outcome, num = "best") {
## Read outcome data
## Check that state and outcome are valid
## For each state, find the hospital of the given rank
## Return a data frame with the hospital names and the
## (abbreviated) state name
results <- unlist(lapply(sort(unique(outcomes$State)), rankhospital, outcome, num),use.names=FALSE)
hosp <- results[c(TRUE,FALSE)]
state <- results[c(FALSE,TRUE)]
all <- data.frame(hosp,state)
names(all) <- c("Hospital.Name", "state")
all
}
r <- rankall("heart attack", 4)
as.character(subset(r, state == "HI")$hospital)
r
rankall <- function(outcome, num = "best") {
## Read outcome data
## Check that state and outcome are valid
## For each state, find the hospital of the given rank
## Return a data frame with the hospital names and the
## (abbreviated) state name
results <- unlist(lapply(sort(unique(outcomes$State)), rankhospital, outcome, num),use.names=FALSE)
hosp <- results[c(TRUE,FALSE)]
state <- results[c(FALSE,TRUE)]
all <- data.frame(hosp,state)
names(all) <- c("Hospital.Name", "state")
all
}
r <- rankall("heart attack", 4)
r
outcomes <- read.csv("C:/Users/kora/Downloads/Documents/outcome-of-care-measures.csv",
colClasses = "character" )
hosp_sort <- function(state,outcome){
#setting NA to be 0
outcomes[outcomes == "Not Available"] <- 0
index <- c(grep("^Hospital.*Death*", names(outcomes)))
mortality_outcomes <- outcomes[,c(2,7,index)]
names(mortality_outcomes)[3:5] <- c("heart attack", "heart failure", "pneumonia")
#Making outcomes numeric
mortality_outcomes[,3:5] <- apply(mortality_outcomes[,3:5],2,as.numeric)
mortality_outcomes[mortality_outcomes == 0] <- NA
selected_state <- mortality_outcomes[mortality_outcomes$State == state,]
order_selected <- arrange(selected_state, selected_state[,outcome], Hospital.Name, na.last=TRUE)
order_selected <- order_selected[complete.cases(order_selected[,outcome]),]
return(order_selected)
#na.omit(selected_state[order(c(selected_state[,outcome]), na.last = TRUE),])
}
best <- function(state, outcome, st = "a") {
if (!state %in% unique(outcomes$State))
return("Invalid State")
if (!outcome %in% c("heart attack", "heart failure", "pneumonia"))
return("Invalid Outcome")
order_selected <- hosp_sort(state,outcome)
return(order_selected[1,c(1,2)])
}
worst <- function(state, outcome, st = "a") {
if (!state %in% unique(outcomes$State))
return("Invalid State")
if (!outcome %in% c("heart attack", "heart failure", "pneumonia"))
return("Invalid Outcome")
order_selected <- hosp_sort(state,outcome)
return(order_selected[nrow(order_selected),c(1,2)])
}
rankhospital <- function(state, outcome, num = "best", st = "a"){
if (num == "best")
return(best(state,outcome))
if (num == "worst")
return(worst(state,outcome))
else {order_selected <- hosp_sort(state,outcome)
return(order_selected[num,c(1,2)])}
}
r <- rankall("heart attack", 4)
r
rankall <- function(outcome, num = "best") {
#print(lapply(unique(rates$State),hosp_sort, outcome))
results <- unlist(lapply(sort(unique(rates$State)), rankhospital, outcome, num),use.names=FALSE)
hosp <- results[c(TRUE,FALSE)]
state <- results[c(FALSE,TRUE)]
all <- data.frame(hosp,state)
names(all) <- c("Hospital.Name", "state")
all
}
r <- rankall("heart attack", 4)
rankall <- function(outcome, num = "best") {
#print(lapply(unique(rates$State),hosp_sort, outcome))
results <- unlist(lapply(sort(unique(outcomes$State)), rankhospital, outcome, num),use.names=FALSE)
hosp <- results[c(TRUE,FALSE)]
state <- results[c(FALSE,TRUE)]
all <- data.frame(hosp,state)
names(all) <- c("Hospital.Name", "state")
all
}
r <- rankall("heart attack", 4)
r
as.character(subset(r, state == "HI")$hospital)
r
r <- rankall("heart attack", 4)
as.character(subset(r, state == "HI")$hospital)
subset(r, state == "HI")$hospital
subset(r, state == "HI")$Hospital.Name
r <- rankall("pneumonia", "worst")
as.character(subset(r, state == "NJ")$Hospital.Name)
r <- rankall("heart failure", 10)
as.character(subset(r, state == "Nv")$Hospital.Name)
as.character(subset(r, state == "NV")$Hospital.Name)
read.table('C:/Users/kora/Downloads/Documents/snp_result (1).txt')
read.table('C:/Users/kora/Downloads/Documents/snp_result (1).txt',sep="\t")
results <- read.table('C:/Users/kora/Downloads/Documents/snp_result (1).txt',sep="\t")
head(results)
View(results)
names(results)
results['v5']
results[,'v5']
results[,5]
unique(results[,8])
results_snps <- cbind(results[,5],results[,8])
results_snps
names(results_snps) <- c("snps","function_class")
head(results_snps)
names(results_snps)
View(results_snps)
names(results_snps)[1:2] <- c("snp","function_class")
names(results_snps)
install.packages('vcfr')
install.packages('vcfr')
install.packages('vcfR')
library(vcfR)
results_vcf <- read.vcfR("C:/Users/kora/Desktop/Phd-bioinformatics/Omega/Popluation Genetics and GWAS/Assignment/22.36649117-36663577.ALL.chr22_GRCh38.genotypes.20170504.vcf")
head(results_vcf)
typeof(results_vcf)
View(results_vcf)
rv <- results_vcf
names(RV)
names(rv)
rv[1]
rv[1,]
rv[1,1]
rv[,2]
rv
names(results_vcf)
results_vcf[,1]
results_vcf[,2]
results_vcf
head(results_vcf)
rv@meta
rv@fix
metadata <- rv@meta
typeof(metadata)
fix_vcf <- rv@fix
names(fix_vcf)
typeof(fix_vcf)
head(fix_vcf)
results_vcf@fix
head(fix_vcf)
head(results_vcf@fix)
results_vcf@fix[1,]
gtvcf <- results_vcf@gt
head(gtvcf)