Professional Documents
Culture Documents
Julia Basics Cheat Sheet Arithmetic operators Vectors are one-dimensional arrays in Julia. They allow a collection of items such as floats,
integers, strings or a mix that allows duplicate values.
37 + 102
Creating vectors
# Subtract two numbers with -
# Create vectors with square brackets, [x1, x2, x3]
102 - 37
x = [1, 2, 3]
4 * 6
Vector{Float64}([1, 2, 3])
21/7
37:100
?first
# Integer divide a number with ÷
# Create sequence of numbers from a to b in steps with a:step:b
22 ÷ 7 # This returns 3
1:2:101
> Comments 3 ^ 3
Vector functions
# Get the remainder after division with %
x = [9, 1, 4]
sort(x)
> Information about objects Assignment operators # Reversing vectors with reverse(x)
reverse(x)
a = 5
reverse!(x)
unique(x)
Packages are libraries of pre-written code (by other programmers) that we can add to our
Julia installation, which help us solve specific problems. Here’s how to install and work with
a -= 3 # This is the same as a = a - 3 Selecting vector elements
packages in Julia.
Numeric comparison operators # Selecting the 6th element of a vector with x[6]
]
# Test greater or equal with >=
x[6]
3 >= 3
add CSV
# Test less than with <
x[begin] # This is the same as x[1]
3 < 4
<DEL>
3 > 1
3 <= 4
using CSV
x[2:6]
import CSV
x[[2,6]]
import DataFrames as df
x = [11, 13, 19]
x[x .== 5]
> The working directory x |> (y -> length(y) + sum(y)) # This returns 43
x[x .< 5]
x[in([2, 5, 8]).(x)]
The working directory is a file path that Julia will use as the starting point for relative file
paths. That is, it's the default location for importing and exporting files. An example of a
Logical operators
working directory looks like "/Users/myname/workspace/myproject"
pwd()
"/home/programming_languages/julia"
# Elementwise and with &
# Elementwise xor (exclusive or) with
cd("/home/programming_languages/julia/cheatsheets")
> Getting started with characters and strings > Getting started with DataFrames
Julia Basics Cheat Sheet Characters and strings are text data types in Julia. Characters refer to text data with exactly one # Install the DataFrames and CSV packages
character, and are created with single quotes, ''. Strings are sequences of characters, and are ]
Learn Julia online at www.DataCamp.com created with double or triple-double quotes, " " or """ """.
add DataFrames
add CSV
char = 'a'
using CSV
df = DataFrame(
# Example vector
# Create a string variable with triple double quotes
string_column= ['M', 'F', 'F', 'M'], # Vector of characters
x = [9, 1, 4, 6, 7, 11, 5]
string = """Hello
a_number = 0, # Fill whole column with one integer
World!"""
a_string = "data frames" # Fill whole column with one string
log(2)
log.(x)
df.string_column
exp(2)
exp.(x)
maximum(x)
sum(x)
The following code requires installing and loading the Statistics and StatsBase packages. "Echo! " ^ 3 # Returns "Echo! Echo! Echo! "
split("lions and tigers and bears", " and ") # Returns 3-element vector
# Select columns of a data frame with select()
mean(x)
Finding and mutating strings # Drop columns of a data frame with select(Not())
median(x)
# Get rows of a df3 with distinct values in column_A with unique(df, :col)
unique(df3, :column_A)
round.(x, 2)
sort(df3, :numeric_column)
ordinalrank(x)
describe(df3)
var(x)
std(x)
cor(x, y)