Chapter 1. Installation and Basics
Last updated
Was this helpful?
Last updated
Was this helpful?
Given the popularity of Python, R, and Stata (in Econ.), why do we need Julia? Three reasons:
Fast, compared with popoular programming langugages in data analysis, Julia shows high efficiency, as shown in the following figure.
(The evaluation is done byDaniel Moura at https://towardsdatascience.com/r-vs-python-vs-julia-90456a2bcbab)
Ease of use This language is new, but pretty similar to R and Python & optimized for data analysis.
Python / R integrateble: Julia allows you integrate ggplot2 in R, pytorch in Python into a data project written in Julia. More details can be found in Chatper 7.
For fresh installation on linux / mac (if you are using windows, click here):
curl -fsSL <https://install.julialang.org> | sh
If you have previous versions installed:
juliaup self uninstall
Run julia
in your shell / commandline
Install the package for installing a kernel for Jupyter notebooks
Install a kernel for Jupyter notebook:
(optional) Install additional kernels for multi-threads computing:
8 thraeds
32 thraeds
(Additional information can be found here)
Now, you will be able to choose Julia for your Jupyter notebooks:
Different from that in Python and R, Julia uses function println()
to print strings and logs:
Some times, we want to create new variables, and assign values to them -- it's like Python:
Of course, we want to combine some logs for us to document the progress of analysis:
Alternatively, you can also use *
operater to concat strings:
(This seemingly weird operation is explained here)
You may have noted that we are doing operations. In Julia, basic operations of numbers are:
Arithmetic Operators
x + y
binary plus
performs addition
x - y
binary minus
performs subtraction
x * y
times
performs multiplication
x / y
divide
performs division
x ÷ y
integer divide
x / y, truncated to an integer
x ^ y
power
raises x to the yth power
x % y
remainder
equivalent to rem(x, y)
(Those are common operations, see full list here)
Logical Operators
!x
Not
x && y
And
x || y
Or
Comparisons
==
Equal to
!=, ≠
Not equal to
<
Less than
<=, ≤
Less than or equal to
>
Greater than
>=, ≥
Greater than or equal to
There are more operations less frequently used in data analysis, see full list here.
Some operations are only applicable to certain data types (e.g., the *
function for concatanating strings has a different meaning to numerical values). If you are confused about the data type of a variable:
String
Interger
To save memories, you can define the length of a number:
By default, it's 64:
Float
What would happen if an int
is multiplied by a float
?
Julia also has a dynamic type system.
Vector (List / Array)
A vector can be indexed by an integer:
Or a vector:
Tuples
A tuple can be indexed by an integer:
Or a vector:
To quickly create a vector composing of a continuous integers for indexing:
Because 1:3
has created a vector-like range:
Dictionary
The dictionary can be retrieved by its key:
And the value can be changed:
In many cases, we need to import many packages to finish diverse tasks. For example, we use ggplot2
in R to create beautiful figures.
In Julia, the workflow is like what we have in R (you can also use my package to simplify the same process in Python).
Then, when a package is installed, you need to claim to use it before using:
After that, you are able to use methods of a package such as JSON.parsefile("some.json")
here
To define a function:
To use a method defined globally, such as println
, just call it.
If it's from a package, use package.method
, e.g., JSON.parsefile("some.json")
we've mentioned.
Great! Let's move on to load your data first.