Getting started with the R data analysis language
Number Game
The R programming language is a universal tool for data analysis and machine learning.
The R language is one of the best solutions for statistical data analysis. R is ideal for tasks such as data science and machine learning. R, which was created by Ross Ihaka and Robert Gentleman at the University of Auckland in 1991, is a GNU project that is similar to the S language, which was developed in the 1970s at Bell Labs.
R is an interpreted language. Input is either executed directly in the command-line interface or collected in scripts. The R language is open source and completely free. R, which runs on Linux, Windows, and macOS, has a large and active community that is constantly creating new, customized modules.
R was developed for statistics, and it comes with fast algorithms that let users analyze large datasets. There is a free and very well-integrated development environment named RStudio, as well as an excellent help system that is available in many languages.
The R language works with a library system, which makes it easy to install extensions as prebuilt packages. It is also very easy to integrate R with other well-known software tools, for example Tableau, SQL, and MS Excel. All of the libraries are available from a worldwide repository, the Comprehensive R Archive Network (CRAN) [1]. The repository contains over 10,000 packages for R, as well as important updates and the R source code.
The R language includes a variety of functions for managing data, creating and customizing data structures and types, and other tasks. R also comes with analysis functions, descriptive statistics, mathematical set and matrix operations, and higher-order functions, such as those of the Map Reduce family. In addition, R supports object-oriented programming with classes, methods, inheritance, and polymorphism.
Installing R
You can download R from the CRAN website. The CRAN site also has installation instructions for various Linux distributions. It is a good idea to also use an IDE. In this article, I will use RStudio, which is the most popular IDE for R.
RStudio is available in two formats [2]. RStudio Desktop is a normal desktop application, and RStudio server runs as a remote web server that gives users access to RStudio via a web browser. I used RStudio Desktop for the examples in this article.
When you launch RStudio Desktop after the install, you are taken to a four-panel view (Figure 1). On the left is an editor, where you can create an R script, and a console that lets you enter queries and display the output directly. Top right, the IDE shows you the environment variables and the history of executed commands. The visualizations (plots) are output at the bottom right. This is also where you can add packages and access the extensive help feature.
First Commands
When you type a command at the command prompt and press Enter, RStudio immediately executes that command and displays the results. Next to the first result, the IDE outputs [1]
; this stands for the first value in your result. Some commands return more than one value, and the results can fill several lines.
To get started, it is a good idea to take a look at R's data types and data structures. More advanced applications build on this knowledge; if you skip over it, you might be frustrated later. Plan some time for the learning curve. The basic data types in R are summarized in Table 1. Table 2 summarizes some R data structures.
Table 1
Data Types in R
Type | Designation | Examples |
---|---|---|
Logical values |
|
|
Integers |
|
|
Floating-point numbers |
|
|
Strings |
|
|
Table 2
Data Structures in R
Name | Description |
---|---|
Vector |
The basic data structure in R. A vector consists of a certain number of components of the same data type. |
List |
A list contains elements of different types, such as numbers, strings, vectors, matrices, or functions. |
Matrix |
Matrices do not form a separate object class in R but consist of a vector with added dimensions. The elements are arranged in a two-dimensional layout and have rows and columns. |
Data frame |
One of the most important data structures in R. This is a table in which each column contains values of a variable and each row contains a set of values from each column. |
Array |
An array stores data in more than two dimensions. An array with the dimensions (2, 3, 4) creates four rectangular matrices, each with two rows and three columns. |
To create an initial graph, you first need to define two vectors x
and y
, as shown in the first two lines of Listing 1. The c
stands for concatenate, but you could also think of it as collect or combine. You then pass the variables x
and y
to the plot()
function (last line of Listing 1), along with vectors; the col
parameter defines the color of the points in the output. Figure 2 shows the results.
Listing 1
First Chart
x <- c(1, 3, 5, 8, 12) y <- c(1, 2, 2, 4, 6) plot(x,y,col="red")
Installing Packages
Each R package is hosted on CRAN, where R itself is also available. But you do not need to visit the website to download an R package. Instead, you can install packages directly at the R command line. The first thing you will want to do is fetch a library for visualizations. To do this, call the install.packages("ggplot2")
command in the command prompt console. The installation requires a working C compiler.
Setting up a package does not make its features available in R yet – it just puts them on your storage medium. To use the package, you need to call it in the R session with the library("ggplot2")
command. After restarting R, the library is no longer active; you might need to re-enable it. Newcomers tend to overlook this step, which often leads to time-consuming troubleshooting.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.