Chapter 1. Installation and Basics
How to install Julia
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
Jupyter Notebook Kernels
Run
julia
in your shell / commandlineInstall 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:
Basics in Julia
Print, assign, and operatons
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
ExpressionNameDescriptionx + 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
ExpressionName!x
Not
x && y
And
x || y
Or
Comparisons
OperatorName==
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.
Data structures
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:
Using Packages
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
Functions and methods
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.
Last updated