-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.R
152 lines (127 loc) · 3.47 KB
/
README.R
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
## ----function_create-----------------------------------------------------
hw <- function(){
print("Hello, World")
}
hw()
## ------------------------------------------------------------------------
formals(hw)
## ------------------------------------------------------------------------
body(hw)
## ----function2_create----------------------------------------------------
p <- function(my_text){
print(my_text)
}
p("Hello, world")
p("Hola, mundo")
p("Howdy, Texas")
## ----plot_function_examp-------------------------------------------------
myplot <- function(x,y,grp,file){
my_p <- ggplot(data.frame(x,y,grp),aes(x=x,y=y)) +
geom_point(aes(color=grp, shape=grp),size=5) +
geom_smooth(method="lm",aes(colour=grp))+
labs(x=substitute(x),y=substitute(y))
ggsave(my_p,file=file)
return(my_p)
}
myplot(iris$Petal.Length,iris$Petal.Width,iris$Species,"petal_petal.jpg")
myplot(iris$Sepal.Length,iris$Sepal.Width,iris$Species,"sepal_l_petal_w.jpg")
## ----if_else_examp-------------------------------------------------------
odd_even <- function(num){
if(num %% 2 == 0){
print("EVEN")
} else {
print("ODD")
}
}
odd_even(27)
odd_even(34)
## ----if_else_examp2------------------------------------------------------
plus_minus <- function(num){
if(num>0){
print("plus")
} else if (num < 0) {
print("minus")
} else {
print("zero")
}
}
plus_minus(198)
plus_minus(-44)
plus_minus(37*0)
## ----for_examp1----------------------------------------------------------
p_vec <- function(vec){
for(i in vec){
print(i)
}
}
p_vec(1:2)
p_vec(letters)
## ----for_examp2----------------------------------------------------------
sum_vec <- function(vec){
j <- 0
for(i in vec){
j <- i+j
print(j)
}
}
sum_vec(1:2)
sum_vec(1:10)
## ----boot----------------------------------------------------------------
mean_boot <- function(vec, R=100){
out <- NULL
for(i in 1:R){
out[i] <- mean(sample(vec,length(vec),replace=T))
}
median(out)
}
mean_boot(rnorm(100))
mean_boot(iris$Petal.Length,R=5000)
## ----for_examp_print-----------------------------------------------------
# A simple vectorized operation
x <- 1:100
y <- 100:1
z <- x+y
z
## ----looping_vector_examp------------------------------------------------
#We will assume vectors of the same length...
add_vecs <- function(vec1,vec2){
out <- NULL
for(i in 1:length(vec1)){
out[i] <- vec1[i]+vec2[i]
}
out
}
add_vecs(x,y)
## ----for_vector_time-----------------------------------------------------
large_vec1 <- as.numeric(1:100000)
large_vec2 <- as.numeric(100000:1)
#Different speed
vec_time <- system.time(large_vec1+large_vec2)
loop_time <- system.time(add_vecs(large_vec1,large_vec2))
vec_time
loop_time
## ----looping_vector_examp2-----------------------------------------------
#We will assume vectors of the same length...
add_vecs2 <- function(vec1,vec2){
out <- vector("numeric",length(vec1))
for(i in 1:length(vec1)){
out[i] <- vec1[i]+vec2[i]
}
out
}
system.time(add_vecs2(large_vec1,large_vec2))
## ----Exercise1, echo=FALSE-----------------------------------------------
## ----ggplot_starter function, eval=FALSE---------------------------------
## plot_nla <- function(x,y,out=NULL){
## #ggplot2 code
## #Note: ggplot requires a data frame as input. How would you deal with that?
##
## #ggsave here
## #look into the is.null() function
## if(put condition here){
## ggsave()
## }
##
## #Need to return something ...
##
## }