Fisseha Berhane, PhD

Data Scientist

443-970-2353 fisseha@jhu.edu CV Resume Linkedin GitHub twitter twitter





Julia Vs R Comparison Cheat Sheet


Julia

R

In [60]:
# Row vector: size (1, n)

z = [1 2 3]    # or  [1; 2; 3]'  or   [1, 2, 3]'
Out[60]:
1×3 Array{Int64,2}:
 1  2  3
In [30]:
z = t(c(1, 2, 3))
z
dim(z)
123
  1. 1
  2. 3
In [61]:
# Column vector: size (n, 1)

z = [1 2 3]'
Out[61]:
3×1 Array{Int64,2}:
 1
 2
 3
In [31]:
z = cbind(c(1, 2, 3))
z
dim(z)
1
2
3
  1. 3
  2. 1
In [4]:
# Column vector: size (n, )
# 1d array

z = [1; 2; 3] # or [1, 2, 3]
Out[4]:
3-element Array{Int64,1}:
 1
 2
 3
In [32]:
z = array(c(1, 2, 3))
z
dim(z)
  1. 1
  2. 2
  3. 3
3
In [205]:
# Integers from A to C with step size B

z = collect(1:2:10)   # A = 1, B = 2, C = 10
             # collect() builds an array from range
Out[205]:
5-element Array{Int64,1}:
 1
 3
 5
 7
 9
In [39]:
z = seq(from = 1, to = 10, by = 2)
z
  1. 1
  2. 3
  3. 5
  4. 7
  5. 9
In [207]:
# To go down, use a negative step value

collect(10:-1:2)
Out[207]:
9-element Array{Int64,1}:
 10
  9
  8
  7
  6
  5
  4
  3
  2
In [40]:
z = seq(from = 10,to = 2, by = -1)
z
  1. 10
  2. 9
  3. 8
  4. 7
  5. 6
  6. 5
  7. 4
  8. 3
  9. 2
In [63]:
# Linearly spaced vector of n points

z = linspace(2, 20, 4)  # n = 4
Out[63]:
4-element LinSpace{Float64}:
 2.0,8.0,14.0,20.0
In [41]:
z = seq(from = 2, to = 20, length.out = 4)
z
  1. 2
  2. 8
  3. 14
  4. 20
In [64]:
# Create a matrix 

z = [1 2; 3 4]
z
Out[64]:
2×2 Array{Int64,2}:
 1  2
 3  4
In [43]:
z = matrix(c(1, 2, 3, 4), ncol = 2, byrow = TRUE)
z
12
34
In [65]:
# 3 x 3 matrix of zeros 

z = zeros(3,3)
Out[65]:
3×3 Array{Float64,2}:
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0
In [49]:
z = matrix(0, 3, 3)
z
000
000
000
In [66]:
# 3 x 3 matrix of ones

z = ones(3,3)
Out[66]:
3×3 Array{Float64,2}:
 1.0  1.0  1.0
 1.0  1.0  1.0
 1.0  1.0  1.0
In [50]:
z = matrix(1, 3, 3)
z
111
111
111
In [215]:
# 3 x 3 identity matrix

z = eye(3) # or eye(3,3)
Out[215]:
3×3 Array{Float64,2}:
 1.0  0.0  0.0
 0.0  1.0  0.0
 0.0  0.0  1.0
In [51]:
z = diag(3)
z
100
010
001
In [109]:
z = falses(3, 4)
Out[109]:
3×4 BitArray{2}:
 false  false  false  false
 false  false  false  false
 false  false  false  false
In [55]:
z = matrix(FALSE, 3,4)
z
FALSEFALSEFALSEFALSE
FALSEFALSEFALSEFALSE
FALSEFALSEFALSEFALSE
In [108]:
z = trues(3, 4)
Out[108]:
3×4 BitArray{2}:
 true  true  true  true
 true  true  true  true
 true  true  true  true
In [56]:
z = matrix(TRUE, 3,4)
z
TRUETRUETRUETRUE
TRUETRUETRUETRUE
TRUETRUETRUETRUE
In [68]:
# Diagonal matrix

z = diagm([3, 4, 5])  # or diagm([3; 4; 5])
Out[68]:
3×3 Array{Int64,2}:
 3  0  0
 0  4  0
 0  0  5
In [7]:
z = diag(c(3,4,5))
z
300
040
005
In [73]:
# Uniform random numbers
z = rand(5,2)
Out[73]:
5×2 Array{Float64,2}:
 0.868183   0.40017 
 0.0193721  0.631149
 0.4029     0.053243
 0.209878   0.116046
 0.281436   0.811648
In [11]:
z = matrix(runif(10), ncol = 2)
z
0.4983519 0.10833300
0.1025173 0.03692186
0.7420357 0.57281816
0.7045229 0.44854733
0.5542345 0.52285643
In [72]:
# Normal random numbers

z = randn(5,2)
Out[72]:
5×2 Array{Float64,2}:
 0.0453683   1.03122 
 0.765221   -0.044198
 1.64055    -1.63661 
 0.0664202   0.245565
 1.33607     2.02564 
In [12]:
z = matrix(rnorm(10), ncol = 2)
z
0.074858710.6024924
1.126477151.7665468
-0.207954600.3756044
1.067669870.9955586
0.913355490.2773119
In [76]:
# Transpose
z = [1,2,3]
z.'    # or transpose(z)
Out[76]:
1×3 Array{Int64,2}:
 1  2  3
In [27]:
z = matrix(1:12, ncol = 3) # R is column-major
t(z)
1 2 3 4
5 6 7 8
9 101112
In [81]:
# Concatenate horizontally
z1 = [1, 2, 3]
z2 = [4, 5, 6]
[z1 z2]   # or hcat(z1,z2)
Out[81]:
3×2 Array{Int64,2}:
 1  4
 2  5
 3  6
In [41]:
z1  = c(1, 2, 3)
z2 = c(4, 5, 6)
cbind(z1, z2)
z1z2
14
25
36
In [83]:
# Concatenate vertically
z1 = [1 2 3]
z2 = [4 5 6]
[z1, z2]   # or vcat(z1,z2)
Out[83]:
2-element Array{Array{Int64,2},1}:
 [1 2 3]
 [4 5 6]
In [39]:
z1  = c(1, 2, 3)
z2 = c(4, 5, 6)
rbind(z1, z2)
z1123
z2456
In [86]:
# Reshape
z1 = 1:20
z2 = reshape(z1, 4,5)
Out[86]:
4×5 Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}:
 1  5   9  13  17
 2  6  10  14  18
 3  7  11  15  19
 4  8  12  16  20
In [43]:
z1 = 1:20
z2 = matrix(z1, ncol = 5)
z2
1 5 91317
2 6 101418
3 7 111519
4 8 121620
In [200]:
# Convert matrix to vector
z1 = [1 2 3; 4 5 6]
z = z1[:]  # or vec(z1)
           # Julia is column-major like R
Out[200]:
6-element Array{Int64,1}:
 1
 4
 2
 5
 3
 6
In [47]:
z1 = matrix(c(1, 2, 3, 4, 5, 6), ncol = 3, byrow = TRUE)
z1
as.vector(z1)
123
456
  1. 1
  2. 4
  3. 2
  4. 5
  5. 3
  6. 6
In [89]:
# Flip left/right

z = reshape(1:9,3,3)

flipdim(z, 2)
Out[89]:
3×3 Array{Int64,2}:
 7  4  1
 8  5  2
 9  6  3
In [59]:
z = matrix(1:9, ncol = 3)
z

z[ ,ncol(z):1]
147
258
369
741
852
963
In [90]:
# Flip up/down

z = reshape(1:9,3,3)

flipdim(z, 1)
Out[90]:
3×3 Array{Int64,2}:
 3  6  9
 2  5  8
 1  4  7
In [60]:
z = matrix(1:9, ncol = 3)
z
z[nrow(z):1, ]
147
258
369
369
258
147
In [112]:
# Access one element: Julia uses one-based indexing

z = reshape(range(1,20), 4,5)
z[1,1]
Out[112]:
1
In [71]:
z = matrix(1:20, ncol = 5)
z[1,1]   # R uses one-based indexing
1
In [216]:
z = fill("Julia", 3,3)
Out[216]:
3×3 Array{String,2}:
 "Julia"  "Julia"  "Julia"
 "Julia"  "Julia"  "Julia"
 "Julia"  "Julia"  "Julia"
In [72]:
z = matrix(rep("R", 9), ncol = 3)
z
RRR
RRR
RRR
In [98]:
# Access specific rows

zc = z[1:2,:] # rows 1 and 2; all columns
Out[98]:
2×5 Array{Int64,2}:
 1  5   9  13  17
 2  6  10  14  18
In [79]:
z = matrix(1:20, ncol = 5)
z[1:2, ]
1 5 91317
2 6 101418
In [99]:
# Access specific columns

zr = z[:,3:4] # columns 3 and 4; all rows
Out[99]:
4×2 Array{Int64,2}:
  9  13
 10  14
 11  15
 12  16
In [80]:
z[, 3:4]
913
1014
1115
1216
In [132]:
z = reshape(1:12, 3,4)
z[end - 1:end, end - 1: end]
Out[132]:
2×2 Array{Int64,2}:
 8  11
 9  12
In [84]:
z = matrix(1:12, ncol = 4)
z[(nrow(z)-1):nrow(z), (ncol(z)-1):ncol(z)]
8 11
9 12
In [133]:
# Get dimensions of an array

z = reshape(1:24, 3,4,2)

size(z)
Out[133]:
(3,4,2)
In [85]:
z = array(1:24, c(3, 4, 2))
dim(z)
  1. 3
  2. 4
  3. 2
In [135]:
# Diagonals of a matrix 

z = reshape(1:20, 4,5)

diag(z)
Out[135]:
4-element Array{Int64,1}:
  1
  6
 11
 16
In [86]:
z = matrix(1:20, nrow = 4)
diag(z)
  1. 1
  2. 6
  3. 11
  4. 16
In [165]:
# Dot product

z1 = 1:10
z2 = 1:10
dot(z1,z2)  # or z1 ⋅ z2
Out[165]:
385
In [91]:
z1 = 1:10
z2 = 1:10
z1 %*% z2
385
In [147]:
# Matrix multiplication

z1 = reshape(1:9,3,3)
z2 = reshape(1:6, 3,2)

z1 * z2
Out[147]:
3×2 Array{Int64,2}:
 30  66
 36  81
 42  96
In [1]:
z1 = matrix(1:9, ncol = 3)
z2 = matrix(1:6, ncol = 2)
z1 %*% z2
3066
3681
4296
In [148]:
# Element-wise multiplication

z1 = reshape(1:9,3,3)
z2 = reshape(1:9, 3,3)

z1 .* z2
Out[148]:
3×3 Array{Int64,2}:
 1  16  49
 4  25  64
 9  36  81
In [3]:
z1 = matrix(1:9, ncol = 3)
z2 = matrix(1:9, nrow = 3)
z1*z2
1 1649
4 2564
9 3681
In [184]:
# Matrix to a power

z = reshape(range(1,16), 4,4)
z^2
Out[184]:
4×4 Array{Int64,2}:
  90  202  314  426
 100  228  356  484
 110  254  398  542
 120  280  440  600
In [5]:
z = matrix(1:16, ncol = 4)
z %*% z
90202314426
100228356484
110254398542
120280440600
In [180]:
# Matrix to a power, elementwise

z.^2
Out[180]:
4×4 Array{Int64,2}:
  1  25   81  169
  4  36  100  196
  9  49  121  225
 16  64  144  256
In [6]:
z^2
1 25 81169
4 36 100196
9 49 121225
16 64 144256
In [431]:
# Inverse of a matrix

z = rand(2:10, 3, 3)
inv(z)
Out[431]:
3×3 Array{Float64,2}:
 -0.042654    -0.146919    0.180095 
  0.127962    -0.0592417  -0.0402844
 -0.00947867   0.189573   -0.07109  
In [106]:
z = matrix(sample(2:10, 9), ncol = 3)
solve(z)
-0.055084746 0.18220339 -0.01271186
-0.008474576-0.27966102 0.22881356
0.152542373 0.03389831 -0.11864407
In [427]:
# Determinant

z = rand(2:10, 3, 3)
det(z)
Out[427]:
83.99999999999997
In [107]:
det(z)
236
In [199]:
# Eigenvalues and eigenvectors

val, vec = eig(z.^2);

println("val:")
println(val)

println("vec:")
println(vec)
val:
[433.366,-20.5172,-1.08611e-14,1.15167]
vec:
[0.368137 0.769875 0.223607 -0.863576; 0.443934 0.543062 -0.67082 0.194401; 0.528794 0.195717 0.67082 0.407338; 0.622716 -0.272161 -0.223607 -0.224766]
In [110]:
eigen(z)
$values
  1. 18.4807860176326+0i
  2. -3.2403930088163+1.50660969276982i
  3. -3.2403930088163-1.50660969276982i
$vectors
0.6352322+0i -0.5024969+0.2749683i-0.5024969-0.2749683i
0.4256570+0i 0.7304165+0.0000000i 0.7304165+0.0000000i
0.6444347+0i 0.0641018-0.3664314i 0.0641018+0.3664314i
In [225]:
# comparison

A = [1 2 3 4]
B = [3 2 3 6]
A == B
Out[225]:
false
In [113]:
A = c(1, 2, 3, 4)
B = c(3, 2, 3, 6)
identical(A, B)
FALSE
In [226]:
# Elementwise comparison

A = [1 2 3 4]
B = [3 2 3 6]
A .== B
Out[226]:
1×4 BitArray{2}:
 false  true  true  false
In [114]:
A = c(1, 2, 3, 4)
B = c(3, 2, 3, 6)
A == B
  1. FALSE
  2. TRUE
  3. TRUE
  4. FALSE
In [227]:
z = [3 4 5; 6 7 9; 15 -20 25]
maximum(z)      # of entire array
Out[227]:
25
In [7]:
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)

max(z)
25
In [228]:
z = [3 4 5; 6 7 9; 15 -20 25]
minimum(z)    # of entire array
Out[228]:
-20
In [8]:
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)

min(z)
-20
In [277]:
z = [3 4 5; 6 7 9; 15 -20 25]
sum(z)     # of entire array
Out[277]:
54
In [9]:
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)

sum(z)
54
In [273]:
z = [3 4 5; 6 7 9; 15 -20 25]
maximum(z, 1)    # of each column
Out[273]:
1×3 Array{Int64,2}:
 15  7  25
In [11]:
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)

apply(z, 2, max)
  1. 15
  2. 7
  3. 25
In [274]:
z = [3 4 5; 6 7 9; 15 -20 25]
maximum(z, 2)    # of each row
Out[274]:
3×1 Array{Int64,2}:
  5
  9
 25
In [12]:
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)

apply(z, 1, max)
  1. 5
  2. 9
  3. 25
In [275]:
z = [3 4 5; 6 7 9; 15 -20 25]
minimum(z, 1)  # of each column
Out[275]:
1×3 Array{Int64,2}:
 3  -20  5
In [13]:
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)

apply(z, 2, min)
  1. 3
  2. -20
  3. 5
In [276]:
z = [3 4 5; 6 7 9; 15 -20 25]
minimum(z,  2)  # of each row
Out[276]:
3×1 Array{Int64,2}:
   3
   6
 -20
In [14]:
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)

apply(z, 1, min)
  1. 3
  2. 6
  3. -20
In [278]:
z = [3 4 5; 6 7 9; 15 -20 25]
sum(z, 1)     # of each column
Out[278]:
1×3 Array{Int64,2}:
 24  -9  39
In [15]:
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)

colSums(z)  # or apply(z, 2, sum)
  1. 24
  2. -9
  3. 39
In [279]:
z = [3 4 5; 6 7 9; 15 -20 25]
sum(z, 2)     # of each row
Out[279]:
3×1 Array{Int64,2}:
 12
 22
 20
In [16]:
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)

rowSums(z)  # or apply(z, 1, sum)
  1. 12
  2. 22
  3. 20
In [280]:
z = [3 4 5; 6 7 9; 15 -20 25]
cumsum(z, 1)     # Cumulative sum by column
Out[280]:
3×3 Array{Int64,2}:
  3   4   5
  9  11  14
 24  -9  39
In [17]:
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)

apply(z, 2, cumsum)
3 4 5
91114
24-939
In [282]:
z = [3 4 5; 6 7 9; 15 -20 25]
cumsum(z, 2)     # Cumulative sum by row
Out[282]:
3×3 Array{Int64,2}:
  3   7  12
  6  13  22
 15  -5  20
In [18]:
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)

t(apply(z, 1, cumsum))
3 712
61322
15-520
In [434]:
# Compare two (or more) arrays element by element, returning a new array with the largest values from each:
z1 = [3 4 6 90 12 -40]
z2 = [5 7 10 -20 40 -10]
max(z1,z2)
Out[434]:
1×6 Array{Int64,2}:
 5  7  10  90  40  -10
In [19]:
z1 = c(3, 4, 6, 90, 12, -40)
z2 = c(5, 7, 10, -20, 40, -10)

pmax(z1,z2)
  1. 5
  2. 7
  3. 10
  4. 90
  5. 40
  6. -10
In [435]:
# Compare two (or more) arrays element by element, returning a new array with the largest values from each:
z1 = [3 4 6 90 12 -40]
z2 = [5 7 10 -20 40 -10]
min(z1,z2)
Out[435]:
1×6 Array{Int64,2}:
 3  4  6  -20  12  -40
In [20]:
z1 = c(3, 4, 6, 90, 12, -40)
z2 = c(5, 7, 10, -20, 40, -10)

pmin(z1,z2)
  1. 3
  2. 4
  3. 6
  4. -20
  5. 12
  6. -40
In [417]:
# Setting the contents of arrays

a = rand(0:10, 10, 10);
a[a .== 0] = -100
a
Out[417]:
10×10 Array{Int64,2}:
 -100     7   3     4  -100   6  -100     9  4     9
    9     2   9     3     2   8     3     9  2     5
    6     1  10     8     3   9     2     5  3     1
    2  -100   2     8     6   4     3     8  4     5
   10     1   1  -100     3   3     7    10  8     7
    4     2   2     9     1   5     5     2  5     7
    6     2   2     9     2   3  -100  -100  7     6
   10     1   9  -100     5   6     6    10  6    10
   10     7   5     6     5   3     8     6  1     6
    4     2   6     6  -100  10     4  -100  1  -100
In [21]:
a = matrix(sample(0:10, 100,replace = TRUE),ncol = 10)

a[a == 0] =-100
a
8 10 10 2 10 3 8 23 2
2 8 1 2 8-100 4-1004 8
10-100 10-100 4 3 6 62 1
-100 5 3 4 6 2-100 92 7
9 2 10 1 2 6 6 109 9
3 10-100 8 6 10 2 27 3
5 1 1 2 3 4 2 31 6
7 4 6-100 9 7 9 69 4
7 1 10-100-100 9 4 108 7
5 5 6 8-100 6 6 109 6
In [380]:
# remove element/s

a = collect(1:10)
splice!(a,1:5)
a
Out[380]:
5-element Array{Int64,1}:
  6
  7
  8
  9
 10
In [152]:
a = 1:10
a[-(1:5)]
  1. 6
  2. 7
  3. 8
  4. 9
  5. 10
In [387]:
# concatenate
a = collect(1:10)

push!(a, 20) # at the end
             # or [a;20]
Out[387]:
11-element Array{Int64,1}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 20
In [153]:
a = 1:10
c(a, 20)
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 20
In [389]:
# concatenate
a = collect(1:10)

unshift!(a, 20) # at the beginning
                # or [20; a]
Out[389]:
11-element Array{Int64,1}:
 20
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
In [154]:
a = 1:10
c(20,a)
  1. 20
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 9
  11. 10
In [238]:
# check if a value is in an array

z = 1:10

9 in z
Out[238]:
true
In [155]:
z = 1:10
9 %in% z
TRUE
In [403]:
# insert to specific locations

a = [1, 3, 4, 5]
splice!(a, 2, 2:3)
a
Out[403]:
5-element Array{Int64,1}:
 1
 2
 3
 4
 5
In [160]:
a = c(1, 3, 4, 5)
c(a[1], 2:3, a[3:length(a)])
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
In [406]:
# remove last element

z = collect(1:10)
pop!(z)
z
Out[406]:
9-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6
 7
 8
 9
In [161]:
z = 1:10
z[-length(z)]
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
In [407]:
# remove first element

z = collect(1:10)
shift!(z)
z
Out[407]:
9-element Array{Int64,1}:
  2
  3
  4
  5
  6
  7
  8
  9
 10
In [162]:
z = 1:10
z[-1]
  1. 2
  2. 3
  3. 4
  4. 5
  5. 6
  6. 7
  7. 8
  8. 9
  9. 10
In [416]:
# delete by indices

z = collect(1:10)
deleteat!(z, [1 4 6])
Out[416]:
7-element Array{Int64,1}:
  2
  3
  5
  7
  8
  9
 10
In [163]:
z = 1:10
z[-c(1,4,6)]
  1. 2
  2. 3
  3. 5
  4. 7
  5. 8
  6. 9
  7. 10
In [321]:
y = [ 2 3 5; -6 3 8; -9 3 11]
find(isodd, y)  # gives indices of odd numbers
Out[321]:
6-element Array{Int64,1}:
 3
 4
 5
 6
 7
 9
In [22]:
y = matrix(c(2, 3, 5, -6, 3, 8, -9, 3, 11),
           ncol = 3, byrow = TRUE)

which(y%%2 !=0)
  1. 3
  2. 4
  3. 5
  4. 6
  5. 7
  6. 9
In [261]:
# Returns the row and column count of the array

x = randn(12,15,16);
size(x)
Out[261]:
(12,15,16)
In [182]:
x = array(rnorm(12*15*16),c(12, 15, 16))
dim(x)
  1. 12
  2. 15
  3. 16
In [262]:
# How many elements the array contains

x = randn(12,15,16);
length(x)
Out[262]:
2880
In [183]:
x = array(rnorm(12*15*16),c(12, 15, 16))
length(x)
2880
In [263]:
# How many non-zero

x = [1 2 0 ; 4 5 0]
countnz(x)
Out[263]:
4
In [185]:
x = matrix(c(1, 2, 0, 4, 5, 0), ncol =3, byrow = TRUE)
length(x[x !=0])
4
In [268]:
union(1:6,4:10)  # removes duplicates
Out[268]:
10-element Array{Int64,1}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
In [186]:
union(1:6, 4:10)
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
In [270]:
# Intersection of two or more arrays

intersect(1:10,7:15)
Out[270]:
7:10
In [187]:
intersect(1:10, 7:15)
  1. 7
  2. 8
  3. 9
  4. 10
In [271]:
# Elements that are in the first array but not the second
setdiff(1:10,7:15)
Out[271]:
6-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6
In [188]:
setdiff(1:10,7:15)
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
In [272]:
# Elements that are in the first array but not the second

setdiff(7:15,1:10)
Out[272]:
5-element Array{Int64,1}:
 11
 12
 13
 14
 15
In [189]:
setdiff(7:15, 1:10)
  1. 11
  2. 12
  3. 13
  4. 14
  5. 15
In [336]:
y = [ 2 3 5; -6 3 8; -9 3 20]
filter(iseven,y)  # gives the evenones
Out[336]:
4-element Array{Int64,1}:
  2
 -6
  8
 20
In [196]:
y = matrix(c(2, 3, 5, -6, 3, 8,-9, 3, 20
             ),ncol = 3, byrow = TRUE)
y[y%%2 ==0]
  1. 2
  2. -6
  3. 8
  4. 20
In [337]:
# Count the number of elements that satisfy the condition

count(isodd, 1:5000)
Out[337]:
2500
In [199]:
y = 1:5000
length(y[y%%2 != 0])
2500
In [339]:
# check if any of the elements satisfy the condition

any([1 2 3 10 12] .== [3 4 5 10 25])
Out[339]:
true
In [200]:
any(c(1, 2, 3, 10, 12)==c(3, 4, 5, 10, 25))
TRUE
In [340]:
# check if all the elements satisfy the condition

all([1 2 3 10 12] .== [3 4 5 10 25])
Out[340]:
false
In [201]:
all(c(1, 2, 3, 10, 12)==c(3, 4, 5, 10, 25))
FALSE
In [346]:
# Random element from an array
z = range(1, 3, 20)
z[rand(1:end)]
Out[346]:
55
In [268]:
z = seq(from = 1, by = 3, length.out = 20)
sample(z, 1)
55
In [351]:
# Find the extreme values of an array:

z = 1:2:200
extrema(z)
Out[351]:
(1,199)
In [269]:
z = seq(from = 1, to = 200, by =2)
range(z)
  1. 1
  2. 199
In [353]:
z = [1 2 3; 4 5 6; 7 8 9]
extrema(z, 1)
Out[353]:
1×3 Array{Tuple{Int64,Int64},2}:
 (1,7)  (2,8)  (3,9)
In [271]:
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), 
           ncol = 3, byrow = TRUE)
apply(z, 2, range)
123
789
In [354]:
z = [1 2 3; 4 5 6; 7 8 9]
extrema(z, 2)
Out[354]:
3×1 Array{Tuple{Int64,Int64},2}:
 (1,3)
 (4,6)
 (7,9)
In [273]:
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), 
           ncol = 3, byrow = TRUE)
t(apply(z, 1, range))
13
46
79
In [365]:
# find product
z = [1 2 3; 4 5 6; 7 8 9]
prod(z)
Out[365]:
362880
In [274]:
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), 
           ncol= 3, byrow = TRUE)
prod(z)
362880
In [366]:
# find product
z = [1 2 3; 4 5 6; 7 8 9]
prod(z, 1)  # for each column
Out[366]:
1×3 Array{Int64,2}:
 28  80  162
In [275]:
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), 
           ncol= 3, byrow = TRUE)
apply(z, 2, prod)
  1. 28
  2. 80
  3. 162
In [368]:
# find product
z = [1 2 3; 4 5 6; 7 8 9]
prod(z, 2)  # for each row
Out[368]:
3×1 Array{Int64,2}:
   6
 120
 504
In [277]:
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), 
           ncol= 3, byrow = TRUE)
apply(z, 1, prod)
  1. 6
  2. 120
  3. 504
In [369]:
# find mean
z = [1 2 3; 4 5 6; 7 8 9]
mean(z)
Out[369]:
5.0
In [279]:
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), 
           ncol= 3, byrow = TRUE)
mean(z)
5
In [370]:
# find mean
z = [1 2 3; 4 5 6; 7 8 9]
mean(z, 1)  # for each column
Out[370]:
1×3 Array{Float64,2}:
 4.0  5.0  6.0
In [280]:
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), 
           ncol= 3, byrow = TRUE)
apply(z, 2, mean)
  1. 4
  2. 5
  3. 6
In [371]:
# find mean
z = [1 2 3; 4 5 6; 7 8 9]
mean(z, 2)  # for each row
Out[371]:
3×1 Array{Float64,2}:
 2.0
 5.0
 8.0
In [281]:
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), 
           ncol= 3, byrow = TRUE)
apply(z, 1, mean)
  1. 2
  2. 5
  3. 8
In [372]:
# find median
z = [1 2 3; 4 5 6; 7 8 9]
middle(z)
Out[372]:
5.0
In [284]:
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), 
           ncol= 3, byrow = TRUE)
median(z)
5
In [373]:
# find median
z = [1 2 3; 4 5 6; 7 8 9]
mean(z, 1)  # for each column
Out[373]:
1×3 Array{Float64,2}:
 4.0  5.0  6.0
In [286]:
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), 
           ncol= 3, byrow = TRUE)
apply(z, 2, median)
  1. 4
  2. 5
  3. 6
In [291]:
# for loop
z = Int64[]
for i = 1:10
    z = [z; i^2];
end
z
Out[291]:
10-element Array{Int64,1}:
   1
   4
   9
  16
  25
  36
  49
  64
  81
 100
In [290]:
z = c()
for(i in 1:10){
    z = c(z, i^2)
}

z
  1. 1
  2. 4
  3. 9
  4. 16
  5. 25
  6. 36
  7. 49
  8. 64
  9. 81
  10. 100
In [292]:
# if condition

evens = Int64[]
for i = 1:20
    if i%2 ==0
        evens = [evens; i]
    end
end
evens
Out[292]:
10-element Array{Int64,1}:
  2
  4
  6
  8
 10
 12
 14
 16
 18
 20
In [291]:
evens = c()
for(i in 1:20){
    if(i%%2 ==0){
        evens = c(evens, i)
    }
}

evens
  1. 2
  2. 4
  3. 6
  4. 8
  5. 10
  6. 12
  7. 14
  8. 16
  9. 18
  10. 20
In [295]:
# while loop and if/else
i = 1
while i <= 20
    if i%2 ==0
        println(i," is even")
    else 
        println(i," is odd")
    end
    i = i+1
end
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
11 is odd
12 is even
13 is odd
14 is even
15 is odd
16 is even
17 is odd
18 is even
19 is odd
20 is even
In [4]:
i = 1
while(i <= 20){
    if(i%%2 == 0){
        cat(paste(i, "is even\n"))
    }else{
        cat(paste(i, "is odd\n"))
    }
    
    i = i + 1
}
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
11 is odd
12 is even
13 is odd
14 is even
15 is odd
16 is even
17 is odd
18 is even
19 is odd
20 is even
In [2]:
# sinle line function

f(x) = 9/5*x + 32

f(-40)
WARNING: Method definition f(
Out[2]:
-40.0
Any) in module Main at In[1]:4 overwritten at In[2]:3.
In [1]:
f  <- function(x) 9/5*x + 32

f(-40)
-40
In [1]:
# Multiple line function

function f(x)
    if x%2 ==0
        return("Yor number is even")
    else
         return(" Yor number is odd")
    end
end

f(-40)
Out[1]:
"Yor number is even"
In [3]:
f  <- function(x){
    if(x%%2 ==0){
        cat(" Your number is even")
    }else{
        cat("your number is odd")
    }
}

f(-40)
 Your number is even
In [448]:
a = [1 2 3;-4 -5 6; 7 2 -9]
sort(a, 1)  # sort along columns
            # we can also use sortcolumns()
Out[448]:
3×3 Array{Int64,2}:
 -4  -5  -9
  1   2   3
  7   2   6
In [6]:
a = matrix(c(1, 2, 3, -4, -5, 6, 7, 2, -9), 
           ncol = 3, byrow = TRUE) 
apply(a, 2, sort)
-4-5-9
1 2 3
7 2 6
In [453]:
a = [1 2 3;-4 -5 6; 7 2 -9]
sort(a, 2)  # sort along rows
            # we can also use sortrows()
Out[453]:
3×3 Array{Int64,2}:
  1   2  3
 -5  -4  6
 -9   2  7
In [8]:
a = matrix(c(1, 2, 3, -4, -5, 6, 7, 2, -9), 
           ncol = 3, byrow = TRUE) 
t(apply(a, 1, sort))
1 23
-5-46
-9 27
In [452]:
a = [1 2 3;-4 -5 6; 7 2 -9]
sort(a, 1, rev = true)  # decreasing order
Out[452]:
3×3 Array{Int64,2}:
  7   2   6
  1   2   3
 -4  -5  -9
In [10]:
a = matrix(c(1, 2, 3, -4, -5, 6, 7, 2, -9), 
           ncol = 3, byrow = TRUE) 
apply(a, 2, sort, decreasing = TRUE)
7 2 6
1 2 3
-4-5-9
comments powered by Disqus