Understanding how to code in R using R-studio

153 VIEWS

·

R is a free programming language and software environment which is used for statistical computing and graphics. RStudio is a free and open-source integrated development environment for R. RStudio is usually preferred when working with R because it is not only free, but also provides a very powerful way of organising and manipulating your R windows and files.
Unlike most conventional programming languages, R does not require you to declare variables of a specific type. It stores the data provided in R-objects, and these objects automatically assign a type to the data based on its characteristics and the way it is stored. The various R-objects which are used to store data are: vectors, lists, matrices, arrays, factors, and data frames.

Working with vectors in RStudio

A vector is an r-object which can be used to store a single value or a group of values of the same data type. However, when working with a group of values, you use the c() function in R to combine them. Below shows how vectors can be created to store a group of values:

You can extract information from a vector by specifying the index where the information is stored. This is demonstrated below:

Unlike vectors, lists can contain multiple data types. To create a list in RStudio, you must use the list() function. Otherwise, R assumes that you want to create a vector. Bellow shows how lists can be created in R:

The information in a list can be extracted by specifying the index where the element is located. This is demonstrated below:

Working with matrices in RStudio

The matrix r-object provides a multi-dimensional method for expressing data. To create a matrix in RStudio, you need the matrix() function. The matrix() function, however, can include these arguments: matrix(data, nrow, ncol, byrow, dimnames). Here is a quick description of each of these arguments:

  1. data: Defines the data used to fill the data elements in the matrix
  2. nrow: Specifies the number of rows to create
  3. ncol: Specifies the number of columns to create
  4. byrow: Defines the arrangement of the vector elements in the matrix. It is a Boolean value.
  5. dimnames: Specifies the names of the dimensions of the matrix.

Below shows how matrices can be created in RStudio:

You can extract information from a matrix r-object by specifying the index (that is, both row and column index) where the element is located. This is demonstrated below:

Working with data frames in RStudio

The data frame r-object is similar in structure to a matrix or a two-dimensional array since they have rows and columns. However, they differ in the sense that each column in the data frame represents a variable, and each row in the data frame represents an example or instance of that variable. To create a data frame in RStudio, first create different variables to represent the various columns in the data frame and assign the corresponding group of information to each variable. In doing this, ensure that all the columns have the same length. After assigning the various groups of information to the variables, parse these variables as arguments in the data.frame() function to create the data frame. This is demonstrated below.

To extract any row from the data frame, you must specify the index of the row that you want to extract. This is demonstrated below:

To extract any row from the data frame, you need to specify the index of the row you want, followed by a comma. This is demonstrated below:

To interact with the vectors containing the column values directly, use the $ operator. This is demonstrated below:

To help ascertain the data structure of the data frame, use the str() function. This is demonstrated below:

To obtain a statistical summary of the information in your data frame, use the summary() function. This is demonstrated below:

Decision structures in R

Using the if statement

The if statement in R allows the user to execute a block of statements if a particular statement is true. This is demonstrated below:

Using the if – else statement

The if – else statement in R allows the user to execute a block of statements if a particular statement is true, or execute a separate block of instructions if that particular statement is false. This is demonstrated below:

Using the switch statement

The switch statement allows R to produce an output that is dependent upon a particular input value. This is demonstrated below:

Loops in R

The Repeat Loop

The Repeat Loop is a loop in R that continues to execute a block of statements until it is broken out of with the break statement. This is demonstrated below:

The While Loop

The While Loop is a loop in R that continues to execute a block of statements until a particular condition is met. This is demonstrated below:

The For Loop

The For Loop is a loop in R that executes for a specified number of times, executing a specified block of statements in each iteration. This is demonstrated below:

Conclusion

I hope that this tutorial has given you all the basic skills you need to help you create very simple R programs using the RStudio software.

Do you think you can beat this Sweet post?

If so, you may have what it takes to become a Sweetcode contributor... Learn More.

David Sasu is a junior studying Computer Science in Ashesi University. He is passionate about understanding technology and using it to solve important problems. He is currently working on the creation of information systems for under-funded orphanages in his country, Ghana. He hopes to specialize in the fields of Artificial Intelligence and cybersecurity to enable him to create systems to help safeguard and improve the African continent.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu