├── .vscode └── settings.json ├── .DS_Store ├── src ├── HealthCareStatistics.jl ├── Data transformation.jl ├── 03 Dataframe joins.jl ├── missing.csv ├── 01 Creating data.jl ├── 04 Missing data.jl ├── generated_data.csv ├── 05 Manipulating data.jl ├── 02 DataFrame introduction.jl └── 00 Introduction to Julia.jl ├── README.md ├── Project.toml └── Manifest.toml /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanklopper/HealthCareStatistics/master/.DS_Store -------------------------------------------------------------------------------- /src/HealthCareStatistics.jl: -------------------------------------------------------------------------------- 1 | module HealthCareStatistics 2 | 3 | greet() = print("Hello World!") 4 | 5 | end # module 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HealthCareStatistics 2 | 3 | Files for my new course on healthcare statistics using Julia. Work has just started. Watch this space. 4 | 5 | Download the Julia file appropriate for your operating system from https://www.julialang.org and install the downloaded file. Note where Julia is installed. 6 | 7 | Download Microsoft Visual Studio code from https://code.visualstudio.com/ (there are files for Windows 10 and macOS). Install VSCode. 8 | 9 | Click the Extensions icon in the left-hand pane of VSCode and search for Julia. Install the extension. 10 | 11 | -------------------------------------------------------------------------------- /Project.toml: -------------------------------------------------------------------------------- 1 | name = "HealthCareStatistics" 2 | uuid = "c0460a50-edbc-44f8-b2a9-6b3c46bc3119" 3 | authors = ["juanklopper "] 4 | version = "0.1.0" 5 | 6 | [deps] 7 | CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" 8 | DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" 9 | DataFramesMeta = "1313f7d8-7da2-5740-9ea0-a2ca25f37964" 10 | Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" 11 | GLM = "38e38edf-8417-5370-95a0-9cbb8c7f171a" 12 | HypothesisTests = "09f84164-cd44-5f33-b23f-e6b0d136a0d5" 13 | Pipe = "b98c9c47-44ae-5843-9183-064241ee97a0" 14 | Pluto = "c3e4b0f8-55cb-11ea-2926-15256bba5781" 15 | PlutoUI = "7f904dfe-b85e-4ff6-b463-dae2292396a8" 16 | Query = "1a8c2f83-1ff3-5112-b086-8aa67b057ba1" 17 | VegaDatasets = "0ae4a718-28b7-58ec-9efb-cded64d6d5b4" 18 | VegaLite = "112f6efa-9a02-5b7d-90c0-432ed331239a" 19 | -------------------------------------------------------------------------------- /src/Data transformation.jl: -------------------------------------------------------------------------------- 1 | using Distributions, HypothesisTests, Random, Statistics, DataFrames, VegaLite 2 | 3 | var_unif = rand(Uniform(10, 20), 100); 4 | 5 | df = DataFrame(Uniform = var_unif); 6 | 7 | df[1:3, :] 8 | 9 | df |> @vlplot(:bar, x={:Uniform, bin=true}, y="count()") 10 | 11 | pvalue(ExactOneSampleKSTest(var_unif, Normal(mean(var_unif), std(var_unif)))) 12 | 13 | var_normal = rand(Normal(10, 2), 100); 14 | 15 | df[!, "Normal"] = var_normal; 16 | 17 | df |> @vlplot(:bar, x={:Normal, bin=true}, y="count()") 18 | 19 | ExactOneSampleKSTest(var_normal, Normal(mean(var_normal), std(var_normal))) 20 | 21 | multi_unif = rand(Uniform(10, 20), (100,1000)); 22 | 23 | ks_unif = [pvalue(ExactOneSampleKSTest(multi_unif[:, i], Normal(mean(multi_unif[:, i]), std(multi_unif[:, i])))) for i in 1:1000]; 24 | 25 | ks_df = DataFrame(pUniform = ks_unif) 26 | 27 | ks_df |> @vlplot(:bar, x={:pUniform, bin=true}, y="count()") 28 | 29 | ExactOneSampleKSTest(ks_unif, Normal(mean(ks_unif), std(ks_unif))) -------------------------------------------------------------------------------- /src/03 Dataframe joins.jl: -------------------------------------------------------------------------------- 1 | # JOINING DATAFRAMES 2 | # ------------------ 3 | 4 | # Read more at https://dataframes.juliadata.org/stable/man/joins/ 5 | 6 | 7 | # CREATING DATAFRAMES 8 | 9 | using DataFrames, Random 10 | 11 | Random.seed!(10) 12 | 13 | dfDemographics = DataFrame(ID = 1:10, Area = rand(["Zone I", "Zone II", "Zone III"], 10)) 14 | 15 | dfPersonal = DataFrame(ID = 1:11, Age = rand(18:85, 11)) 16 | 17 | dfData = DataFrame(ID = 1:8, LDLDelta = rand(-5:5, 8) ./ 10) 18 | 19 | 20 | # JOIN ONLY WHEN DATA IS AVAILABLE ACROSS DATAFRAMES 21 | 22 | innerjoin(dfDemographics, dfPersonal, on = :ID) 23 | 24 | innerjoin(dfPersonal, dfData, on = :ID) 25 | 26 | innerjoin(dfDemographics, dfPersonal, dfData, on = :ID) 27 | 28 | 29 | # JOIN ON THE FIRST DATAFRAME 30 | 31 | leftjoin(dfPersonal, dfData, on = :ID) 32 | 33 | 34 | # JOIN ON THE LAST DATAFRAME 35 | rightjoin(dfData, dfPersonal, on = :ID) 36 | 37 | 38 | # JOIN ON ALL DATA 39 | 40 | outerjoin(dfDemographics, dfPersonal, dfData, on = :ID) 41 | 42 | 43 | -------------------------------------------------------------------------------- /src/missing.csv: -------------------------------------------------------------------------------- 1 | ID,Age,Zone,Group,LDLDelta 2 | 1,42,I,Placebo,-0.8 3 | 2,70,I,Treatment,-1.2 4 | 3,36,II,Placebo,0 5 | 4,36,II,Treatment,-0.6 6 | 5,74,III,Placebo,-0.1 7 | 6,34,II,Treatment, 8 | 7,54,I,Placebo,-1.8 9 | 8,44,III,Treatment,-1.4 10 | 9,57,III,Placebo,-1.6 11 | 10,,II,Treatment, 12 | 11,31,II,Placebo,-0.4 13 | 12,55,I,Treatment,0 14 | 13,40,I,Placebo,-0.2 15 | 14,48,II,Treatment, 16 | 15,,II,Placebo,-1.5 17 | 16,67,III,Treatment,-0.4 18 | 17,61,II,Placebo,-0.7 19 | 18,55,I,Treatment,-1.8 20 | 19,62,III,Placebo,-1.1 21 | 20,60,III,Treatment,-0.2 22 | 21,64,II,Placebo,-0.3 23 | 22,63,II,Treatment,-1.9 24 | 23,44,I,Placebo,-0.8 25 | 24,50,I,Treatment,-1.4 26 | 25,37,II,Placebo,-1.5 27 | 26,65,II,Treatment, 28 | 27,53,III,Placebo,-2 29 | 28,45,II,Treatment,-1.1 30 | 29,48,I,Placebo,-1 31 | 30,47,III,Treatment,-1.2 32 | 31,,,Placebo,-1.6 33 | 32,55,II,Treatment,-0.6 34 | 33,55,II,Placebo,-0.2 35 | 34,56,I,Treatment,-1.5 36 | 35,55,I,Placebo,-1.4 37 | 36,51,II,Treatment,-2 38 | 37,47,II,Placebo,-0.6 39 | 38,72,III,Treatment,-2 40 | 39,49,II,Placebo,-1.3 41 | 40,48,,Treatment,-1.5 42 | 41,53,III,Placebo,-1.9 43 | 42,55,III,Treatment,-1.9 44 | 43,54,II,Placebo,-0.6 45 | 44,75,II,Treatment,0 46 | 45,43,I,Placebo,-0.5 47 | 46,57,I,Treatment,-0.5 48 | 47,42,II,Placebo,-1.1 49 | 48,,II,Treatment,-0.7 50 | 49,59,III,Placebo, 51 | 50,56,II,Treatment,-1.5 52 | 51,41,I,Placebo,-0.9 53 | 52,38,III,Treatment,-1.1 54 | 53,48,III,Placebo,-1.4 55 | 54,52,II,Treatment,0 56 | 55,39,II,Placebo,-1.2 57 | 56,38,I,Treatment,0 -------------------------------------------------------------------------------- /src/01 Creating data.jl: -------------------------------------------------------------------------------- 1 | # CREATING DATA AND STORING IT IN A DATAFRAME 2 | # ------------------------------------------- 3 | 4 | # CREATING RANDOM VARIABLE 5 | 6 | using Random 7 | 8 | rand() # A single random value on the interval [0,1] 9 | 10 | 1:5 # A unit range (shorthand for UnitRange) 11 | 12 | UnitRange(1, 5) # The UnitRange function 13 | 14 | collect(1:5) # Create an array from the unit range 15 | 16 | rand(1:5) # A single random value from the interval [1,5] 17 | 18 | rand(1:5, 3) # Three random variable on the interval [1,5] 19 | 20 | rand(["Treatment", "Placebo"], 5) # Random nominal categorical variable 21 | 22 | categorical(rand(["Treatment", "Placebo"], 5), levels = ["Placebo", "Treatment"]) 23 | # Indicate the data type as categorical arrays and sets the levels 24 | 25 | using Distributions 26 | 27 | rand(Normal(), 5) # Five random variable from the standard normal distribution 28 | 29 | # CREATING A DATAFRAME OBJECT 30 | 31 | using DataFrames 32 | 33 | DataFrame() # An empty dataframe object 34 | 35 | DataFrame(HeartRate = 70:75) # Using a column name and an assigment 36 | 37 | DataFrame(:HeartRate => 70:75) # Using a symbol and a unit range pair 38 | 39 | # CREATING A DATAFRAME WITH DATA 40 | 41 | n = 100 # Sample size 42 | 43 | rand() # A random variable from the interval [0, 1] (uniform distribution) 44 | 45 | round(rand(), digits = 1) # Round off to a single digit 46 | 47 | Random.seed!(21); # Seed the pseudorandom number generator 48 | 49 | df = DataFrame(ID = 1:n, Age = rand(18:85, n), Group = categorical(rand(["Treatment", "Placebo"], n)), DeltaLDL = round.(rand(Normal(-0.5, 0.5), n), digits = 1)) 50 | 51 | 52 | # SAVE DATAFRAME OBJECT AS A CSV FILE 53 | 54 | using CSV 55 | 56 | CSV.write("/Users/juan/Documents/Julia/Projects/HealthCareStatistics/src/generated_data.csv", df) 57 | 58 | 59 | # READING A CSV FILE AS A DATAFRAME 60 | 61 | df2 = CSV.File("src/generated_data.csv") |> DataFrame 62 | 63 | first(df2, 3) 64 | -------------------------------------------------------------------------------- /src/04 Missing data.jl: -------------------------------------------------------------------------------- 1 | # MANAGING MISSING DATA 2 | # --------------------- 3 | 4 | # MISSING DATA TYPE 5 | 6 | missing # Julia keyword for absent data 7 | 8 | typeof(missing) 9 | 10 | supertype(Missing) 11 | 12 | wcc = [12.4, 13.4, missing, 9.8, 14.7] # Array containing an absent data point 13 | 14 | typeof(wcc) 15 | 16 | eltype(wcc) # Data types of elements 17 | 18 | sum(skipmissing(wcc)) # Add all values and skip absent data point 19 | 20 | length(collect(skipmissing(wcc))) # Some functions require collect functions 21 | 22 | wcc_mean = sum(skipmissing(wcc)) / length(collect(skipmissing(wcc))) 23 | # Mean is sum divided by sample size 24 | 25 | imputed_wcc = coalesce.(wcc, wcc_mean) # Replace missing data with mean 26 | 27 | wcc # Absent data is still missing 28 | 29 | 30 | # READ A FILE WITH MISSING DATA DENOTED BY EMPTY CELLS 31 | 32 | using CSV, DataFrames 33 | 34 | df = CSV.File("src/missing.csv") |> DataFrame 35 | 36 | names(df) 37 | 38 | show(df, allrows = true) 39 | 40 | 41 | # TOTAL MISSING VALUES 42 | 43 | count(ismissing, Matrix(df)) # Convert to an array 44 | 45 | count(ismissing, Iterators.flatten(eachcol(df))) # Alternative 46 | 47 | 48 | # TOTAL MISSING VALUES IN EACH COLUMN 49 | 50 | mapcols(x -> count(ismissing, x), df) # Show number of missing values 51 | # in each columns 52 | 53 | 54 | # FIND ROWS WITH MISSING VALUES 55 | 56 | filter(row -> any(ismissing, row), df) 57 | 58 | 59 | # REMOVE ALL ROWS WITH MISSING DATA 60 | 61 | dfAllData = dropmissing(df) # Create a new dataframe object 62 | 63 | show(dfAllData, allrows = true) 64 | 65 | 66 | # REMOVE ROWS WHEN SPECIFIED COLUMNS HAS MISSING DATA 67 | 68 | show(dropmissing(df, :Age), allrows = true) # Only drop rows when 69 | # age data is not available 70 | 71 | 72 | # IMPUTE MISSING DATA 73 | 74 | sum(skipmissing(df.Age)) # Sum of all available ages 75 | 76 | length(collect(skipmissing(df.Age))) # Sample size of available ages 77 | 78 | mean_age = sum(skipmissing(df.Age)) / length(collect(skipmissing(df.Age))) # Mean age 79 | 80 | imputed_age = coalesce.(df.Age, mean_age) # Array with imputed values 81 | 82 | df.Age = imputed_age # Replace age column with array 83 | 84 | mapcols(x -> count(ismissing, x), df) # Show number of missing values 85 | # in each columns -------------------------------------------------------------------------------- /src/generated_data.csv: -------------------------------------------------------------------------------- 1 | ID,Age,Group,DeltaLDL 2 | 1,22,Treatment,-1.3 3 | 2,73,Treatment,-0.5 4 | 3,36,Treatment,-0.0 5 | 4,81,Treatment,-0.5 6 | 5,59,Treatment,0.0 7 | 6,72,Treatment,-1.6 8 | 7,48,Treatment,-0.3 9 | 8,21,Placebo,0.2 10 | 9,66,Placebo,-1.5 11 | 10,24,Placebo,-0.1 12 | 11,35,Placebo,-0.0 13 | 12,77,Treatment,-0.4 14 | 13,73,Placebo,-0.7 15 | 14,77,Treatment,-0.7 16 | 15,49,Placebo,-0.2 17 | 16,24,Placebo,-0.1 18 | 17,76,Treatment,-0.5 19 | 18,47,Placebo,-1.5 20 | 19,49,Placebo,-0.9 21 | 20,57,Placebo,-0.6 22 | 21,43,Placebo,-1.1 23 | 22,42,Placebo,-1.3 24 | 23,80,Treatment,-0.4 25 | 24,81,Placebo,-0.1 26 | 25,80,Placebo,0.7 27 | 26,83,Treatment,-0.3 28 | 27,58,Placebo,-0.2 29 | 28,45,Placebo,-1.5 30 | 29,55,Placebo,0.3 31 | 30,77,Treatment,-0.7 32 | 31,24,Treatment,-0.9 33 | 32,23,Treatment,0.1 34 | 33,35,Placebo,-1.4 35 | 34,48,Placebo,-0.7 36 | 35,45,Placebo,-0.1 37 | 36,18,Placebo,-1.3 38 | 37,52,Placebo,-1.3 39 | 38,78,Placebo,-0.9 40 | 39,78,Treatment,-1.0 41 | 40,73,Placebo,0.3 42 | 41,36,Placebo,-0.6 43 | 42,52,Placebo,-0.3 44 | 43,83,Placebo,-0.4 45 | 44,57,Treatment,-0.6 46 | 45,80,Placebo,-1.5 47 | 46,51,Placebo,-0.9 48 | 47,80,Treatment,-0.6 49 | 48,38,Treatment,-0.9 50 | 49,53,Treatment,-0.5 51 | 50,27,Placebo,0.2 52 | 51,42,Placebo,-0.7 53 | 52,52,Placebo,-0.1 54 | 53,70,Treatment,0.3 55 | 54,21,Placebo,-0.2 56 | 55,81,Treatment,-0.2 57 | 56,78,Treatment,-0.3 58 | 57,63,Treatment,0.1 59 | 58,31,Treatment,-0.5 60 | 59,42,Placebo,-0.6 61 | 60,35,Placebo,0.1 62 | 61,48,Treatment,-0.7 63 | 62,69,Treatment,0.2 64 | 63,38,Treatment,-0.5 65 | 64,73,Treatment,-0.9 66 | 65,62,Treatment,0.0 67 | 66,41,Treatment,-0.7 68 | 67,30,Treatment,0.0 69 | 68,56,Placebo,-1.6 70 | 69,28,Treatment,0.8 71 | 70,61,Treatment,-0.6 72 | 71,53,Treatment,-0.5 73 | 72,58,Treatment,-1.1 74 | 73,80,Treatment,-0.1 75 | 74,37,Treatment,0.6 76 | 75,41,Placebo,-0.1 77 | 76,23,Placebo,-1.0 78 | 77,52,Placebo,-0.2 79 | 78,33,Placebo,0.1 80 | 79,29,Treatment,-0.7 81 | 80,28,Treatment,-1.0 82 | 81,41,Treatment,-1.3 83 | 82,39,Treatment,-1.2 84 | 83,45,Placebo,-0.9 85 | 84,29,Treatment,0.4 86 | 85,53,Placebo,0.3 87 | 86,28,Treatment,-0.4 88 | 87,76,Placebo,-0.8 89 | 88,81,Treatment,-0.8 90 | 89,23,Placebo,-0.5 91 | 90,40,Treatment,-0.1 92 | 91,65,Treatment,0.6 93 | 92,48,Placebo,0.3 94 | 93,67,Placebo,0.2 95 | 94,49,Placebo,-0.3 96 | 95,74,Treatment,-0.5 97 | 96,29,Placebo,0.2 98 | 97,21,Placebo,0.3 99 | 98,39,Placebo,-0.5 100 | 99,47,Treatment,-0.5 101 | 100,54,Treatment,-1.2 102 | -------------------------------------------------------------------------------- /src/05 Manipulating data.jl: -------------------------------------------------------------------------------- 1 | # MANIPULATING DATA 2 | # ----------------- 3 | 4 | # INTRODUCTION 5 | 6 | # The DataFramesMeta package adds six macros 7 | # to the select, transformm, and combine functions in 8 | # the DataFrames package 9 | # It provides a convenient way to select and manipulate 10 | # data 11 | 12 | 13 | # SETUP 14 | 15 | using DataFrames, DataFramesMeta 16 | 17 | using Random, Statistics 18 | 19 | 20 | # GENERATE DATA 21 | 22 | Random.seed!(10); # Seed pseudorandom number generator for 23 | # reproducible results 24 | 25 | df = DataFrame(ID = 1:10, Var1 = rand(1:10, 10), Var2 = rand(-50:50, 10) ./ 10, Cat1 = rand(["I", "II"], 10)) 26 | # Two variables with 10 rows of data 27 | 28 | 29 | # SPLIT-APPLY-COMBINE 30 | 31 | # This is a strategy to group data, apply functions 32 | # to each group, and combine the data again 33 | # The groupyby function creates a GroupedDataFrame object 34 | # The combine, select or select! and the transform or 35 | # transform! functions can be used on a GroupedDataFrame 36 | # These functions also act of AbstractDataFrame objects (when 37 | # the groupby function is not used) 38 | 39 | 40 | # CREATING A GROUPEDDATAFRAME object 41 | 42 | gdf = groupby(df, :Cat1) # Group by sample space 43 | # elements in the Cat1 variable 44 | 45 | gdf[(Cat1 = "I", )] # View only selected subdataframe 46 | 47 | 48 | # CALCULATE A STATISTIC 49 | 50 | combine(gdf, nrow) # Number of cases per class 51 | 52 | combine(gdf, :Var1 => sum) # Sum of Var1 53 | 54 | combine(gdf, :Var1 => mean) # Mean of Var1 per 55 | # Cat1 element 56 | 57 | combine(gdf, :Var1 => mean => :mean) # Name the 58 | # new column 59 | 60 | combine(gdf, nrow, :Var1 => mean => :average) 61 | # More than one calculation 62 | 63 | combine(gdf, AsTable([:Var1, :Var2]) => 64 | x -> mean(x.Var1) / mean(x.Var2)) # More than 65 | # one variable 66 | 67 | combine(x -> mean(x.Var1) / std(x.Var1), gdf) 68 | # Function notation 69 | 70 | combine(gdf, :Var1 => (x -> [extrema(x)]) => [:min, :max]) 71 | 72 | combine(gdf) do df 73 | (μ = mean(df.Var1), σ = std(df.Var1), e = extrema(df.Var1)) 74 | end # Using a do end block 75 | 76 | 77 | # SELECT SPECIFIC DATA 78 | 79 | @select(df, :Var1) # Select only the first variable and 80 | # return a dataframe object 81 | 82 | @select(df, :Var2, Var3 = abs.(:Var2)) # Select a column 83 | # and create a new one by transforming another 84 | 85 | 86 | # TRANSFORM DATA 87 | 88 | @transform(df, Var3 = abs.(:Var2)) # Same as @select 89 | 90 | 91 | # SELECT ON CRITERIA 92 | 93 | @where(df, :Var2 .> 0) # Select only rows where Var2 is positive 94 | 95 | @where(df, :Var2 .< 0, :Cat1 .== "I") # Adding more constraints 96 | 97 | @where(df, :Var2 .> mean(:Var2)) # Selection criteria based on 98 | # a statistic 99 | 100 | -------------------------------------------------------------------------------- /src/02 DataFrame introduction.jl: -------------------------------------------------------------------------------- 1 | # DATAFRAME INTRODUCTION 2 | # -------------------- 3 | 4 | # IMPORTING DATA 5 | 6 | using CSV 7 | 8 | using DataFrames 9 | 10 | using Pipe 11 | 12 | pwd() 13 | 14 | df = CSV.File("src/generated_data.csv") |> DataFrame 15 | 16 | 17 | # VIEWING THE DATA 18 | 19 | first(df, 3) # Print first three rows 20 | 21 | last(df, 3) # Print the last three rows 22 | 23 | show(df, allrows = true) # View all rows 24 | 25 | show(df, allcols = true) # View all columns 26 | 27 | 28 | # COLUMN HEADERS (VARIABLE NAMES) 29 | 30 | names(df) 31 | 32 | 33 | # GENERATING ARRAYS FROM A COLUMN 34 | 35 | df.Age # Age array (not a copy) 36 | 37 | df."Age" # Age array (not a copy) 38 | 39 | df[!, :Age] # Age array (not a copy) 40 | 41 | df[!, "Age"] # Age array (not a copy) 42 | 43 | df[!, 2] # Age array (not a copy) 44 | 45 | df.Age === df[!, :Age] 46 | 47 | df."Age" === df[!, 2] 48 | 49 | df[:, :Age] # Age array (a copy) 50 | 51 | df[:, "Age"] # Age array (a copy) 52 | 53 | df[:, 2] # Age array (a copy) 54 | 55 | df[:, 2] === df[:, :Age] 56 | 57 | first(df, 3) 58 | 59 | age = df[:, :Age] # Array is a copy 60 | 61 | df.Age[1] = 100 # Change dataframe permanently 62 | 63 | first(df, 3) 64 | 65 | age # Not changed 66 | 67 | age = df[!, :Age] # Array is not a copy 68 | 69 | df.Age[1] = 10 # Change dataframe permanently 70 | 71 | first(df, 3) 72 | 73 | age # Changed 74 | 75 | 76 | # GENERATING A MATRIX FROM A DATAFRAME 77 | 78 | Matrix(df) 79 | 80 | 81 | # SUBSETS 82 | 83 | df[1:3, :] # First three rows of all the columns 84 | 85 | df[[1, 3, 5], :] # Rows 1, 3, and 5 of all the columns 86 | 87 | df[1:3, [:Age, :Group]] # First three rows of Age and Group columns 88 | 89 | df[[5, 3, 1], [:Group, :Age, :ID]] # Control the order 90 | 91 | df[1:3, r"Gr"] # Using regular expression 92 | 93 | df[1:3, Not(r"G")] # Omit a column 94 | 95 | 96 | # CONDITIONS 97 | 98 | df[df.Age .< 28, :] # Select only rows where age is less than 28 99 | 100 | df[40 .< df.Age .< 60, :] # Ages between 40 and 60 (exclusive) 101 | 102 | df[(df.Age .< 25) .| (df.Age .> 70), :] # Age less than 25 or greater than 70 103 | 104 | df[(df.Age .<= 25) .& (df.Group .== "Treatment"), :] 105 | # Age 25 and younger and in treatment group 106 | 107 | df[in.(df.Age, Ref([20, 30, 40, 50, 60, 70, 80])), :] 108 | # Specific Ages 109 | 110 | select(df, :Age, :Group) # Use select function to select columns (returns a dataframe object) 111 | 112 | filter(:Group => ==("Treatment"), df) # Use filter function to select specific rows 113 | 114 | filter(:Group => x -> x == "Treatment", df) # Alternative syntax 115 | 116 | filter(row -> row.Group == "Treatment", df) # Alternative syntax 117 | 118 | select(df[:, [:Age, :DeltaLDL]], AsTable(:) => ByRow(extrema) => [:lo, :hi]) 119 | 120 | 121 | # GHANGE DATA TYPE OF VARIABLE 122 | 123 | first(df, 3) # Group is a string type variable 124 | 125 | df.Group = categorical(df.Group, levels = ["Placebo", "Treatment"]) 126 | # Change group variable to a categorical variable and set the levels 127 | 128 | first(df, 3) # Now a categorical variable with levels 129 | 130 | 131 | # REPLACING DATA 132 | 133 | df.Age[[1, 3, 5]] .= 0 # Change specified ages to 0 134 | 135 | first(df, 7) 136 | 137 | replace!(df.Age, 0 => 20) # Replace with 20 138 | 139 | first(df, 7) -------------------------------------------------------------------------------- /Manifest.toml: -------------------------------------------------------------------------------- 1 | # This file is machine-generated - editing it directly is not advised 2 | 3 | [[Adapt]] 4 | deps = ["LinearAlgebra"] 5 | git-tree-sha1 = "42c42f2221906892ceb765dbcb1a51deeffd86d7" 6 | uuid = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" 7 | version = "2.3.0" 8 | 9 | [[Artifacts]] 10 | deps = ["Pkg"] 11 | git-tree-sha1 = "c30985d8821e0cd73870b17b0ed0ce6dc44cb744" 12 | uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" 13 | version = "1.3.0" 14 | 15 | [[Base64]] 16 | uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" 17 | 18 | [[BinaryProvider]] 19 | deps = ["Libdl", "Logging", "SHA"] 20 | git-tree-sha1 = "ecdec412a9abc8db54c0efc5548c64dfce072058" 21 | uuid = "b99e7846-7c00-51b0-8f62-c81ae34c0232" 22 | version = "0.5.10" 23 | 24 | [[CSV]] 25 | deps = ["Dates", "Mmap", "Parsers", "PooledArrays", "SentinelArrays", "Tables", "Unicode"] 26 | git-tree-sha1 = "290a56b2448024a1501834ee8b7d5d7004bc5ad3" 27 | uuid = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" 28 | version = "0.8.2" 29 | 30 | [[CategoricalArrays]] 31 | deps = ["DataAPI", "Future", "JSON", "Missings", "Printf", "Statistics", "StructTypes", "Unicode"] 32 | git-tree-sha1 = "5861101791fa76fafe8dddefd70ffbfe4e33ecae" 33 | uuid = "324d7699-5711-5eae-9e2f-1d82baa6b597" 34 | version = "0.9.0" 35 | 36 | [[CodecZlib]] 37 | deps = ["TranscodingStreams", "Zlib_jll"] 38 | git-tree-sha1 = "ded953804d019afa9a3f98981d99b33e3db7b6da" 39 | uuid = "944b1d66-785c-5afd-91f1-9de20f533193" 40 | version = "0.7.0" 41 | 42 | [[Combinatorics]] 43 | git-tree-sha1 = "08c8b6831dc00bfea825826be0bc8336fc369860" 44 | uuid = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" 45 | version = "1.0.2" 46 | 47 | [[Compat]] 48 | deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"] 49 | git-tree-sha1 = "919c7f3151e79ff196add81d7f4e45d91bbf420b" 50 | uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" 51 | version = "3.25.0" 52 | 53 | [[CompilerSupportLibraries_jll]] 54 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 55 | git-tree-sha1 = "8e695f735fca77e9708e795eda62afdb869cbb70" 56 | uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae" 57 | version = "0.3.4+0" 58 | 59 | [[ConstructionBase]] 60 | git-tree-sha1 = "a2a6a5fea4d6f730ec4c18a76d27ec10e8ec1c50" 61 | uuid = "187b0558-2788-49d3-abe0-74a17ed4e7c9" 62 | version = "1.0.0" 63 | 64 | [[Crayons]] 65 | git-tree-sha1 = "3f71217b538d7aaee0b69ab47d9b7724ca8afa0d" 66 | uuid = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f" 67 | version = "4.0.4" 68 | 69 | [[DataAPI]] 70 | git-tree-sha1 = "ad84f52c0b8f05aa20839484dbaf01690b41ff84" 71 | uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a" 72 | version = "1.4.0" 73 | 74 | [[DataFrames]] 75 | deps = ["CategoricalArrays", "Compat", "DataAPI", "Future", "InvertedIndices", "IteratorInterfaceExtensions", "LinearAlgebra", "Markdown", "Missings", "PooledArrays", "PrettyTables", "Printf", "REPL", "Reexport", "SortingAlgorithms", "Statistics", "TableTraits", "Tables", "Unicode"] 76 | git-tree-sha1 = "b46e1deb4592a5df7416b10dfcd6b01fb194ab9a" 77 | uuid = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" 78 | version = "0.22.2" 79 | 80 | [[DataFramesMeta]] 81 | deps = ["DataFrames", "Reexport"] 82 | git-tree-sha1 = "d2b8f08f3b84ba53321d5609a622ad9f61998a6a" 83 | uuid = "1313f7d8-7da2-5740-9ea0-a2ca25f37964" 84 | version = "0.6.0" 85 | 86 | [[DataStructures]] 87 | deps = ["InteractiveUtils", "OrderedCollections"] 88 | git-tree-sha1 = "88d48e133e6d3dd68183309877eac74393daa7eb" 89 | uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" 90 | version = "0.17.20" 91 | 92 | [[DataValueInterfaces]] 93 | git-tree-sha1 = "bfc1187b79289637fa0ef6d4436ebdfe6905cbd6" 94 | uuid = "e2d170a0-9d28-54be-80f0-106bbe20a464" 95 | version = "1.0.0" 96 | 97 | [[DataValues]] 98 | deps = ["DataValueInterfaces", "Dates"] 99 | git-tree-sha1 = "d88a19299eba280a6d062e135a43f00323ae70bf" 100 | uuid = "e7dc6d0d-1eca-5fa6-8ad6-5aecde8b7ea5" 101 | version = "0.4.13" 102 | 103 | [[Dates]] 104 | deps = ["Printf"] 105 | uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" 106 | 107 | [[DelimitedFiles]] 108 | deps = ["Mmap"] 109 | uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab" 110 | 111 | [[Distributed]] 112 | deps = ["Random", "Serialization", "Sockets"] 113 | uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" 114 | 115 | [[Distributions]] 116 | deps = ["FillArrays", "LinearAlgebra", "PDMats", "Printf", "QuadGK", "Random", "SparseArrays", "SpecialFunctions", "StaticArrays", "Statistics", "StatsBase", "StatsFuns"] 117 | git-tree-sha1 = "2cce211af0abaa01354904e2af2232a4431d8b14" 118 | uuid = "31c24e10-a181-5473-b8eb-7969acd0382f" 119 | version = "0.24.8" 120 | 121 | [[DoubleFloats]] 122 | deps = ["GenericSVD", "GenericSchur", "LinearAlgebra", "Polynomials", "Printf", "Quadmath", "Random", "Requires", "SpecialFunctions"] 123 | git-tree-sha1 = "3351fb3839b6967604f9abca62fd3c1f85875906" 124 | uuid = "497a8b3b-efae-58df-a0af-a86822472b78" 125 | version = "1.1.15" 126 | 127 | [[ExprTools]] 128 | git-tree-sha1 = "10407a39b87f29d47ebaca8edbc75d7c302ff93e" 129 | uuid = "e2ba6199-217a-4e67-a87a-7c52f15ade04" 130 | version = "0.1.3" 131 | 132 | [[EzXML]] 133 | deps = ["Printf", "XML2_jll"] 134 | git-tree-sha1 = "0fa3b52a04a4e210aeb1626def9c90df3ae65268" 135 | uuid = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615" 136 | version = "1.1.0" 137 | 138 | [[FileIO]] 139 | deps = ["Pkg"] 140 | git-tree-sha1 = "fee8955b9dfa7bec67117ef48085fb2b559b9c22" 141 | uuid = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549" 142 | version = "1.4.5" 143 | 144 | [[FilePaths]] 145 | deps = ["FilePathsBase", "Glob", "MacroTools", "Reexport", "URIParser"] 146 | git-tree-sha1 = "85507891ca01aa1e6afaa66087bb903b7e164284" 147 | uuid = "8fc22ac5-c921-52a6-82fd-178b2807b824" 148 | version = "0.8.1" 149 | 150 | [[FilePathsBase]] 151 | deps = ["Dates", "Mmap", "Printf", "Test", "UUIDs"] 152 | git-tree-sha1 = "74b340c6f78b6ee2699f8bd2f790a97f0122349f" 153 | uuid = "48062228-2e41-5def-b9a4-89aafe57970f" 154 | version = "0.9.6" 155 | 156 | [[FillArrays]] 157 | deps = ["LinearAlgebra", "Random", "SparseArrays"] 158 | git-tree-sha1 = "ff537e5a3cba92fb48f30fec46723510450f2c0e" 159 | uuid = "1a297f60-69ca-5386-bcde-b61e274b549b" 160 | version = "0.10.2" 161 | 162 | [[Formatting]] 163 | deps = ["Printf"] 164 | git-tree-sha1 = "8339d61043228fdd3eb658d86c926cb282ae72a8" 165 | uuid = "59287772-0a20-5a39-b81b-1366585eb4c0" 166 | version = "0.4.2" 167 | 168 | [[Future]] 169 | deps = ["Random"] 170 | uuid = "9fa8497b-333b-5362-9e8d-4d0656e87820" 171 | 172 | [[FuzzyCompletions]] 173 | deps = ["REPL"] 174 | git-tree-sha1 = "5ca3ddf3061771d25d1699ce53a80a39300811e3" 175 | uuid = "fb4132e2-a121-4a70-b8a1-d5b831dcdcc2" 176 | version = "0.4.0" 177 | 178 | [[GLM]] 179 | deps = ["Distributions", "LinearAlgebra", "Printf", "Random", "Reexport", "SparseArrays", "SpecialFunctions", "Statistics", "StatsBase", "StatsFuns", "StatsModels"] 180 | git-tree-sha1 = "0482be613c44fbc4620c0f06b78d8988444a0fab" 181 | uuid = "38e38edf-8417-5370-95a0-9cbb8c7f171a" 182 | version = "1.3.11" 183 | 184 | [[GenericSVD]] 185 | deps = ["LinearAlgebra"] 186 | git-tree-sha1 = "62909c3eda8a25b5673a367d1ad2392ebb265211" 187 | uuid = "01680d73-4ee2-5a08-a1aa-533608c188bb" 188 | version = "0.3.0" 189 | 190 | [[GenericSchur]] 191 | deps = ["LinearAlgebra", "Printf"] 192 | git-tree-sha1 = "43b4dc5648028be2c6e96201aa3653903bd1af21" 193 | uuid = "c145ed77-6b09-5dd9-b285-bf645a82121e" 194 | version = "0.4.0" 195 | 196 | [[Glob]] 197 | git-tree-sha1 = "4df9f7e06108728ebf00a0a11edee4b29a482bb2" 198 | uuid = "c27321d9-0574-5035-807b-f59d2c89b15c" 199 | version = "1.3.0" 200 | 201 | [[HTTP]] 202 | deps = ["Base64", "Dates", "IniFile", "MbedTLS", "Sockets"] 203 | git-tree-sha1 = "c7ec02c4c6a039a98a15f955462cd7aea5df4508" 204 | uuid = "cd3eb016-35fb-5094-929b-558a96fad6f3" 205 | version = "0.8.19" 206 | 207 | [[HypothesisTests]] 208 | deps = ["Combinatorics", "Distributions", "LinearAlgebra", "Random", "Rmath", "Roots", "Statistics", "StatsBase"] 209 | git-tree-sha1 = "552892528991c3e17eb60e623f1ac94c0663eb7d" 210 | uuid = "09f84164-cd44-5f33-b23f-e6b0d136a0d5" 211 | version = "0.10.2" 212 | 213 | [[IniFile]] 214 | deps = ["Test"] 215 | git-tree-sha1 = "098e4d2c533924c921f9f9847274f2ad89e018b8" 216 | uuid = "83e8ac13-25f8-5344-8a64-a9f2b223428f" 217 | version = "0.5.0" 218 | 219 | [[InteractiveUtils]] 220 | deps = ["Markdown"] 221 | uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" 222 | 223 | [[Intervals]] 224 | deps = ["Dates", "Printf", "RecipesBase", "Serialization", "TimeZones"] 225 | git-tree-sha1 = "323a38ed1952d30586d0fe03412cde9399d3618b" 226 | uuid = "d8418881-c3e1-53bb-8760-2df7ec849ed5" 227 | version = "1.5.0" 228 | 229 | [[InvertedIndices]] 230 | deps = ["Test"] 231 | git-tree-sha1 = "15732c475062348b0165684ffe28e85ea8396afc" 232 | uuid = "41ab1584-1d38-5bbf-9106-f11c6c58b48f" 233 | version = "1.0.0" 234 | 235 | [[IterableTables]] 236 | deps = ["DataValues", "IteratorInterfaceExtensions", "Requires", "TableTraits", "TableTraitsUtils"] 237 | git-tree-sha1 = "70300b876b2cebde43ebc0df42bc8c94a144e1b4" 238 | uuid = "1c8ee90f-4401-5389-894e-7a04a3dc0f4d" 239 | version = "1.0.0" 240 | 241 | [[IteratorInterfaceExtensions]] 242 | git-tree-sha1 = "a3f24677c21f5bbe9d2a714f95dcd58337fb2856" 243 | uuid = "82899510-4779-5014-852e-03e436cf321d" 244 | version = "1.0.0" 245 | 246 | [[JLLWrappers]] 247 | git-tree-sha1 = "c70593677bbf2c3ccab4f7500d0f4dacfff7b75c" 248 | uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" 249 | version = "1.1.3" 250 | 251 | [[JSON]] 252 | deps = ["Dates", "Mmap", "Parsers", "Unicode"] 253 | git-tree-sha1 = "81690084b6198a2e1da36fcfda16eeca9f9f24e4" 254 | uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" 255 | version = "0.21.1" 256 | 257 | [[JSONSchema]] 258 | deps = ["BinaryProvider", "HTTP", "JSON"] 259 | git-tree-sha1 = "b0a7f9328967df5213691d318a03cf70ea8c76b1" 260 | uuid = "7d188eb4-7ad8-530c-ae41-71a32a6d4692" 261 | version = "0.2.0" 262 | 263 | [[LibGit2]] 264 | deps = ["Printf"] 265 | uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" 266 | 267 | [[Libdl]] 268 | uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" 269 | 270 | [[Libiconv_jll]] 271 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 272 | git-tree-sha1 = "8e924324b2e9275a51407a4e06deb3455b1e359f" 273 | uuid = "94ce4f54-9a6c-5748-9c1c-f9c7231a4531" 274 | version = "1.16.0+7" 275 | 276 | [[LinearAlgebra]] 277 | deps = ["Libdl"] 278 | uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" 279 | 280 | [[Logging]] 281 | uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" 282 | 283 | [[MacroTools]] 284 | deps = ["Markdown", "Random"] 285 | git-tree-sha1 = "6a8a2a625ab0dea913aba95c11370589e0239ff0" 286 | uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" 287 | version = "0.5.6" 288 | 289 | [[Markdown]] 290 | deps = ["Base64"] 291 | uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" 292 | 293 | [[MbedTLS]] 294 | deps = ["Dates", "MbedTLS_jll", "Random", "Sockets"] 295 | git-tree-sha1 = "1c38e51c3d08ef2278062ebceade0e46cefc96fe" 296 | uuid = "739be429-bea8-5141-9913-cc70e7f3736d" 297 | version = "1.0.3" 298 | 299 | [[MbedTLS_jll]] 300 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 301 | git-tree-sha1 = "0eef589dd1c26a3ac9d753fe1a8bcad63f956fa6" 302 | uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" 303 | version = "2.16.8+1" 304 | 305 | [[Missings]] 306 | deps = ["DataAPI"] 307 | git-tree-sha1 = "ed61674a0864832495ffe0a7e889c0da76b0f4c8" 308 | uuid = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28" 309 | version = "0.4.4" 310 | 311 | [[Mmap]] 312 | uuid = "a63ad114-7e13-5084-954f-fe012c677804" 313 | 314 | [[Mocking]] 315 | deps = ["ExprTools"] 316 | git-tree-sha1 = "916b850daad0d46b8c71f65f719c49957e9513ed" 317 | uuid = "78c3b35d-d492-501b-9361-3d52fe80e533" 318 | version = "0.7.1" 319 | 320 | [[MsgPack]] 321 | deps = ["Serialization"] 322 | git-tree-sha1 = "a8cbf066b54d793b9a48c5daa5d586cf2b5bd43d" 323 | uuid = "99f44e22-a591-53d1-9472-aa23ef4bd671" 324 | version = "1.1.0" 325 | 326 | [[NodeJS]] 327 | deps = ["Pkg"] 328 | git-tree-sha1 = "350ac618f41958e6e0f6b0d2005ae4547eb1b503" 329 | uuid = "2bd173c7-0d6d-553b-b6af-13a54713934c" 330 | version = "1.1.1" 331 | 332 | [[Nullables]] 333 | git-tree-sha1 = "8f87854cc8f3685a60689d8edecaa29d2251979b" 334 | uuid = "4d1e1d77-625e-5b40-9113-a560ec7a8ecd" 335 | version = "1.0.0" 336 | 337 | [[OffsetArrays]] 338 | deps = ["Adapt"] 339 | git-tree-sha1 = "b0cc1c42b63e30b759f4e1cf045ad8a51069d6cc" 340 | uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" 341 | version = "1.4.2" 342 | 343 | [[OpenSpecFun_jll]] 344 | deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Pkg"] 345 | git-tree-sha1 = "9db77584158d0ab52307f8c04f8e7c08ca76b5b3" 346 | uuid = "efe28fd5-8261-553b-a9e1-b2916fc3738e" 347 | version = "0.5.3+4" 348 | 349 | [[OrderedCollections]] 350 | git-tree-sha1 = "cf59cfed2e2c12e8a2ff0a4f1e9b2cd8650da6db" 351 | uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" 352 | version = "1.3.2" 353 | 354 | [[PDMats]] 355 | deps = ["LinearAlgebra", "SparseArrays", "SuiteSparse", "Test"] 356 | git-tree-sha1 = "95a4038d1011dfdbde7cecd2ad0ac411e53ab1bc" 357 | uuid = "90014a1f-27ba-587c-ab20-58faa44d9150" 358 | version = "0.10.1" 359 | 360 | [[Parsers]] 361 | deps = ["Dates"] 362 | git-tree-sha1 = "50c9a9ed8c714945e01cd53a21007ed3865ed714" 363 | uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0" 364 | version = "1.0.15" 365 | 366 | [[Pipe]] 367 | git-tree-sha1 = "6842804e7867b115ca9de748a0cf6b364523c16d" 368 | uuid = "b98c9c47-44ae-5843-9183-064241ee97a0" 369 | version = "1.3.0" 370 | 371 | [[Pkg]] 372 | deps = ["Dates", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "UUIDs"] 373 | uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" 374 | 375 | [[Pluto]] 376 | deps = ["Base64", "Dates", "Distributed", "FuzzyCompletions", "HTTP", "InteractiveUtils", "Logging", "Markdown", "MsgPack", "Pkg", "REPL", "Sockets", "Tables", "UUIDs"] 377 | git-tree-sha1 = "9b6f46fb347ace8d24ca705f5e42d9c3b10843f8" 378 | uuid = "c3e4b0f8-55cb-11ea-2926-15256bba5781" 379 | version = "0.12.18" 380 | 381 | [[PlutoUI]] 382 | deps = ["Base64", "Dates", "Logging", "Markdown", "Random", "Suppressor"] 383 | git-tree-sha1 = "1b58f5a202953a2ef4dc8623e55fcc55c0f03c33" 384 | uuid = "7f904dfe-b85e-4ff6-b463-dae2292396a8" 385 | version = "0.6.10" 386 | 387 | [[Polynomials]] 388 | deps = ["Intervals", "LinearAlgebra", "OffsetArrays", "RecipesBase"] 389 | git-tree-sha1 = "1c6c5b0c3713738d6b987903c529d80622c37e07" 390 | uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45" 391 | version = "1.2.0" 392 | 393 | [[PooledArrays]] 394 | deps = ["DataAPI"] 395 | git-tree-sha1 = "b1333d4eced1826e15adbdf01a4ecaccca9d353c" 396 | uuid = "2dfb63ee-cc39-5dd5-95bd-886bf059d720" 397 | version = "0.5.3" 398 | 399 | [[PrettyTables]] 400 | deps = ["Crayons", "Formatting", "Markdown", "Reexport", "Tables"] 401 | git-tree-sha1 = "237170206bf38a66fee4d845f4ae57f63788eeb0" 402 | uuid = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d" 403 | version = "0.10.1" 404 | 405 | [[Printf]] 406 | deps = ["Unicode"] 407 | uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" 408 | 409 | [[QuadGK]] 410 | deps = ["DataStructures", "LinearAlgebra"] 411 | git-tree-sha1 = "12fbe86da16df6679be7521dfb39fbc861e1dc7b" 412 | uuid = "1fd47b50-473d-5c70-9696-f719f8f3bcdc" 413 | version = "2.4.1" 414 | 415 | [[Quadmath]] 416 | deps = ["Printf", "Random", "Requires"] 417 | git-tree-sha1 = "cd993c45147a8432bf24358f14bf2cfd4aeb14df" 418 | uuid = "be4d8f0f-7fa4-5f49-b795-2f01399ab2dd" 419 | version = "0.5.4" 420 | 421 | [[Query]] 422 | deps = ["DataValues", "IterableTables", "MacroTools", "QueryOperators", "Statistics"] 423 | git-tree-sha1 = "a66aa7ca6f5c29f0e303ccef5c8bd55067df9bbe" 424 | uuid = "1a8c2f83-1ff3-5112-b086-8aa67b057ba1" 425 | version = "1.0.0" 426 | 427 | [[QueryOperators]] 428 | deps = ["DataStructures", "DataValues", "IteratorInterfaceExtensions", "TableShowUtils"] 429 | git-tree-sha1 = "c233ed156b7628ea5a730c3b856b471a90b58afd" 430 | uuid = "2aef5ad7-51ca-5a8f-8e88-e75cf067b44b" 431 | version = "0.9.2" 432 | 433 | [[REPL]] 434 | deps = ["InteractiveUtils", "Markdown", "Sockets"] 435 | uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" 436 | 437 | [[Random]] 438 | deps = ["Serialization"] 439 | uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" 440 | 441 | [[RecipesBase]] 442 | git-tree-sha1 = "b3fb709f3c97bfc6e948be68beeecb55a0b340ae" 443 | uuid = "3cdcf5f2-1ef4-517c-9805-6587b60abb01" 444 | version = "1.1.1" 445 | 446 | [[Reexport]] 447 | deps = ["Pkg"] 448 | git-tree-sha1 = "7b1d07f411bc8ddb7977ec7f377b97b158514fe0" 449 | uuid = "189a3867-3050-52da-a836-e630ba90ab69" 450 | version = "0.2.0" 451 | 452 | [[Requires]] 453 | deps = ["UUIDs"] 454 | git-tree-sha1 = "cfbac6c1ed70c002ec6361e7fd334f02820d6419" 455 | uuid = "ae029012-a4dd-5104-9daa-d747884805df" 456 | version = "1.1.2" 457 | 458 | [[Rmath]] 459 | deps = ["Random", "Rmath_jll"] 460 | git-tree-sha1 = "86c5647b565873641538d8f812c04e4c9dbeb370" 461 | uuid = "79098fc4-a85e-5d69-aa6a-4863f24498fa" 462 | version = "0.6.1" 463 | 464 | [[Rmath_jll]] 465 | deps = ["Libdl", "Pkg"] 466 | git-tree-sha1 = "d76185aa1f421306dec73c057aa384bad74188f0" 467 | uuid = "f50d1b31-88e8-58de-be2c-1cc44531875f" 468 | version = "0.2.2+1" 469 | 470 | [[Roots]] 471 | deps = ["Printf"] 472 | git-tree-sha1 = "8f743e4f4368d1d753f3806bf635899dad6b4847" 473 | uuid = "f2b01f46-fcfa-551c-844a-d8ac1e96c665" 474 | version = "1.0.7" 475 | 476 | [[SHA]] 477 | uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" 478 | 479 | [[SentinelArrays]] 480 | deps = ["Dates", "Random"] 481 | git-tree-sha1 = "6ccde405cf0759eba835eb613130723cb8f10ff9" 482 | uuid = "91c51154-3ec4-41a3-a24f-3f23e20d615c" 483 | version = "1.2.16" 484 | 485 | [[Serialization]] 486 | uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" 487 | 488 | [[Setfield]] 489 | deps = ["ConstructionBase", "Future", "MacroTools", "Requires"] 490 | git-tree-sha1 = "7a151f918819326a6003dba451dabe65f8c0f6fb" 491 | uuid = "efcf1570-3423-57d1-acb7-fd33fddbac46" 492 | version = "0.6.0" 493 | 494 | [[SharedArrays]] 495 | deps = ["Distributed", "Mmap", "Random", "Serialization"] 496 | uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383" 497 | 498 | [[ShiftedArrays]] 499 | git-tree-sha1 = "22395afdcf37d6709a5a0766cc4a5ca52cb85ea0" 500 | uuid = "1277b4bf-5013-50f5-be3d-901d8477a67a" 501 | version = "1.0.0" 502 | 503 | [[Sockets]] 504 | uuid = "6462fe0b-24de-5631-8697-dd941f90decc" 505 | 506 | [[SortingAlgorithms]] 507 | deps = ["DataStructures", "Random", "Test"] 508 | git-tree-sha1 = "03f5898c9959f8115e30bc7226ada7d0df554ddd" 509 | uuid = "a2af1166-a08f-5f64-846c-94a0d3cef48c" 510 | version = "0.3.1" 511 | 512 | [[SparseArrays]] 513 | deps = ["LinearAlgebra", "Random"] 514 | uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" 515 | 516 | [[SpecialFunctions]] 517 | deps = ["OpenSpecFun_jll"] 518 | git-tree-sha1 = "d8d8b8a9f4119829410ecd706da4cc8594a1e020" 519 | uuid = "276daf66-3868-5448-9aa4-cd146d93841b" 520 | version = "0.10.3" 521 | 522 | [[StaticArrays]] 523 | deps = ["LinearAlgebra", "Random", "Statistics"] 524 | git-tree-sha1 = "9da72ed50e94dbff92036da395275ed114e04d49" 525 | uuid = "90137ffa-7385-5640-81b9-e52037218182" 526 | version = "1.0.1" 527 | 528 | [[Statistics]] 529 | deps = ["LinearAlgebra", "SparseArrays"] 530 | uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" 531 | 532 | [[StatsBase]] 533 | deps = ["DataAPI", "DataStructures", "LinearAlgebra", "Missings", "Printf", "Random", "SortingAlgorithms", "SparseArrays", "Statistics"] 534 | git-tree-sha1 = "7bab7d4eb46b225b35179632852b595a3162cb61" 535 | uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" 536 | version = "0.33.2" 537 | 538 | [[StatsFuns]] 539 | deps = ["Rmath", "SpecialFunctions"] 540 | git-tree-sha1 = "3b9f665c70712af3264b61c27a7e1d62055dafd1" 541 | uuid = "4c63d2b9-4356-54db-8cca-17b64c39e42c" 542 | version = "0.9.6" 543 | 544 | [[StatsModels]] 545 | deps = ["DataAPI", "DataStructures", "LinearAlgebra", "Printf", "ShiftedArrays", "SparseArrays", "StatsBase", "StatsFuns", "Tables"] 546 | git-tree-sha1 = "74b80efa9e77f800d75707fc00a5d8cae2ce58a5" 547 | uuid = "3eaba693-59b7-5ba5-a881-562e759f1c8d" 548 | version = "0.6.16" 549 | 550 | [[StructTypes]] 551 | deps = ["Dates", "UUIDs"] 552 | git-tree-sha1 = "d94235fcdc4a09649f263365c5f7e4ed4ba6ed34" 553 | uuid = "856f2bd8-1eba-4b0a-8007-ebc267875bd4" 554 | version = "1.2.1" 555 | 556 | [[SuiteSparse]] 557 | deps = ["Libdl", "LinearAlgebra", "Serialization", "SparseArrays"] 558 | uuid = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9" 559 | 560 | [[Suppressor]] 561 | git-tree-sha1 = "a819d77f31f83e5792a76081eee1ea6342ab8787" 562 | uuid = "fd094767-a336-5f1f-9728-57cf17d0bbfb" 563 | version = "0.2.0" 564 | 565 | [[TableShowUtils]] 566 | deps = ["DataValues", "Dates", "JSON", "Markdown", "Test"] 567 | git-tree-sha1 = "14c54e1e96431fb87f0d2f5983f090f1b9d06457" 568 | uuid = "5e66a065-1f0a-5976-b372-e0b8c017ca10" 569 | version = "0.2.5" 570 | 571 | [[TableTraits]] 572 | deps = ["IteratorInterfaceExtensions"] 573 | git-tree-sha1 = "b1ad568ba658d8cbb3b892ed5380a6f3e781a81e" 574 | uuid = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c" 575 | version = "1.0.0" 576 | 577 | [[TableTraitsUtils]] 578 | deps = ["DataValues", "IteratorInterfaceExtensions", "Missings", "TableTraits"] 579 | git-tree-sha1 = "8fc12ae66deac83e44454e61b02c37b326493233" 580 | uuid = "382cd787-c1b6-5bf2-a167-d5b971a19bda" 581 | version = "1.0.1" 582 | 583 | [[Tables]] 584 | deps = ["DataAPI", "DataValueInterfaces", "IteratorInterfaceExtensions", "LinearAlgebra", "TableTraits", "Test"] 585 | git-tree-sha1 = "240d19b8762006ff04b967bdd833269ad642d550" 586 | uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" 587 | version = "1.2.2" 588 | 589 | [[Test]] 590 | deps = ["Distributed", "InteractiveUtils", "Logging", "Random"] 591 | uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" 592 | 593 | [[TextParse]] 594 | deps = ["CodecZlib", "DataStructures", "Dates", "DoubleFloats", "Mmap", "Nullables", "WeakRefStrings"] 595 | git-tree-sha1 = "af728c38c839aee693637e15e244074a02f16c68" 596 | uuid = "e0df1984-e451-5cb5-8b61-797a481e67e3" 597 | version = "1.0.1" 598 | 599 | [[TimeZones]] 600 | deps = ["Dates", "EzXML", "Mocking", "Pkg", "Printf", "RecipesBase", "Serialization", "Unicode"] 601 | git-tree-sha1 = "4ba8a9579a243400db412b50300cd61d7447e583" 602 | uuid = "f269a46b-ccf7-5d73-abea-4c690281aa53" 603 | version = "1.5.3" 604 | 605 | [[TranscodingStreams]] 606 | deps = ["Random", "Test"] 607 | git-tree-sha1 = "7c53c35547de1c5b9d46a4797cf6d8253807108c" 608 | uuid = "3bb67fe8-82b1-5028-8e26-92a6c54297fa" 609 | version = "0.9.5" 610 | 611 | [[URIParser]] 612 | deps = ["Unicode"] 613 | git-tree-sha1 = "53a9f49546b8d2dd2e688d216421d050c9a31d0d" 614 | uuid = "30578b45-9adc-5946-b283-645ec420af67" 615 | version = "0.4.1" 616 | 617 | [[UUIDs]] 618 | deps = ["Random", "SHA"] 619 | uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" 620 | 621 | [[Unicode]] 622 | uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" 623 | 624 | [[Vega]] 625 | deps = ["DataStructures", "DataValues", "Dates", "FileIO", "FilePaths", "IteratorInterfaceExtensions", "JSON", "JSONSchema", "MacroTools", "NodeJS", "Pkg", "REPL", "Random", "Setfield", "TableTraits", "TableTraitsUtils", "URIParser"] 626 | git-tree-sha1 = "ea6d7d5ee93fce89c352fe4cd31d6d2a57ef312b" 627 | uuid = "239c3e63-733f-47ad-beb7-a12fde22c578" 628 | version = "2.0.0" 629 | 630 | [[VegaDatasets]] 631 | deps = ["DataStructures", "DataValues", "FilePaths", "IterableTables", "IteratorInterfaceExtensions", "JSON", "TableShowUtils", "TableTraits", "TableTraitsUtils", "TextParse"] 632 | git-tree-sha1 = "e18b77c433c5607dcc35f839a118e7735cc4fa57" 633 | uuid = "0ae4a718-28b7-58ec-9efb-cded64d6d5b4" 634 | version = "2.1.0" 635 | 636 | [[VegaLite]] 637 | deps = ["Base64", "DataStructures", "DataValues", "Dates", "FileIO", "FilePaths", "IteratorInterfaceExtensions", "JSON", "JSONSchema", "MacroTools", "NodeJS", "Pkg", "REPL", "Random", "Setfield", "TableTraits", "TableTraitsUtils", "URIParser", "Vega"] 638 | git-tree-sha1 = "57634eda07c4a48f543c7e8701eb6f4ee09ba2fa" 639 | uuid = "112f6efa-9a02-5b7d-90c0-432ed331239a" 640 | version = "2.3.0" 641 | 642 | [[WeakRefStrings]] 643 | deps = ["DataAPI", "Random", "Test"] 644 | git-tree-sha1 = "28807f85197eaad3cbd2330386fac1dcb9e7e11d" 645 | uuid = "ea10d353-3f73-51f8-a26c-33c1cb351aa5" 646 | version = "0.6.2" 647 | 648 | [[XML2_jll]] 649 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Libiconv_jll", "Pkg", "Zlib_jll"] 650 | git-tree-sha1 = "be0db24f70aae7e2b89f2f3092e93b8606d659a6" 651 | uuid = "02c8fc9c-b97f-50b9-bbe4-9be30ff0a78a" 652 | version = "2.9.10+3" 653 | 654 | [[Zlib_jll]] 655 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 656 | git-tree-sha1 = "320228915c8debb12cb434c59057290f0834dbf6" 657 | uuid = "83775a58-1f1d-513f-b197-d71354ab007a" 658 | version = "1.2.11+18" 659 | -------------------------------------------------------------------------------- /src/00 Introduction to Julia.jl: -------------------------------------------------------------------------------- 1 | # Preceding a line of code with a hash symbol 2 | # tells the Julia compiler to ignore it. Use 3 | # it to comment your code for future use and 4 | # as guide to other reading your code. 5 | 6 | # JULIA THE BIG CALCULATOR 7 | # ------------------------ 8 | 9 | # Basic arithmetic 10 | # ---------------- 11 | 12 | # Julia is a language for scientic computing. It 13 | # can do all sorts of mathematical operations. 14 | # In this section, we look at basic arithmetic. 15 | 16 | # Addition 17 | 2 + 2 18 | 19 | 2 + 2 + 2 20 | 21 | # Subtraction 22 | 2 - 3 23 | 24 | 2 + (-3) 25 | 26 | # Multiplication 27 | 3 * 2 28 | 29 | 3 * 2 * 2 30 | 31 | # Division 32 | 10 / 2 33 | 34 | 10 / 3 35 | 36 | # Left division 37 | 3 \ 7 38 | 7 / 3 39 | 40 | # Rational number 41 | 10 // 3 42 | 43 | # Power 44 | # The symbol for power is ^ (SHIFT and 6 on most 45 | # keyboards). 46 | 3^2 47 | 48 | # Built in Julia functions for mathematics 49 | # ---------------------------------------- 50 | 51 | # Julia is a functional language. A function 52 | # is a keyword (reserved by the language). Given 53 | # an input, called arguments (with values) or 54 | # parameters, they perform a funtion. Even 55 | # 2 + 2 is actual showhard for the +() 56 | # function. 57 | +(2, 2) 58 | # Above, we passed two parameters to the +() 59 | # function. Note that the parameters are 60 | # separated by commas. Functions are followed 61 | # directly by a set of parentheses, within 62 | # which the parameters are passed. 63 | 64 | # Truncated integer division 65 | 9 ÷ 4 66 | ÷(9, 4) 67 | 68 | # Multiplicative inverse 69 | inv(4) 70 | 71 | # Floor division 72 | fld(7, 3) 73 | fld(-11, 3) 74 | fld(11, -3) 75 | 76 | # Modulus 77 | # Remmber that the modulus and remainder 78 | # mathematical operations differ when it 79 | # comes to negative numbers. For the 80 | # modulo operation, the sign of the 81 | # result is the same as the divisor. 82 | # For the remainder operation, it is the 83 | # same as the dividend 84 | mod(11, 3) 85 | mod(3, 11) 86 | mod(-11, 3) 87 | mod(11, -3) 88 | mod(7, 3) 89 | 90 | # Remainder 91 | # 3 divides 7 twice, with a remainder 92 | # of 1 93 | 7 / 3 94 | rem(7, 3) 95 | %(7, 3) 96 | 7 % 3 97 | # Remainder take the sign of the dividend 98 | 11 % -3 99 | -11 % 3 100 | 101 | # Quotient and remainder 102 | divrem(11, 3) 103 | 104 | # Rationalize 105 | 10 // 4 106 | 10 / 4 107 | rationalize(2.5) 108 | 109 | # Fractional and integer part of a number 110 | modf(10.5) 111 | modf(-10.5) 112 | 113 | # Factorial 114 | factorial(4) 115 | 116 | # Greatest common divisior 117 | gcd(6, 9) 118 | 119 | # Square root 120 | sqrt(100) 121 | √9 122 | 123 | # Cube root 124 | cbrt(27) 125 | 126 | # Exponent 127 | # The exponent is a power with Euler's 128 | # number as the base 129 | exp(1.0) 130 | # We can use exp10() for a base of 10 131 | exp10(2) 132 | 133 | # Logarithm 134 | # The log() function takes the natural logarithm. 135 | log(exp(1)) 136 | 137 | # The log10() function has 10 as its base. 138 | log10(1000) 139 | 140 | # The log2() function has 2 as its base 141 | log2(8) 142 | 143 | # We can specify the base using the log() 144 | # function). 145 | log(10, 100) 146 | 147 | # All the trigonometric functions are 148 | # available 149 | sin(π / 6) 150 | sind(30) 151 | cos(π / 3) 152 | cosd(60) 153 | tan(π / 4) 154 | csc(π / 6) 155 | sec(π / 3) 156 | cot(π / 4) 157 | asin(0.5) 158 | rad2deg(π / 6) 159 | asind(0.5) 160 | sinh(2) 161 | 162 | # Hypoteneuse 163 | hypot(3, 4) 164 | 165 | # Rounding to digits or significant 166 | # numbers 167 | println(round(π; digits = 10)) 168 | println(round(10.23534; sigdigits = 3)) 169 | 170 | # Absolute value 171 | abs(-3) 172 | 173 | # The imaginary number 174 | 2 + 4im 175 | # For imaginary numbers, this will be 176 | # the norm (length of vector in 177 | # Argand plane). 178 | abs(3 + 4im) 179 | real(3 + 4im) 180 | imag(3 + 4im) 181 | reim(3 + 4im) 182 | conj(3 + 4im) 183 | rad2deg(angle(1 + im)) 184 | 185 | 186 | # OPERATORS 187 | # --------- 188 | 189 | # Introduction 190 | # ------------ 191 | 192 | # We have already seen the mathematical 193 | # operators +, -, *, /, \, %, etc. in 194 | # action. 195 | # The most common operator in Julia is 196 | # the assignment operator, =. It assigns 197 | # what is to its right to what is to its 198 | # left. 199 | # We put a computer variable name on 200 | # the left of the assignment operator. A 201 | # computer variable is a name chosen by 202 | # us. It is the name that references 203 | # a section of computer memory to which 204 | # the assigment operator assigns an object. 205 | # Below, we assign the value 3 to the 206 | # computer variable x. 207 | x = 3 208 | 209 | # The 3 is an object, now assigned to 210 | # a small piece of computer memory called 211 | # x. This object is an instance of a Julia 212 | # type. Everything in Julia is, or has, 213 | # a type. 214 | 215 | # Assignment operators 216 | 217 | # We have seen the = assignment operator. 218 | # There are numerous others 219 | 220 | # The += operator is equivalent to 221 | # x = x + 1 222 | x += 1 223 | 224 | # The -= operator 225 | x -= 1 226 | 227 | # The *= operator 228 | x *= 2 229 | 230 | # The /= operator 231 | x /= 2 232 | 233 | # The \= operator 234 | x \= 6 235 | 236 | # The ^= operator 237 | x ^= 3 238 | 239 | 240 | # Logical operators 241 | # ----------------- 242 | 243 | # The logical operators are &&, ||, and !. 244 | # The logical operators enables 245 | # Boolean logical where we have the 246 | # true and false keywords. 247 | true 248 | 249 | false 250 | 251 | # The && operator signifies logical AND 252 | true && true 253 | 254 | true && false 255 | 256 | false && true 257 | 258 | false && false 259 | 260 | # The || operator is the logical OR 261 | true || true 262 | 263 | true || false 264 | 265 | false || true 266 | 267 | false || false 268 | 269 | # The ! operator is logical NOT 270 | !true 271 | 272 | !false 273 | 274 | 275 | # Relational operators 276 | 277 | # These operators compare values 278 | 279 | # Less than 280 | 3 < 4 281 | 282 | # false is represented as 0 and true as 1 283 | false < true 284 | 285 | # Greater than 286 | 3 > 4 287 | 288 | # Greater than or equal to 289 | 8 >= 8 290 | 8 ≥ 8 291 | 292 | # Is equal to 293 | 3 == 3 294 | 295 | 3 == 3. 296 | 297 | # Value and type equality 298 | 3 === 3. 299 | 300 | # Not equal to 301 | 3 != 4 302 | 3 ≠ 4 303 | 304 | 305 | # STRINGS 306 | # ------- 307 | 308 | # Characters 309 | # ---------- 310 | 311 | # Characters are denoted by single 312 | # quotationmarks. 313 | 'x' 314 | 315 | typeof('x') 316 | 317 | # Type 'x' in the REPL and note that it 318 | # is an ASCII/Unicode character (a 32-bit) 319 | # primitive type). 320 | # It is therefor represented by an integer. 321 | Int('x') 322 | 323 | Char(120) 324 | 325 | # This allows for comparions and 326 | # arithmetic. 327 | Int('A') 328 | 329 | Int('a') 330 | 331 | Int('A') < Int('a') 332 | 333 | Int('A') + Int('a') 334 | 335 | # New lines and tabs 336 | Int('\n') 337 | 338 | Int('\t') 339 | 340 | # Double quotation marks signifies strings 341 | typeof("a") 342 | 343 | # Strings 344 | # ------- 345 | 346 | # Strings are denoted by a pair of 347 | # double quotations or a set of three 348 | # double quotes. 349 | "I love Julia." 350 | 351 | "I love 'Julia'." 352 | 353 | println("""I love "Julia".""") 354 | 355 | # Escape with a backslash 356 | println("I love \"Julia\".") 357 | 358 | typeof("I love Julia.") 359 | 360 | # Using new lines 361 | println("I love Julia.\nIt is a great language.") 362 | 363 | # Using a tab 364 | println("The total is:\t100") 365 | 366 | # String indexing 367 | # --------------- 368 | 369 | # Each character in a string is indexed 370 | # (starting at 1) 371 | "I love Julia."[1:6] 372 | 373 | "I love Julia."[8:12] 374 | 375 | # This is the same as a Substring 376 | SubString("I love Julia.", 8, 12) 377 | 378 | # String operators 379 | # ---------------- 380 | 381 | # Concatenation 382 | "Hello" * "Julia" 383 | println("Hello", "Julia") 384 | "Hello " * "Julia" 385 | ("Hello" * ' ') * "Julia" # Grouping concatenation if more than two 386 | 387 | # Repeats 388 | "Julia"^3 389 | repeat("Julia", 3) 390 | 391 | 392 | # String-related function 393 | # ----------------------- 394 | 395 | # Remove the last character 396 | chop("Julia.") 397 | 398 | # Checks last character 399 | endswith("Julia", "a") 400 | 401 | # First index value of specified character 402 | findfirst("l", "I love Julia.") 403 | 404 | # First n characters 405 | first("I love Julia", 6) 406 | 407 | # Last n characters 408 | last("I love Julia.", 6) 409 | 410 | # Number of characters 411 | length("I love Julia.") 412 | 413 | # Convert to lowercase 414 | lowercase("I love Julia.") 415 | 416 | # Convert to uppercase 417 | uppercase("I love Julia.") 418 | 419 | # First character uppercase 420 | uppercasefirst("i love Julia.") 421 | 422 | # String interpolation 423 | # -------------------- 424 | 425 | # Perform mathematical functions in 426 | # a string 427 | "3 times 4 is $(3 * 4)." 428 | 429 | x = 3 * 4; 430 | 431 | "3 × 4 = $x" 432 | 433 | 434 | # JULIA TYPE SYSTEM 435 | # ----------------- 436 | 437 | # Introduction 438 | # ------------ 439 | 440 | # Everything in Juia is a type. Even 441 | # as simple number such as 3. 442 | 443 | # The typeof() function returns the type 444 | # of an object. 445 | typeof(3) 446 | 447 | # Examples of types 448 | # ----------------- 449 | 450 | # We saw that 3 is a 64-bit integer. 451 | 452 | # We can assign this object to a 453 | # variable. 454 | 455 | x = 3 456 | 457 | # Since this object was assigned to 458 | # the computer variable x (and it is 459 | # a single element), x has the same 460 | # type. 461 | typeof(x) 462 | 463 | # There is a whole lot of types 464 | # in Julia. 465 | typeof(3.) 466 | typeof(1 + im) 467 | typeof(1. + im) 468 | typeof("Julia") 469 | typeof('J') 470 | 471 | (1 + 2)::Int64 472 | 473 | # Types are hierarchical and follow 474 | # a tree-like structure. 475 | 476 | # Type hierarchy 477 | # -------------- 478 | 479 | # All types in Julia are a subtype 480 | # of the type Any. Right at the 481 | # bottom of the tree we have abstract 482 | # types. They can be instantiated. We 483 | # saw some examples above. 484 | 485 | # The Int64 type is an abstract type. 486 | # Below, we investigate its parent 487 | # types using the supertype() function. 488 | 489 | supertype(Int64) 490 | 491 | supertype(Signed) 492 | 493 | supertype(Integer) 494 | 495 | supertype(Real) 496 | 497 | supertype(Number) 498 | 499 | # All of the types yp the hierarchy 500 | # are called abstract types and they 501 | # cannot be instantiated. 502 | 503 | # We can look at where the Float64 504 | # abstract type falls in the type 505 | # hierarchy. 506 | supertype(Float64) 507 | 508 | supertype(AbstractFloat) 509 | 510 | # It joins up with the Int64 abstract 511 | # type as a Real. 512 | 513 | # We can also look at the child types 514 | # down the hierarchy. 515 | subtypes(Real) 516 | 517 | subtypes(Rational) 518 | 519 | # We note that Rational is a concrete 520 | # type. 521 | 522 | 523 | # FUNCTIONS 524 | # --------- 525 | 526 | # Introduction 527 | # ------------ 528 | 529 | # While Julia has a tremendous number of 530 | # functions, we can create our own using 531 | # the function keyword. 532 | 533 | # Creating functions 534 | # ------------------ 535 | 536 | # A function consists of the function 537 | # keyword, a chosen function name, and 538 | # some arguments 539 | function addition(x, y) 540 | return x + y 541 | end 542 | 543 | addition(3, 4) 544 | 545 | # The return keyword is not strictly required. 546 | function addition(x, y) 547 | x + y 548 | end 549 | 550 | addition(4, 5) 551 | 552 | # Only the last evaluation is returned by the function. 553 | 554 | function addition(x, y) 555 | x * y 556 | x % y 557 | x + y 558 | end 559 | 560 | addition(5, 6) 561 | 562 | # When the return keyword is used, the function 563 | # immediately returns that value. 564 | 565 | function addition(x, y) 566 | return x + y 567 | x * y 568 | end 569 | 570 | addition(10, 20) 571 | 572 | # We can specify the type of the 573 | # arguments 574 | function integer_addition(x::Int, y::Int) 575 | return x + y 576 | end 577 | 578 | integer_addition(3, 4) 579 | 580 | # The return type can also be specified. 581 | function multiplication(x, y)::Int64 582 | return x * y 583 | end 584 | 585 | multiplication(3., 4) 586 | 587 | typeof(multiplication(3., 4)) 588 | 589 | # A function can return multiple values and 590 | # does so in the form of a tuple (see 591 | # collections later). 592 | 593 | function add_mult(x, y) 594 | return x + y, x * y 595 | end 596 | 597 | add_mult(3, 4) 598 | 599 | typeof(add_mult(3, 4)) 600 | 601 | # Each element of a tuple can be named 602 | # (assigned to a computer variable). 603 | a, b = add_mult(3, 4) 604 | 605 | # The variables a and b now hold the 606 | # individual elements of the tuple. 607 | a 608 | 609 | b 610 | 611 | # A shorter syntax is available. 612 | short_addition(x, y) = x + y 613 | 614 | short_addition(3, 4) 615 | 616 | # We can use Unicode function names. 617 | ∏(x, y) = x * y 618 | 619 | ∏(3, 4) 620 | 621 | # Anomymous function 622 | # ------------------ 623 | 624 | # The map() function maps the second 625 | # argument to the anonymous function in 626 | # the first argument. 627 | map(x -> x^2, 4) 628 | 629 | 630 | # Arguments 631 | # --------- 632 | 633 | # Arguments are positional. 634 | divide(x, y) = x / y 635 | 636 | divide(10, 2) 637 | 638 | # Default arguments 639 | divide(x = 10, y = 5) = x / y 640 | 641 | # A call without arguments will use the 642 | # defaults of the positional 643 | # arguments. 644 | divide() 645 | 646 | # The default values can be overwritten 647 | divide(20, 4) 648 | 649 | # Keyword arguments 650 | 651 | # These are placed after a semi-colon. 652 | # Default values can be passed. A name 653 | # and value must specified (order of named 654 | # arguments does not matter) to overwrite 655 | # the default values. 656 | 657 | multiply(x, y; a = 3, b = 4) = x * y * a * b 658 | 659 | multiply(3, 4) 660 | 661 | multiply(3, 4; b = 2, a = 1) 662 | 663 | # Piping 664 | 665 | # Piping allows us to construct a 666 | # chain of functions. 667 | 668 | # The unit range is an iterable. 669 | 1:5 670 | 671 | typeof(1:5) 672 | 673 | # We can pipe one operation into the 674 | # next. 675 | 1:5 |> sum |> √ 676 | 677 | # This is the same as below. 678 | √(1 + 2 + 3 + 4 + 5) 679 | 680 | # We can also compose functions. 681 | (sqrt ∘ +)(1, 2, 3, 4, 5) 682 | (sqrt ∘ sum)(1:5) 683 | 684 | 685 | # Variable scope 686 | # -------------- 687 | 688 | # The scope of a variable is its 689 | # visibility within our code. This 690 | # allows us to use the same variable 691 | # name, whilst it holds different values 692 | # and performs different tasks. 693 | 694 | # Setting a variable to a global scope 695 | x = 3; 696 | 697 | # Defining a function and stipulating 698 | # that the same variable, x, be used 699 | # in local scope. 700 | function local_scope(p, q) 701 | local x = p * q 702 | return x 703 | end 704 | 705 | local_scope(3, 4) 706 | 707 | # Using x in the global scope. 708 | x 709 | 710 | # Below, we create a function where 711 | # x is used in the global scope. 712 | 713 | function global_scope(p, q) 714 | global x = p * q 715 | return x 716 | end 717 | 718 | global_scope(3, 4) 719 | 720 | # The global x has now changed. 721 | x 722 | 723 | # To optimize code, global variables 724 | # can be expressed as constants. Their 725 | # type can then not be changed. 726 | 727 | const my_constant = 3 728 | 729 | # The constant variable value can 730 | # be reassigned, but not its type. 731 | my_constant = 4 732 | 733 | # try-catch 734 | # --------- 735 | 736 | # The try and ctach statements help us 737 | # catch errors. Below, we create a 738 | # function that takes the square root 739 | # of an argument. If the square root 740 | # cannot be taken, a message to the 741 | # effect is printed. 742 | function square_root(x) 743 | try 744 | sqrt(x) 745 | catch 746 | println("The square root cannot be taken.") 747 | end 748 | end 749 | 750 | square_root(9) 751 | 752 | square_root(-9) 753 | 754 | square_root("9") 755 | 756 | 757 | # Methods 758 | # ------- 759 | 760 | my_number_addition(x::Number, y::Number) = x + y 761 | 762 | my_number_addition(3., 4) 763 | 764 | addition(x::T, y::T) where {T} = Float = x + y 765 | 766 | addition(3, 4) 767 | 768 | methods(addition) 769 | 770 | addition(x::Number, y::Number) = x + y 771 | 772 | methods(addition) 773 | 774 | 775 | # FLOW CONTROL 776 | # ------------ 777 | 778 | # Introduction 779 | # ------------ 780 | 781 | # The control of flow allows us to 782 | # execute code based on conditionals. 783 | 784 | # Compound expressions 785 | # -------------------- 786 | 787 | # Compound expressions allow us to 788 | # evaluate code in order. 789 | answer = begin 790 | x = 3 791 | y = 2 792 | x + y 793 | end 794 | 795 | answer 796 | 797 | # There are alternative forms of this 798 | # syntax. 799 | answer = (x = 4; y = 2; x + y) 800 | 801 | answer 802 | 803 | answer = begin x = 2; y = 2; x + y end 804 | 805 | # if-elseif-else statements 806 | # ------------------------- 807 | 808 | # These statements allow us to perform 809 | # conditional check and execute selected 810 | # code accordingly. 811 | if 3 > 4 812 | println("Greater") 813 | elseif 3 == 4 814 | println("Equal") 815 | else 816 | println("Less") 817 | end 818 | 819 | # If-elseif-else statements are often used 820 | # in functions. 821 | function compare(x, y) 822 | if x == y 823 | print("Same") 824 | elseif x > y 825 | print("Greater") 826 | else 827 | print("Less") 828 | end 829 | end 830 | 831 | compare(3, 4) 832 | 833 | # Below, we create a function that iterates 834 | # from 1 through 20 using a unit range. We 835 | # set a divisor and iterate over the unit 836 | # range. If the unit value at each iteration 837 | # is divisible by the divisor, we print the 838 | # value, a tab, and the text Bar. If it is not, 839 | # we print the value, a tab, and the text 840 | # Foo. 841 | function foobar(m) 842 | for i = 1:20 843 | if i % m == 0 844 | print(i, "\tBar\n") 845 | else 846 | print(i, "\tFoo\n") 847 | end 848 | end 849 | end 850 | 851 | foobar(3) 852 | 853 | # The ternary operator is a form of 854 | # if-else statement. 855 | 6 % 3 == 0 ? "Bar" : "Foo" 856 | 857 | function shorter_FooBar(m) 858 | for i = 1:20 859 | i % m == 0 ? println(i, "\tBar") : println(i, "\tFoo") 860 | end 861 | end 862 | 863 | shorter_FooBar(3) 864 | 865 | # while loop 866 | # ---------- 867 | 868 | # The while loop iterates until 869 | # a condition is satisifed. 870 | itr = 1 871 | 872 | # Below, we iteratively increase the 873 | # value of the global variable, itr, 874 | # by 1. 875 | while itr <= 5 876 | println(itr) 877 | global itr += 1 878 | end 879 | 880 | # for loop 881 | # -------- 882 | 883 | # This loops through a collection that 884 | # is iterable, such as a unit range. 885 | for n = 1:10 886 | println(n) 887 | end 888 | 889 | # We can loop within a loop. 890 | for i = 1:2, j = 1:3 891 | println((i, j)) 892 | end 893 | 894 | 895 | # CONSTRUCTORS 896 | # ------------ 897 | 898 | # Introduction 899 | # ------------ 900 | 901 | # Constructors create objects just like 902 | # functions do. The objects are instances 903 | # of the composite types. 904 | 905 | # Creating an abstract type 906 | # ------------------------- 907 | 908 | # Below, we construct a type called 909 | # BloodPressure. 910 | 911 | struct BloodPressure 912 | systolic 913 | diastolic 914 | end 915 | 916 | # Blood pressure is now a type, just like 917 | # Real is a type. 918 | typeof(Real) 919 | 920 | typeof(BloodPressure) 921 | 922 | # The type BloofPressure has two fields 923 | fieldnames(BloodPressure) 924 | 925 | # We can instantiate BloodPressure 926 | ideal = BloodPressure(120, 80) 927 | 928 | ideal.systolic 929 | 930 | ideal.diastolic 931 | 932 | # The ideal variable is an instance 933 | # of the BloodPressure type 934 | typeof(ideal) 935 | 936 | # BloodPressure is a child of Any 937 | supertype(BloodPressure) 938 | 939 | # It is a concrete type (hence our 940 | # ability to instantiate it). 941 | subtypes(BloodPressure) 942 | 943 | # Inner constructors 944 | # ------------------ 945 | 946 | # We can define properties inside of 947 | # a constructor. Below, we also 948 | # constrain the values of the fields 949 | # to integers. 950 | 951 | struct BloodPressure2 952 | systolic::Int 953 | diastolic::Int 954 | BloodPressure2(systolic, diastolic) = 955 | systolic < diastolic ? error("Systolic must be higher than diastolic") : 956 | new(systolic, diastolic) 957 | end 958 | 959 | # Reals will be converted to integers (if 960 | # possible). 961 | BloodPressure2(120., 80) 962 | 963 | typeof(BloodPressure2(120., 80).systolic) 964 | 965 | # So far, our structs are immutable. This is easily 966 | # changed by adding the mutable keyword. 967 | 968 | # Mutable structs 969 | # --------------- 970 | 971 | # Below, we create a mutable struct. 972 | mutable struct Physiology 973 | systolic::Int 974 | diastolic::Int 975 | heart_rate::Int 976 | temperature::Real 977 | end 978 | 979 | # Now we instantiate our new type. 980 | ideal = Physiology(120, 80, 72, 37) 981 | 982 | # The heart rate was set to 72. 983 | ideal.heart_rate 984 | 985 | # We can now rewrite this value. 986 | ideal.heart_rate = 65 987 | 988 | ideal 989 | 990 | # More control of field types 991 | # --------------------------- 992 | 993 | # Parametric types allow us to 994 | # specify a consistent type for 995 | # the fields. 996 | 997 | mutable struct Electrolytes{T} 998 | sodium::T 999 | potassium::T 1000 | end 1001 | 1002 | # When instantiated, both field values 1003 | # must be the same type. 1004 | ideal = Electrolytes(140., 4.5) 1005 | 1006 | ill = Electrolytes(150, 3) 1007 | 1008 | # The advantage of using parametric types 1009 | # is that they can be instantiated as any 1010 | # type. 1011 | 1012 | Electrolytes{String} <: Electrolytes 1013 | 1014 | Electrolytes{Real} <: Electrolytes 1015 | 1016 | # We now also specify the type. 1017 | Electrolytes{Float64}(140, 4) 1018 | 1019 | # Functions containing our structs 1020 | # -------------------------------- 1021 | 1022 | # Now that we have created a new type, 1023 | # we can use it in a function. Below, 1024 | # we create a function called pulse_pressure, 1025 | # which is the difference between systolic 1026 | # and disatolic blood pressure. 1027 | function pulse_pressure(b::BloodPressure) 1028 | return b.systolic - b.diastolic 1029 | end 1030 | 1031 | # Instantiating BloodPressure 1032 | bp = BloodPressure(120, 80) 1033 | 1034 | # Calling the function and passing 1035 | # the instance of BloodPressure to it. 1036 | pp = pulse_pressure(bp) 1037 | 1038 | # Now, we create another function to 1039 | # calculate mean arterial pressure. The 1040 | # equation for this subtracts a third of 1041 | # the pulse pressure from the diastolic 1042 | # pressure. Note how we call another function 1043 | # inside of this function. 1044 | function mean_arterial_pressure(b::BloodPressure) 1045 | pulpres = pulse_pressure(b) 1046 | return b.diastolic + (1 / 3) * pulpres 1047 | end 1048 | 1049 | mean_arterial_pressure(bp) 1050 | 1051 | # The Electrolyte type was parametrically 1052 | # defined. Our instance, ill, had two 1053 | # integer-valued fields. When creating 1054 | # a function that might call any data 1055 | # type, we can constrain the type. 1056 | function check_potassium(p::Electrolytes{<:Real}) 1057 | if p.potassium > 5.2 1058 | println("Too high") 1059 | elseif p.potassium < 3.6 1060 | println("Too low") 1061 | else 1062 | println("Normal") 1063 | end 1064 | end 1065 | 1066 | # Here, we re-print the ill variable. 1067 | ill 1068 | 1069 | # Now, we use it as an argument in the 1070 | # function. 1071 | check_potassium(ill) 1072 | 1073 | 1074 | # COLLECTIONS 1075 | # ----------- 1076 | 1077 | # Introduction 1078 | # ------------ 1079 | 1080 | # Collections are elemnts combined 1081 | # into a single unit. 1082 | 1083 | # Arrays 1084 | # ------ 1085 | 1086 | # Literal arrays are elements 1087 | # separted by commas and placed 1088 | # in square brackets. 1089 | 1090 | [3] 1091 | 1092 | [1, 2, 3] 1093 | # Note that Atom displays this as a vector. 1094 | # In the REPL this is displayed as 1095 | # as a 3-element Array{Int64,1}. This 1096 | # refers to the fact that we entered 1097 | # 3 obects, all of type Int64 and they are 1098 | # whet we would consider a rank 1 tensor. 1099 | 1100 | # We can specify the keyword Array. 1101 | Array([1, 2, 3]) 1102 | 1103 | # The element type can also be specified. 1104 | Float64[1, 2, 3] 1105 | 1106 | # The type is a vector (one-dimensional) 1107 | # array. 1108 | typeof([1, 2, 3]) 1109 | 1110 | # In the REPL this will be displayed as an 1111 | # array. We can view thr hierarchical type 1112 | # structure using teh sypertype and subtypes 1113 | # functions. 1114 | supertype(Vector) 1115 | 1116 | supertype(DenseArray) 1117 | 1118 | supertype(AbstractArray) 1119 | 1120 | subtypes(AbstractArray) 1121 | 1122 | # To create a row vector, we add 1123 | # spaces between elements. 1124 | [1 2 3] 1125 | # Note the change to Array{Int64, 2}. 1126 | # This is now what we would consider 1127 | # a matrix (rank 2 tensor) with a 1128 | # single row and 3 columns. 1129 | 1130 | # We add a second row (more than one 1131 | # column), by adding a semi-colon. 1132 | [1 2 3; 4 5 6] 1133 | 1134 | # We now have a 2 by 3 array of 1135 | # integer elements. 1136 | 1137 | # Column vectors can be created as 1138 | # row elements (spaces inbewteen) 1139 | # in an array. 1140 | [[1, 2] [3, 4] [5, 6]] 1141 | 1142 | # The reshape() function takes an array 1143 | # and reshapes it according to 1144 | # the given dimesnions. Note that 1145 | # the elements are placed down the 1146 | # columns. 1147 | reshape([1, 2, 3, 4, 5, 6], (3, 2)) 1148 | 1149 | reshape([1, 2, 3, 4, 5, 6], (2, 3)) 1150 | 1151 | # There are a number of useful functions 1152 | # that create arrays. 1153 | 1154 | # The zeros() function creates 1155 | # an array of 0's of given length. 1156 | # or dimension 1157 | zeros(3) 1158 | 1159 | # We can specify the type of 0's. 1160 | zeros(Int64, 3) 1161 | 1162 | # The ones() function does the same, 1163 | # but with 1's. 1164 | ones(Int64, 3, 3) 1165 | 1166 | # In Julia, true is a Bit and is 1167 | # set to 1. 1168 | trues(3, 3) 1169 | 1170 | # False is set to 0. 1171 | falses(3, 3) 1172 | 1173 | # Diagonal() creates a diagonal 1174 | # matrix given the diagonal 1175 | # values. This function is 1176 | # part of package called LinearAlgebra. 1177 | # We must import it first. 1178 | using LinearAlgebra 1179 | Diagonal([1, 2, 3]) 1180 | # Note how this is a sparse array, 1181 | # with only the non-zero elements 1182 | # and their indices saved. 1183 | 1184 | # The range() function takes 1185 | # a starting value as positional 1186 | # argument and keyword arguments for 1187 | # the stop and step values. It 1188 | # return a unit range ideal for 1189 | # iteration. 1190 | range(1; stop = 5, step = 1) 1191 | 1192 | # The collect() function returns an 1193 | # array of the actual values in 1194 | # the unit array. 1195 | collect(range(1; stop = 5, step = 1)) 1196 | 1197 | # The fill() function creates an array 1198 | # with filled with a specified value. 1199 | fill(3., (2, 2)) 1200 | 1201 | # List comprehension creates elements 1202 | # of an array by expression and iteration. 1203 | [i^2 for i = 1:3] 1204 | 1205 | # The copy() function creates a copy of 1206 | # an array. 1207 | my_array = [1 2 3; 4 5 6; 2 3 4] 1208 | 1209 | # The copy() function makes a copy 1210 | # of an array. 1211 | my_array_2 = copy(my_array) 1212 | 1213 | # Now we change the value of the 1214 | # first element in the copy. 1215 | my_array_2[1, 1] = 100 1216 | 1217 | my_array_2 1218 | 1219 | # The value of the first element 1220 | # in the original array has not 1221 | # changed. 1222 | my_array 1223 | 1224 | # If we simply assign a new array to 1225 | # an exiting array, they point to the 1226 | # same object and changes to one are 1227 | # changes to both. 1228 | my_array_2 = my_array 1229 | 1230 | # Now we change a value in my_array_2. 1231 | my_array_2[1, 1] = 1000 1232 | 1233 | # The same change was made to 1234 | # my_array. 1235 | my_array 1236 | 1237 | # The similar() function creates a 1238 | # copy of the referenced array, but 1239 | # with placeholder values of a given 1240 | # type. 1241 | my_array_3 = similar(my_array, Float64) 1242 | 1243 | # We can also create an array 1244 | # with undefined values. 1245 | my_array_4 = Array{Float64}(undef, 3, 3) 1246 | 1247 | # The rand() funtion returns a 1248 | # value (or specified number of) 1249 | # values from the half-open interval 1250 | # [0,1) taken from a uniform 1251 | # distribution. 1252 | rand(5) 1253 | 1254 | # The randn() function does the same, but 1255 | # from the standard normal distribution. 1256 | randn(5) 1257 | 1258 | # A vector with elements of differing 1259 | # types will have all the elements 1260 | # converted to the highest concrete 1261 | # type. 1262 | [1, 2., 3//4] 1263 | 1264 | # When an element is a string, elements 1265 | # are all of type Any. 1266 | ["Julia", 1, 0.5] 1267 | 1268 | # An empty array is of element type 1269 | # Any. 1270 | [] 1271 | 1272 | 1273 | # Finding properties of arrays 1274 | # ---------------------------- 1275 | 1276 | # There are many functions to investigate 1277 | # the properties of an array. 1278 | 1279 | # Changing a single element to a float. 1280 | my_array_4 = [1 2 3; 4. 5 6; 2 3 4] 1281 | 1282 | # All the elements in my_array were 1283 | # 64-bit integers. 1284 | eltype(my_array) 1285 | 1286 | # Type inheritence will convert all elements 1287 | # to Float64. 1288 | eltype(my_array_4) 1289 | 1290 | # Number of elements in an array. 1291 | length(my_array) 1292 | 1293 | # Number of dimensions. 1294 | ndims(my_array) 1295 | 1296 | # Tupe of length of each dimension. 1297 | size(my_array) 1298 | 1299 | # Tuple of valid indices alon each axis. 1300 | axes(my_array) 1301 | 1302 | # Specify the axis. 1303 | axes(my_array, 1) 1304 | 1305 | # There is an efficient iterator for each 1306 | # index in an array. 1307 | [println(i) for i in eachindex(my_array)] 1308 | 1309 | 1310 | # Indexing 1311 | # -------- 1312 | 1313 | # Each element in an array has an index. 1314 | 1315 | # Below, we create a 5x4 array. 1316 | my_array = [1 2 3 4; 5 4 3 2; 3 4 5 6; 7 6 5 4; 2 4 6 8]; 1317 | my_array 1318 | 1319 | # The size() function returns the 1320 | # required array. 1321 | size(my_array) 1322 | 1323 | # All the elements in the first 1324 | # row. 1325 | my_array[1, :] 1326 | 1327 | # All elements in column 1. 1328 | my_array[:, 1] 1329 | 1330 | # All elements in rows 1 to 3 1331 | # and columns 1 to 3. 1332 | my_array[1:3, 1:3] 1333 | 1334 | # Values in column 1 and 3. 1335 | my_array[:, [1, 3]] 1336 | 1337 | # We can also extract values using a 1338 | # matrix of Boolean values. 1339 | slt = rand(Bool, (5, 4)) 1340 | 1341 | # Only the indices with true (1) 1342 | # are selected when this is used 1343 | # to index a matrix. 1344 | my_array[slt] 1345 | 1346 | # We can use such a selection to 1347 | # change values. 1348 | my_array[slt] .= 300; 1349 | 1350 | # The selected values have now changed. 1351 | my_array 1352 | 1353 | # Dictionaries 1354 | # ------------ 1355 | 1356 | # Dictionaries are elements in a list, 1357 | # each consisting of two parts, a key 1358 | # and a value. So, instead of relying 1359 | # on an index, we can reference the 1360 | # key to get a value. The Dict function 1361 | # creates a dictionary. 1362 | my_dict = Dict("Language" => "Julia", "Version" => 1.4, 1363 | "Type" => "Compiled") 1364 | 1365 | # The keys() function returns the keys 1366 | # in a dictionary 1367 | keys(my_dict) 1368 | 1369 | # The values() function returns the 1370 | # values in a dictionary. 1371 | values(my_dict) 1372 | 1373 | # The get() function returns the 1374 | # value of a specified key, or else 1375 | # a default if it is given. 1376 | 1377 | 1378 | --------------------------------------------------------------------------------