The Brass Tacks of Julia Programming – Part III

Articles From: QuantInsti
Website: QuantInsti

See Part I for instructions on basic arithmetic operations and Part II for working with matrices

Using R and Python code in Julia

There can be times where you want to use your R or Python code as the libraries to perform certain operations may not be available in Julia.

However, using 2 languages to perform a certain operation can be a challenge. Hence, Julia provides an option to use R and Python code by using the “PyCall” and “RCall” packages.

How to use Python code in Julia

Here’s how you can use your Python code in Julia.

# Install the "PyCall" package for Python
using Pkg
using PyCall

## Importing numpy package from Python
np = pyimport("numpy")

## Using numpy package in Julia

3-element Vector{Int64}:

How to use R code in Julia

Let’s look at an example of using R in Julia.

# Install "RCall" package for R
using RCall

# There are several ways to call R code

# Method 1
# Calling the mean() function from R
rcall(:mean, [1,2,3])

# Method 2 for user-defined functions
power_4 <- function(x)
## Calling the defined function
rcall(:power_4, 5)

[1] 2
[1] 625

For further details on using the RCall package, you can refer here.

Python vs Julia – Comparison of syntax for basic operations

Column vector[1, 2, 3][1,2,3]
Row vector[[1], [2], [3]][1 2 3]
Sequence of numbersrange(1, 10,2)[1:2:10; ]
Matrix[[1,2] , [3,4]][1 2; 3 4]
Random numbersnp.random.rand(4,5)rand(4,5)
Mutationinplace =True!
Matrix to vectorA.flatten()A[:]
Conjugate of a matrixA.conj()A’
Element wise operationmap().
IndexingStarts from 0Starts from 1
Maximum valuemax(A)maximum(A)
Minimum valuemin(A)minimum(A)
For loopfor i in range(n):
# command
for i in 1:n
# command
While loopWhile i <=n:While i <=n
If-elseIf i <=m:
# command
if i <=m
# command
# command
Temporary functionY = lambda x: x**5Y = x -> x^5
Functiondef func(y, x):
return y + 3*x
func(y, x) = y + 3x
function func(y, x)
return y + 3x

Visit QuantInsti for additional insight on this article:

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.