forked from CrumpLab/LabJournalWebsite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJournal.Rmd
More file actions
251 lines (218 loc) · 3.4 KB
/
Journal.Rmd
File metadata and controls
251 lines (218 loc) · 3.4 KB
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
---
title: "R Basic Problems"
output:
html_document:
toc: true
toc_float: true
collapsed: false
number_sections: false
toc_depth: 1
#code_folding: hide
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(message=FALSE,warning=FALSE, cache=TRUE)
```
## Easy Practice Problems for Week 1
### Problem 1
Do simple math with numbers
```{r}
1+1
7-3
3*8
10/2
```
### Problem 2
Put numbers into variables-then do simple math
```{r}
AV<-3
C<-2
D<-4
AV+C
D-AV
C*D
D/C
```
###Problem 3
Place numbers 1-100 seperately
```{r}
for(i in 1:100){
print(i)
}
seq(1:100)
```
###Problem 4
Find sum of all intergers 1-100
```{r}
Vec<-(1:100)
sum(Vec)
```
###Problem 4.2
Summing using For Loop
```{r}
Numbers<-c()
Sum<-0
for (i in 1:100){
if(i%%1==0){
Numbers<-c(Numbers,i)
Sum<-Sum+i
}
}
print(Sum)
```
###Problem 5
Make your own summing function
```{r}
Sumfun<-function(a){
total<-0
for(i in a){
total<-total+i
}
return(total)
}
TestRun<-(1:3)
Sumfun(TestRun)
```
###Problem 6
List odd numbers from 1-100
```{r}
Oddnumbers<-c()
seq(1,99,2)
```
###Problem 7
Prime numbers 1-1000
```{r}
prime.numbers <- function(limit){
n <- 2:limit
i <- 1
while(i < length(n)){
p <- n[i]
not.prime <- n[which(n %% p==0)]
not.prime <- not.prime[! not.prime %in% p]
n <- n[! n %in% not.prime]
i <- i + 1
}
n
}
prime.numbers(1:1000)
```
###Problem 8
100 random numbers
```{r}
runif(100)
```
###Problem 9
100 random numbers within range
```{r}
runif(100,min=1,max=20.5)
```
###Problem 10
Creating functions for descriptive statistics
Mean
```{r}
Average<-function(x){
return(sum(x)/length(x))
}
Test<-(1:5)
Average(Test)
```
Mode
```{r}
Mode_A<-function(x){
B<-table(x)
which.max(B)
print(names(which.max(B)))
}
Mtest<-c(1, 2 , 3 , 3 , 3 , 3 , 4 , 4 , 5 , 6 , 6 , 6)
Mode_A(Mtest)
```
Range
```{r}
Range_A<-function(x){
return(max(x)-min(x))
}
Test3<-(1:10)
Range_A(Test3)
```
Median
```{r}
Middle<-function(x){
sort(x, decreasing = FALSE)
B<-length(x)
return(median(x))
}
test<-c(19,1,7,2,18,4)
Middle(test)
```
Standard Deviation
```{r}
SD_A<-function(x){
s<-mean(x)
i<-length(x)
return(sd(x))
}
Stest<-c(2,3,5,7,1)
SD_A(Stest)
```
### Problem 11
Number of characters in a string
```{r}
Vec<-"AngelinaVasquez"
strsplit(Vec,split="")
```
### Problem 12
Number of words in string
```{r}
Sentence<-"How is your day ?"
sapply(strsplit(Sentence," "), length)
```
### Problem 13
Number of sentences in string
```{r}
Sent<-" Hello. It is nice today. The weather is sunny."
table(unlist(strsplit(Sent,split="[.]")))
```
###Problem 14
Number of times letter occurs in string
```{r}
B<- " My name is Angelina"
table(unlist(strsplit(B,split="")))
```
###Problem 15
Is a word in this sentence?
```{r}
A<-"Hi"
B<-"Hi there"
C<-"There is a cat"
D<-"The cat is in the tree"
grepl(A, B)
```
###Problem 16
Current time in milliseconds
```{r}
print(as.numeric(Sys.time())*1000, digits=15)
```
###Problem 17
How long does it take for a code to run
```{r}
print(as.numeric(Sys.time())*1000, digits=15)
return(print(as.numeric(Sys.time())*1000, digits=15)-print(as.numeric(Sys.time())*1000, digits=15))
```
###Problem 18
###Problem 19
Put variable into a text file
```{r}
y<-13
write.csv(y)
```
###Problem 20
20x20 Matrix
```{r}
Mat<-matrix(runif(10),ncol=20,nrow=20)
print(Mat)
```
###Problem 21
Turn matrix into a txt file
```{r}
Matr<-matrix(runif(10),ncol=5,nrow=5)
write.csv(Matr)
```