In general, you can use the following template to install a package in R:
For demonstration purposes, you’ll see how to install the readxl package. This package is used to import Excel files into R.
The same steps that will be reviewed can be used to install other packages in R as well.
Steps to Install a Package in R
Step 1: Launch R
To start, you’ll need to launch R. You’ll then see the R Console:
Step 2: Type the command to install the package
Now you’ll need to use the following template to install your package:
For example, you may type the following command in the R Console in order to install the readxl package:
Once you are done typing the command, press ENTER to proceed with the installation.
Step 3: Select a Mirror for the installation
For the final step, select a Mirror for the installation. You may choose a mirror which is closer to your geographic location.
Step 4: Start using the package installed
To start using the package installed, you’ll need to load it in the R Editor.
For instance, to load the readxl package, you’ll have to use:
Let’s say that you want to import an Excel file into R (where the Excel file name is ‘Products’). The data that is stored in the Excel file is as follows:
For demonstration purposes, assume that the file is stored under the following path:
So this is the code to import the Excel file:
You’ll need to adjust the path to reflect the location where the Excel file is stored on your computer (don’t forget to use double backslash within the path name to avoid any errors).
Run the code in R, and you’ll get this table which matches with the data stored in the Excel file:
product price 1 Laptop 1200 2 Tablet 350 3 Printer 150 4 Keyboard 100