Move Seamlessly from R to Julia - 1
I’ve been using R and Python for quite a while, but recently I started exploring Julia, and I absolutely love it! Unlike Python and R, which were developed in the early 1990s, Julia’s development began around 2010, making it a newer language. So, in this video, I’ll share how you can make a smooth transition from R to Julia.
Before diving into the specifics, let me briefly explain how Julia differs from other programming languages like Python or R. At the core of all programming, computers understand only one language: the language of zeros and ones. In the early days of computing, programmers had to work directly with machine language. I was recently reading about the development of the classic 1990s video game Prince of Persia and learned that it was written entirely in 6502 assembly language. This is the “language of zeros and ones,” or something equivalent.
The challenge with this language is that while it’s very efficient because it communicates directly with the hardware, writing large programs in assembly is not practical. To address this, computer scientists introduced compiled languages like C, C++, and Java. These languages let you write code in a human-readable format, and a compiler translates it into machine code, which the computer can understand. This process, known as compilation, makes programs more efficient but often less interactive. For example, after writing a large program, you need to compile and run it, then make changes and compile again.
To solve this issue, interpreted languages like R and Python were created. In these languages, you write code line-by-line, and the interpreter immediately gives you results. However, interpreted languages tend to be slower because they don’t benefit from the efficiency of compilation.
This is where Julia shines. Julia combines the flexibility of interpreted languages with the efficiency of compiled ones. The first time you run a function, Julia interprets it, but it also compiles the code behind the scenes. The next time you run that function, it uses the compiled version, resulting in a faster execution time.
Let’s look at some of Julia’s advantages. I came across a Reddit thread where someone asked about Julia’s standout features compared to languages like MATLAB and Python. One person shared their experience working at a hedge fund, where they found Julia not only made their research more productive but also saved hundreds of thousands of dollars on AWS costs.
Here are some of the main points they mentioned:
Speed: Julia has Python and MATLAB-like syntax, but it runs 10 times faster without any extra effort. With some optimization, you can achieve speeds up to 100x or even 1000x faster.
Macros: Julia offers powerful macros, which let you write code that generates other code. This reduces the size of your codebase and makes debugging and evaluation easier.
Single Language: Unlike Python, where you often write critical parts in C for efficiency, in Julia, everything is done in one language. This is a big advantage because it simplifies development and makes your brain’s job easier.
Jupyter Notebook Support: If you’re familiar with Python, you know Jupyter notebooks. Julia works seamlessly with them, so it’s easy to get started.
Concurrency: Julia uses a concurrency model similar to Go, making it easy to write single-threaded functions that you can run concurrently later. This is especially useful when working with multi-core computers or servers.
GPU Support: Julia makes it easy to run code on GPUs, with broadcasting operations automatically converting to GPU kernels. Broadcasting is a feature that applies functions to each element of an array or vector efficiently.
So, let’s dive into using Julia! The first step is installing it, which is straightforward. Head to JuliaLang.org and download the appropriate installer for your system (Windows, Mac, or Linux). After installation, you’ll see the Julia icon on your desktop or Finder (if you're on Mac). Double-click to open the Julia window, and you're ready to go.
Let’s go over some basic Julia syntax, which is quite similar to R:
For simple operations like addition, you can do
1 + 3
, and the result will appear.Multiplication, subtraction, and division also work as expected, with the same operators.
For the remainder, use the
%
operator:34 % 3
will return 1.To quit Julia, use
Ctrl + D
, and to clear the screen, useCtrl + L
.
Let’s now talk about vectors, which are a fundamental data structure in Julia. Here’s how you work with vectors in R and Julia:
In R, you can create a vector with
1:10
, and it generates a sequence of integers from 1 to 10. In Julia, the equivalent is1:10
, which also creates a vector from 1 to 10.To generate a vector with specific increments, in R you would use
seq(1, 10, 0.01)
, whereas in Julia, you use1:0.01:10
.You can perform vectorized operations, like multiplying a vector by itself (
X * X
), just like you would in R.
Now, let’s look at broadcasting in Julia. This is a feature that allows you to apply a function to each element of a vector. For example, to apply the sine function to each element of a vector X
, in R you would simply do sin(X)
. In Julia, you do sin.(X)
, where the dot .
is used for broadcasting.
If you want to plot in Julia, it’s similar to R, but you need to install a plotting package first. You can do this by running import Pkg; Pkg.add("Plots")
, and then load the package with using Plots
. After that, you can easily plot data like plot(x, y)
.
Julia also supports data frames, but just like with plots, you need to install the DataFrames
package first. Once installed, you can use Julia’s data frame capabilities just like you would in R.
In the next video, I’ll dive deeper into working with data frames in Julia, so stay tuned for that.