The Brass Tacks of Julia Programming – Part III


Visit: 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 Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight 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 permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR 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 sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. 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.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.