Get startedGet started for free

Using packages

1. Using packages

Now you can write your own Julia scripts and functions. But like the length and sort functions, a lot of the code you might want has already been written by someone else. You just need to find it.

2. Packages

This is where packages come in. A Julia package is a collection of Julia files from which you can borrow functions and structures. Some popular packages you're sure to use are the Statistics package for calculating basic statistics to describe your data; the DataFrames package, which allows you to work with tabular data; the CSV package, which loads CSV data; and the Plots package, for visualizing your data. We'll use most of these packages in this course. There are thousands of Julia packages available already, and the list keeps growing. You can find a complete list of public packages and some curated lists of packages here.

3. Installing packages

All the packages you will need in this course have already been installed on DataCamp. If you want, you can install these packages on your own system too. The easiest way to install a package is to launch a Julia session by typing Julia in your terminal and import the package name. If you don't have the package installed already, Julia will ask you if you would like to install it.

4. Importing packages

Once you have installed the package, you need to import it. In Julia, there are two ways to import packages. We can use the import keyword or the using keyword followed by the package name. Here we import the Statistics package using both methods. The Statistics package has a function called mean, which calculates the mean of an array of values. We can use the function by typing Statistics-dot-mean and passing it an array. If we have imported the package with the using keyword, then we can access the mean function just by typing mean instead of Statistics-dot-mean. This isn't available if we use the import keyword. There are benefits to both approaches. The using keyword means the calls to imported functions will be more compact, though sometimes, it will be unclear where each function has come from, especially if we have imported multiple packages. Things can also get messy if we want to write our own function called mean.

5. Importing packages

We can shorten the name of the package with the import keyword by importing the package under an alias. This essentially means to import it with a shorter nickname. Here we import the Statistics package as sts. Now we can use the mean function by calling sts-dot-mean. We cannot create an alias when we import a package with the using keyword.

6. The Statistics package

The Statistics package has functions for calculating the mean, median, standard deviation, and variance of an array. For each of these functions we pass in the array and they return a number. You'll practice using these functions and importing packages in the exercises.

7. Let's practice!

Let's practice.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.