-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomework10.Rmd
More file actions
130 lines (110 loc) · 3.03 KB
/
Copy pathHomework10.Rmd
File metadata and controls
130 lines (110 loc) · 3.03 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
---
title: "Homework 10- For Loops"
author: "Morgan Southgate"
date: "May 3, 2017"
output: html_document
---
## Number 1
### A function to calculate the number of zeroes in an atomic vector v
```{r}
zeroSum <- function(v=rep(-10:10,times=3)){
w <- vector(mode="numeric")
for (i in seq_along(v)){
if(v[i]==0) w[i] <- 1 else
w[i] <- 0
}
return(print(sum(w)))
}
zeroSum()
```
## Number 2
### Using subsetting rather than for loops to accomplish the same thing as in # 1
```{r}
v=rep(-10:10,times=3)
length(v[v==0])
```
##Number 3
### Code for the function maxDiff() which returns the maximum difference between all possible pairs of elements in an atomic vector
```{r}
maxDiff <- function(x=rep(-5:5,each=1)) {
diff <- c(dist(x))
return(max(diff))
}
maxDiff()
```
# Number 4
### Altering the maxDiff function to output the values and numbered positions of the maximally distant numbers in the atomic vector as the second and third elements in the output
### Based on code shared by Alex Burnham
```{r, echo=TRUE}
maxDiff1 <- function(x=rep(-5:5,each=1)){
mat <- matrix(nrow=length(x),ncol=length(x))
rownames(mat) <- x
colnames(mat)<- x
for (i in seq_along(x)){
for (j in seq_along(x)){
mat[i,j]<- x[i]-x[j]
}
}
z <- c(mat)
x <- which(mat==max(abs(mat)), arr.ind=TRUE)
w<-colnames(mat)[x[1,1]]
t<-rownames(mat)[x[1,2]]
return(list(c(w,t),x,max(abs(z))))
}
maxDiff1()
```
## Number 5
### Making a function maxDiff2 which does the same thing as maxDiff1 but that works by storing a temporary variable that keeps track of differences and retains the largest
### Based on code shared by Alex Looi
```{r}
maxDiff2 <- function(x=rep(-10:10,by=1)){
tempPairs =expand.grid(x,x)
diffPairs = abs(tempPairs$Var1 - tempPairs$Var2)
temp_max = 0
for(v in 1:length(diffPairs)){
if(temp_max <= diffPairs[v]){
temp_max = diffPairs[v]
}
}
return(temp_max)
}
maxDiff2()
```
## Number 6
### A function that takes as input two matrices and multiplies them together using the rules of matrix multiplication
### Based on code shared by Alex Looi
```{r}
matMult = function(m1=matrix(data=1:10,nrow=2,ncol=5),m2=t(m1)){
m = matrix(0, nrow(m1), ncol(m2))
if (nrow(m1) != ncol(m2)){
cat("input matrices are not correct dimensions", "\n")
} else {
for (i in 1:nrow(m1)){
for (j in 1:ncol(m2)){
cell_val = sum(m1[i,]*m2[,j])
m[i,j] = cell_val
}
}
}
return(m)
}
# test function with default parameter
matMult()
# compare results to those obtained by using the built-in R function
m1=matrix(data=1:10,nrow=2,ncol=5)
m2=t(m1)
m1%*%m2
```
## Number 7
### A function that takes two integers representing the numbers of rows and columns in a matrix and returns a matrix of these dimensions in which the value of each cell is the product of the row number x column number.
```{r}
matDims <- function(nr=sample(1:20,size=1),nc=sample(1:20,size=1)){
mat = matrix(0,nr=nr,nc=nc)
for (r in 1:nr){
for (c in 1:nc){
mat[r,c] = r*c
}}
return(mat)
}
matDims()
```