# Fisseha Berhane, PhD

#### Data Scientist 443-970-2353 [email protected] CV Resume    ## Julia Vs R Comparison Cheat Sheet

### R¶

In :
# Row vector: size (1, n)

z = [1 2 3]    # or  [1; 2; 3]'  or   [1, 2, 3]'

Out:
1×3 Array{Int64,2}:
1  2  3
In :
z = t(c(1, 2, 3))
z
dim(z)

 1 2 3
1. 1
2. 3
In :
# Column vector: size (n, 1)

z = [1 2 3]'

Out:
3×1 Array{Int64,2}:
1
2
3
In :
z = cbind(c(1, 2, 3))
z
dim(z)

 1 2 3
1. 3
2. 1
In :
# Column vector: size (n, )
# 1d array

z = [1; 2; 3] # or [1, 2, 3]

Out:
3-element Array{Int64,1}:
1
2
3
In :
z = array(c(1, 2, 3))
z
dim(z)

1. 1
2. 2
3. 3
3
In :
# 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:
5-element Array{Int64,1}:
1
3
5
7
9
In :
z = seq(from = 1, to = 10, by = 2)
z

1. 1
2. 3
3. 5
4. 7
5. 9
In :
# To go down, use a negative step value

collect(10:-1:2)

Out:
9-element Array{Int64,1}:
10
9
8
7
6
5
4
3
2
In :
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 :
# Linearly spaced vector of n points

z = linspace(2, 20, 4)  # n = 4

Out:
4-element LinSpace{Float64}:
2.0,8.0,14.0,20.0
In :
z = seq(from = 2, to = 20, length.out = 4)
z

1. 2
2. 8
3. 14
4. 20
In :
# Create a matrix

z = [1 2; 3 4]
z

Out:
2×2 Array{Int64,2}:
1  2
3  4
In :
z = matrix(c(1, 2, 3, 4), ncol = 2, byrow = TRUE)
z

 1 2 3 4
In :
# 3 x 3 matrix of zeros

z = zeros(3,3)

Out:
3×3 Array{Float64,2}:
0.0  0.0  0.0
0.0  0.0  0.0
0.0  0.0  0.0
In :
z = matrix(0, 3, 3)
z

 0 0 0 0 0 0 0 0 0
In :
# 3 x 3 matrix of ones

z = ones(3,3)

Out:
3×3 Array{Float64,2}:
1.0  1.0  1.0
1.0  1.0  1.0
1.0  1.0  1.0
In :
z = matrix(1, 3, 3)
z

 1 1 1 1 1 1 1 1 1
In :
# 3 x 3 identity matrix

z = eye(3) # or eye(3,3)

Out:
3×3 Array{Float64,2}:
1.0  0.0  0.0
0.0  1.0  0.0
0.0  0.0  1.0
In :
z = diag(3)
z

 1 0 0 0 1 0 0 0 1
In :
z = falses(3, 4)

Out:
3×4 BitArray{2}:
false  false  false  false
false  false  false  false
false  false  false  false
In :
z = matrix(FALSE, 3,4)
z

 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
In :
z = trues(3, 4)

Out:
3×4 BitArray{2}:
true  true  true  true
true  true  true  true
true  true  true  true
In :
z = matrix(TRUE, 3,4)
z

 TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
In :
# Diagonal matrix

z = diagm([3, 4, 5])  # or diagm([3; 4; 5])

Out:
3×3 Array{Int64,2}:
3  0  0
0  4  0
0  0  5
In :
z = diag(c(3,4,5))
z

 3 0 0 0 4 0 0 0 5
In :
# Uniform random numbers
z = rand(5,2)

Out:
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 :
z = matrix(runif(10), ncol = 2)
z

 0.498352 0.108333 0.102517 0.0369219 0.742036 0.572818 0.704523 0.448547 0.554234 0.522856
In :
# Normal random numbers

z = randn(5,2)

Out:
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 :
z = matrix(rnorm(10), ncol = 2)
z

 0.0748587 0.602492 1.12648 1.76655 -0.207955 0.375604 1.06767 0.995559 0.913355 0.277312
In :
# Transpose
z = [1,2,3]
z.'    # or transpose(z)

Out:
1×3 Array{Int64,2}:
1  2  3
In :
z = matrix(1:12, ncol = 3) # R is column-major
t(z)

 1 2 3 4 5 6 7 8 9 10 11 12
In :
# Concatenate horizontally
z1 = [1, 2, 3]
z2 = [4, 5, 6]
[z1 z2]   # or hcat(z1,z2)

Out:
3×2 Array{Int64,2}:
1  4
2  5
3  6
In :
z1  = c(1, 2, 3)
z2 = c(4, 5, 6)
cbind(z1, z2)

z1z2
14
25
36
In :
# Concatenate vertically
z1 = [1 2 3]
z2 = [4 5 6]
[z1, z2]   # or vcat(z1,z2)

Out:
2-element Array{Array{Int64,2},1}:
[1 2 3]
[4 5 6]
In :
z1  = c(1, 2, 3)
z2 = c(4, 5, 6)
rbind(z1, z2)

 z1 z2 1 2 3 4 5 6
In :
# Reshape
z1 = 1:20
z2 = reshape(z1, 4,5)

Out:
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 :
z1 = 1:20
z2 = matrix(z1, ncol = 5)
z2

 1 5 9 13 17 2 6 10 14 18 3 7 11 15 19 4 8 12 16 20
In :
# Convert matrix to vector
z1 = [1 2 3; 4 5 6]
z = z1[:]  # or vec(z1)
# Julia is column-major like R

Out:
6-element Array{Int64,1}:
1
4
2
5
3
6
In :
z1 = matrix(c(1, 2, 3, 4, 5, 6), ncol = 3, byrow = TRUE)
z1
as.vector(z1)

 1 2 3 4 5 6
1. 1
2. 4
3. 2
4. 5
5. 3
6. 6
In :
# Flip left/right

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

flipdim(z, 2)

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

z[ ,ncol(z):1]

 1 4 7 2 5 8 3 6 9
 7 4 1 8 5 2 9 6 3
In :
# Flip up/down

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

flipdim(z, 1)

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

 1 4 7 2 5 8 3 6 9
 3 6 9 2 5 8 1 4 7
In :
# Access one element: Julia uses one-based indexing

z = reshape(range(1,20), 4,5)
z[1,1]

Out:
1
In :
z = matrix(1:20, ncol = 5)
z[1,1]   # R uses one-based indexing

1
In :
z = fill("Julia", 3,3)

Out:
3×3 Array{String,2}:
"Julia"  "Julia"  "Julia"
"Julia"  "Julia"  "Julia"
"Julia"  "Julia"  "Julia"
In :
z = matrix(rep("R", 9), ncol = 3)
z

 R R R R R R R R R
In :
# Access specific rows

zc = z[1:2,:] # rows 1 and 2; all columns

Out:
2×5 Array{Int64,2}:
1  5   9  13  17
2  6  10  14  18
In :
z = matrix(1:20, ncol = 5)
z[1:2, ]

 1 5 9 13 17 2 6 10 14 18
In :
# Access specific columns

zr = z[:,3:4] # columns 3 and 4; all rows

Out:
4×2 Array{Int64,2}:
9  13
10  14
11  15
12  16
In :
z[, 3:4]

 9 13 10 14 11 15 12 16
In :
z = reshape(1:12, 3,4)
z[end - 1:end, end - 1: end]

Out:
2×2 Array{Int64,2}:
8  11
9  12
In :
z = matrix(1:12, ncol = 4)
z[(nrow(z)-1):nrow(z), (ncol(z)-1):ncol(z)]

 8 11 9 12
In :
# Get dimensions of an array

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

size(z)

Out:
(3,4,2)
In :
z = array(1:24, c(3, 4, 2))
dim(z)

1. 3
2. 4
3. 2
In :
# Diagonals of a matrix

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

diag(z)

Out:
4-element Array{Int64,1}:
1
6
11
16
In :
z = matrix(1:20, nrow = 4)
diag(z)

1. 1
2. 6
3. 11
4. 16
In :
# Dot product

z1 = 1:10
z2 = 1:10
dot(z1,z2)  # or z1 ⋅ z2

Out:
385
In :
z1 = 1:10
z2 = 1:10
z1 %*% z2

 385
In :
# Matrix multiplication

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

z1 * z2

Out:
3×2 Array{Int64,2}:
30  66
36  81
42  96
In :
z1 = matrix(1:9, ncol = 3)
z2 = matrix(1:6, ncol = 2)
z1 %*% z2

 30 66 36 81 42 96
In :
# Element-wise multiplication

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

z1 .* z2

Out:
3×3 Array{Int64,2}:
1  16  49
4  25  64
9  36  81
In :
z1 = matrix(1:9, ncol = 3)
z2 = matrix(1:9, nrow = 3)
z1*z2

 1 16 49 4 25 64 9 36 81
In :
# Matrix to a power

z = reshape(range(1,16), 4,4)
z^2

Out:
4×4 Array{Int64,2}:
90  202  314  426
100  228  356  484
110  254  398  542
120  280  440  600
In :
z = matrix(1:16, ncol = 4)
z %*% z

 90 202 314 426 100 228 356 484 110 254 398 542 120 280 440 600
In :
# Matrix to a power, elementwise

z.^2

Out:
4×4 Array{Int64,2}:
1  25   81  169
4  36  100  196
9  49  121  225
16  64  144  256
In :
z^2

 1 25 81 169 4 36 100 196 9 49 121 225 16 64 144 256
In :
# Inverse of a matrix

z = rand(2:10, 3, 3)
inv(z)

Out:
3×3 Array{Float64,2}:
-0.042654    -0.146919    0.180095
0.127962    -0.0592417  -0.0402844
-0.00947867   0.189573   -0.07109  
In :
z = matrix(sample(2:10, 9), ncol = 3)
solve(z)

 -0.0550847 0.182203 -0.0127119 -0.00847458 -0.279661 0.228814 0.152542 0.0338983 -0.118644
In :
# Determinant

z = rand(2:10, 3, 3)
det(z)

Out:
83.99999999999997
In :
det(z)

236
In :
# 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 :
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 :
# comparison

A = [1 2 3 4]
B = [3 2 3 6]
A == B

Out:
false
In :
A = c(1, 2, 3, 4)
B = c(3, 2, 3, 6)
identical(A, B)

FALSE
In :
# Elementwise comparison

A = [1 2 3 4]
B = [3 2 3 6]
A .== B

Out:
1×4 BitArray{2}:
false  true  true  false
In :
A = c(1, 2, 3, 4)
B = c(3, 2, 3, 6)
A == B

1. FALSE
2. TRUE
3. TRUE
4. FALSE
In :
z = [3 4 5; 6 7 9; 15 -20 25]
maximum(z)      # of entire array

Out:
25
In :
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)

max(z)

25
In :
z = [3 4 5; 6 7 9; 15 -20 25]
minimum(z)    # of entire array

Out:
-20
In :
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)

min(z)

-20
In :
z = [3 4 5; 6 7 9; 15 -20 25]
sum(z)     # of entire array

Out:
54
In :
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)

sum(z)

54
In :
z = [3 4 5; 6 7 9; 15 -20 25]
maximum(z, 1)    # of each column

Out:
1×3 Array{Int64,2}:
15  7  25
In :
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 :
z = [3 4 5; 6 7 9; 15 -20 25]
maximum(z, 2)    # of each row

Out:
3×1 Array{Int64,2}:
5
9
25
In :
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 :
z = [3 4 5; 6 7 9; 15 -20 25]
minimum(z, 1)  # of each column

Out:
1×3 Array{Int64,2}:
3  -20  5
In :
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 :
z = [3 4 5; 6 7 9; 15 -20 25]
minimum(z,  2)  # of each row

Out:
3×1 Array{Int64,2}:
3
6
-20
In :
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 :
z = [3 4 5; 6 7 9; 15 -20 25]
sum(z, 1)     # of each column

Out:
1×3 Array{Int64,2}:
24  -9  39
In :
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 :
z = [3 4 5; 6 7 9; 15 -20 25]
sum(z, 2)     # of each row

Out:
3×1 Array{Int64,2}:
12
22
20
In :
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 :
z = [3 4 5; 6 7 9; 15 -20 25]
cumsum(z, 1)     # Cumulative sum by column

Out:
3×3 Array{Int64,2}:
3   4   5
9  11  14
24  -9  39
In :
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)

apply(z, 2, cumsum)

 3 4 5 9 11 14 24 -9 39
In :
z = [3 4 5; 6 7 9; 15 -20 25]
cumsum(z, 2)     # Cumulative sum by row

Out:
3×3 Array{Int64,2}:
3   7  12
6  13  22
15  -5  20
In :
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)

t(apply(z, 1, cumsum))

 3 7 12 6 13 22 15 -5 20
In :
# 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:
1×6 Array{Int64,2}:
5  7  10  90  40  -10
In :
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 :
# 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:
1×6 Array{Int64,2}:
3  4  6  -20  12  -40
In :
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 :
# Setting the contents of arrays

a = rand(0:10, 10, 10);
a[a .== 0] = -100
a

Out:
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 :
a = matrix(sample(0:10, 100,replace = TRUE),ncol = 10)

a[a == 0] =-100
a

 8 10 10 2 10 3 8 2 3 2 2 8 1 2 8 -100 4 -100 4 8 10 -100 10 -100 4 3 6 6 2 1 -100 5 3 4 6 2 -100 9 2 7 9 2 10 1 2 6 6 10 9 9 3 10 -100 8 6 10 2 2 7 3 5 1 1 2 3 4 2 3 1 6 7 4 6 -100 9 7 9 6 9 4 7 1 10 -100 -100 9 4 10 8 7 5 5 6 8 -100 6 6 10 9 6
In :
# remove element/s

a = collect(1:10)
splice!(a,1:5)
a

Out:
5-element Array{Int64,1}:
6
7
8
9
10
In :
a = 1:10
a[-(1:5)]

1. 6
2. 7
3. 8
4. 9
5. 10
In :
# concatenate
a = collect(1:10)

push!(a, 20) # at the end
# or [a;20]

Out:
11-element Array{Int64,1}:
1
2
3
4
5
6
7
8
9
10
20
In :
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 :
# concatenate
a = collect(1:10)

unshift!(a, 20) # at the beginning
# or [20; a]

Out:
11-element Array{Int64,1}:
20
1
2
3
4
5
6
7
8
9
10
In :
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 :
# check if a value is in an array

z = 1:10

9 in z

Out:
true
In :
z = 1:10
9 %in% z

TRUE
In :
# insert to specific locations

a = [1, 3, 4, 5]
splice!(a, 2, 2:3)
a

Out:
5-element Array{Int64,1}:
1
2
3
4
5
In :
a = c(1, 3, 4, 5)
c(a, 2:3, a[3:length(a)])

1. 1
2. 2
3. 3
4. 4
5. 5
In :
# remove last element

z = collect(1:10)
pop!(z)
z

Out:
9-element Array{Int64,1}:
1
2
3
4
5
6
7
8
9
In :
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 :
# remove first element

z = collect(1:10)
shift!(z)
z

Out:
9-element Array{Int64,1}:
2
3
4
5
6
7
8
9
10
In :
z = 1:10
z[-1]

1. 2
2. 3
3. 4
4. 5
5. 6
6. 7
7. 8
8. 9
9. 10
In :
# delete by indices

z = collect(1:10)
deleteat!(z, [1 4 6])

Out:
7-element Array{Int64,1}:
2
3
5
7
8
9
10
In :
z = 1:10
z[-c(1,4,6)]

1. 2
2. 3
3. 5
4. 7
5. 8
6. 9
7. 10
In :
y = [ 2 3 5; -6 3 8; -9 3 11]
find(isodd, y)  # gives indices of odd numbers

Out:
6-element Array{Int64,1}:
3
4
5
6
7
9
In :
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 :
# Returns the row and column count of the array

x = randn(12,15,16);
size(x)

Out:
(12,15,16)
In :
x = array(rnorm(12*15*16),c(12, 15, 16))
dim(x)

1. 12
2. 15
3. 16
In :
# How many elements the array contains

x = randn(12,15,16);
length(x)

Out:
2880
In :
x = array(rnorm(12*15*16),c(12, 15, 16))
length(x)

2880
In :
# How many non-zero

x = [1 2 0 ; 4 5 0]
countnz(x)

Out:
4
In :
x = matrix(c(1, 2, 0, 4, 5, 0), ncol =3, byrow = TRUE)
length(x[x !=0])

4
In :
union(1:6,4:10)  # removes duplicates

Out:
10-element Array{Int64,1}:
1
2
3
4
5
6
7
8
9
10
In :
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 :
# Intersection of two or more arrays

intersect(1:10,7:15)

Out:
7:10
In :
intersect(1:10, 7:15)

1. 7
2. 8
3. 9
4. 10
In :
# Elements that are in the first array but not the second
setdiff(1:10,7:15)

Out:
6-element Array{Int64,1}:
1
2
3
4
5
6
In :
setdiff(1:10,7:15)

1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
In :
# Elements that are in the first array but not the second

setdiff(7:15,1:10)

Out:
5-element Array{Int64,1}:
11
12
13
14
15
In :
setdiff(7:15, 1:10)

1. 11
2. 12
3. 13
4. 14
5. 15
In :
y = [ 2 3 5; -6 3 8; -9 3 20]
filter(iseven,y)  # gives the evenones

Out:
4-element Array{Int64,1}:
2
-6
8
20
In :
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 :
# Count the number of elements that satisfy the condition

count(isodd, 1:5000)

Out:
2500
In :
y = 1:5000
length(y[y%%2 != 0])

2500
In :
# check if any of the elements satisfy the condition

any([1 2 3 10 12] .== [3 4 5 10 25])

Out:
true
In :
any(c(1, 2, 3, 10, 12)==c(3, 4, 5, 10, 25))

TRUE
In :
# check if all the elements satisfy the condition

all([1 2 3 10 12] .== [3 4 5 10 25])

Out:
false
In :
all(c(1, 2, 3, 10, 12)==c(3, 4, 5, 10, 25))

FALSE
In :
# Random element from an array
z = range(1, 3, 20)
z[rand(1:end)]

Out:
55
In :
z = seq(from = 1, by = 3, length.out = 20)
sample(z, 1)

55
In :
# Find the extreme values of an array:

z = 1:2:200
extrema(z)

Out:
(1,199)
In :
z = seq(from = 1, to = 200, by =2)
range(z)

1. 1
2. 199
In :
z = [1 2 3; 4 5 6; 7 8 9]
extrema(z, 1)

Out:
1×3 Array{Tuple{Int64,Int64},2}:
(1,7)  (2,8)  (3,9)
In :
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),
ncol = 3, byrow = TRUE)
apply(z, 2, range)

 1 2 3 7 8 9
In :
z = [1 2 3; 4 5 6; 7 8 9]
extrema(z, 2)

Out:
3×1 Array{Tuple{Int64,Int64},2}:
(1,3)
(4,6)
(7,9)
In :
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),
ncol = 3, byrow = TRUE)
t(apply(z, 1, range))

 1 3 4 6 7 9
In :
# find product
z = [1 2 3; 4 5 6; 7 8 9]
prod(z)

Out:
362880
In :
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),
ncol= 3, byrow = TRUE)
prod(z)

362880
In :
# find product
z = [1 2 3; 4 5 6; 7 8 9]
prod(z, 1)  # for each column

Out:
1×3 Array{Int64,2}:
28  80  162
In :
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 :
# find product
z = [1 2 3; 4 5 6; 7 8 9]
prod(z, 2)  # for each row

Out:
3×1 Array{Int64,2}:
6
120
504
In :
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 :
# find mean
z = [1 2 3; 4 5 6; 7 8 9]
mean(z)

Out:
5.0
In :
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),
ncol= 3, byrow = TRUE)
mean(z)

5
In :
# find mean
z = [1 2 3; 4 5 6; 7 8 9]
mean(z, 1)  # for each column

Out:
1×3 Array{Float64,2}:
4.0  5.0  6.0
In :
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 :
# find mean
z = [1 2 3; 4 5 6; 7 8 9]
mean(z, 2)  # for each row

Out:
3×1 Array{Float64,2}:
2.0
5.0
8.0
In :
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 :
# find median
z = [1 2 3; 4 5 6; 7 8 9]
middle(z)

Out:
5.0
In :
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),
ncol= 3, byrow = TRUE)
median(z)

5
In :
# find median
z = [1 2 3; 4 5 6; 7 8 9]
mean(z, 1)  # for each column

Out:
1×3 Array{Float64,2}:
4.0  5.0  6.0
In :
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 :
# for loop
z = Int64[]
for i = 1:10
z = [z; i^2];
end
z

Out:
10-element Array{Int64,1}:
1
4
9
16
25
36
49
64
81
100
In :
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 :
# if condition

evens = Int64[]
for i = 1:20
if i%2 ==0
evens = [evens; i]
end
end
evens

Out:
10-element Array{Int64,1}:
2
4
6
8
10
12
14
16
18
20
In :
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 :
# 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 :
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 :
# sinle line function

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

f(-40)

WARNING: Method definition f(
Out:
-40.0
Any) in module Main at In:4 overwritten at In:3.

In :
f  <- function(x) 9/5*x + 32

f(-40)

-40
In :
# 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:
"Yor number is even"
In :
f  <- function(x){
if(x%%2 ==0){
}else{
}
}

f(-40)

 Your number is even
In :
a = [1 2 3;-4 -5 6; 7 2 -9]
sort(a, 1)  # sort along columns
# we can also use sortcolumns()

Out:
3×3 Array{Int64,2}:
-4  -5  -9
1   2   3
7   2   6
In :
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 :
a = [1 2 3;-4 -5 6; 7 2 -9]
sort(a, 2)  # sort along rows
# we can also use sortrows()

Out:
3×3 Array{Int64,2}:
1   2  3
-5  -4  6
-9   2  7
In :
a = matrix(c(1, 2, 3, -4, -5, 6, 7, 2, -9),
ncol = 3, byrow = TRUE)
t(apply(a, 1, sort))

 1 2 3 -5 -4 6 -9 2 7
In :
a = [1 2 3;-4 -5 6; 7 2 -9]
sort(a, 1, rev = true)  # decreasing order

Out:
3×3 Array{Int64,2}:
7   2   6
1   2   3
-4  -5  -9
In :
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