forked from theomitsa/bilingual-R-Markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayoperations
More file actions
221 lines (189 loc) · 3.38 KB
/
Copy patharrayoperations
File metadata and controls
221 lines (189 loc) · 3.38 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
---
title: "pythonandR array operations"
author: "Theophano Mitsa"
date: "August 1, 2019"
output: html_document
---
```{r setup, include=FALSE}
library(reticulate)
use_python("C:/Users/Theo/Anaconda3/python.exe")
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
Discover the Python that will be used
```{r}
py_discover_config()
```
1-D Vectors and type in R
```{r }
a1<-c(1,3,4)
a1
typeof(a1)
```
```{python}
import numpy as np
a=np.array([1,3,4])
print(type(a))
```
A zero vector and array in R
```{r}
a0<-vector("numeric",7)
a0
a00<-array(0,dim=c(2,3))
a00
```
A zero array in Python
```{python}
import numpy as np
a=np.zeros((2,3))
print(a)
```
A sequence in R
```{r}
s=seq(1,10,2)
s
s1<-1:5
names(s1)<-c("cat","frog","dog","bird","ant")
s1
```
A sequence in Python
```{python}
import numpy as np
s=np.arange(1,10,2)
print(s)
s2=np.linspace(0,100,20)
print(s2)
```
Accesing an array in R
```{r}
#Note array indices in R start at 1
a<-c(5,4,2,2,5)
a[1]
```
Accesing an array in Python
```{python}
#NOTE HERE INDICES START AT 0
a=[23,34,49,12,11]
print(type(a))
print(a[0])
import numpy as np
b=np.array([12,23,19,34])
print(b[0])
```
2-D array and matrix creation in r
NOTE: THERE IS NO DIFFERENCE BETWEEN A 2-D ARRAY AND MATRIX IN R
```{r}
set.seed(3)
arr4<-array(c(1,2,4,9,10,3), dim=c(2,3))
arr4
arr1<-array(rnorm(6,12,3),dim=c(2,3))
mat11<-matrix(c(2,9,5,3,9,10,1,4,10),nrow=3)
dimnames(arr1)=list(c("dep1","dep2"),c("Tom","Jim","Sam"))
arr1
set.seed(3)
mat1<-matrix(rnorm(6,12,3), nrow=2)
dimnames(mat1)=list(c("dep1","dep2"),c("Tom","Jim","Sam"))
mat1
identical(arr1,mat1)
```
Now let's try 2-D arrays in Python
```{python}
import numpy as np
b=np.array([(3,2,1),(1,3,4)])
print(b)
```
Reshape a matrix in R
```{r}
library(pracma)
mat11<-matrix(c(2,9,5,3,9,10),nrow=3)
print(mat11)
mat2<-Reshape(mat11,2,3)
print(mat2)
```
Reshaping-copying-creating a view of an array in Python
```{python}
import numpy as np
x=np.arange(1,5)
print(x)
a=np.array([(10,20,30),(5,6,7)])
g=a.reshape(3,-1)
print(g)
#Create a copy
xx=x
xx is x
#Create a view
xv=x.view()
xv is x
```
Mathematical functions with arrays and matrices in R
```{r}
am=matrix(c(1,9,16,25),nrow=2)
ams=sqrt(am)
print(am[,1])
print(ams)
l=sum(am)
print(l)
mm=rowMeans(am)
print(mm)
msa=rowSums(am)
mc=colMeans(am)
mcs=colSums(am)
```
Mathematical functions with arrays in Python
```{python}
import numpy as np
a=np.array([(10,20,30),(5,6,7)])
c=np.sqrt(a)
s=a.sum()
max=a.max()
print(s)
print(max)
k=a.argmax()
sr=a.sum(axis=0)
mr=a.mean(axis=0)
print(a[...,0])
```
Matrix algebra in R
```{r}
mat1=matrix(c(3,2,1,1,3,4,10,30,20),nrow=3)
MT=t(mat1)
MI=solve(mat1)
#DOT PRODUCT
PR=mat1%*%MI
print(PR)
# Element by element multipl
C=mat1*MT
#Eigen values and eigenvectors
y=eigen(mat1)
#eigen values
print(y$val)
```
Matrix algebra in Python, which is implemented using numpy arrays
```{python}
import numpy as np
b=np.array([(3,2,1),(1,3,4),(10,30,20)])
c=b.T
d=np.linalg.inv(b)
print(c)
print(d)
e=b.dot(d)
print(e)
```
Sample in R
```{r}
set.seed(5)
sample(1:30, 6, replace=TRUE)
```
Sample in Python
```{python}
import numpy as np
import random
ar0=np.arange(1,10,1)
ar0list=ar0.tolist()
#WITH REPLACEMENT
random.seed(3)
print ("choosing 7 random items from a sequence with replacement ",random.choices(ar0list,k=7))
#WITHOUT REPLACEMENT
random.seed(2)
print ("choosing 7 random items from a sequence without replacement ",random.sample(ar0list,k=7))
```