Skip to main content

R Programming Basic Terms and Concepts

The Beginner’s Guide to R Programming Basics:

Welcome to this lecture on the basic vocabulary used in R programming! Programming is a discipline that involves creating software and applications that can automate tasks, perform calculations, and make decisions. To get started in programming, you need to understand some of the basic concepts and vocabulary used in the programming world. In this lecture, we will discuss some of the most common terms you'll encounter as a beginner programmer.


Variables

You have heard this term in the previous lessons, so what does it mean?

Variables are used to store data in a program. They have a name and a value, and you can use them to perform operations, calculations, and decision-making in your code. In most programming languages, you can define a variable by specifying its name and value, like this:

X<-10

Here, we've defined a variable named x and assigned it a value of 10. We can then use this variable in other parts of our program to perform operations, like this:

Y<-X+5

Here, we've defined another variable named y and assigned it the value of x + 5. Since x has a value of 10, y will have a value of 15.

Data Types

You must be very familiar with this term at this stage. So Data types refer to the kind of data that can be stored in a variable. In most programming languages, there are several data types to choose from, including integers, numeric, strings, and Booleans. Here are some examples of data types in R programming language:

  • You can define an integer variable like this: x <- 10L, where L represents the integer value.
  • You can define a string variable like this: s <- "Hello, world!" which is same as initiating character.
  • You can define a Boolean variable like this: y <- true
  • You can initialize lists, data frames, matrixes etc. we have covered Data types in depth, you can watch those lectures, if you haven’t watched yet.

Operators

Well Operators are used to perform operations on variables and values. There are several types of operators, including arithmetic operators, comparison operators, and logical operators.

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on numeric values in R. R has several arithmetic operators that can be used to perform addition, subtraction, multiplication, division, and other operations. Here are the most common arithmetic operators in R:

  • +: The addition operator adds two values together. For example, 2 + 3 would return 5.
  • -: The subtraction operator subtracts one value from another. For example, 5 - 3 would return 2.
  • *: The multiplication operator multiplies two values together. For example, 2 * 3 would return 6.
  • /: The division operator divides one value by another. For example, 6 / 2 would return 3.
  • ^: The exponentiation operator raises one value to the power of another. For example, 2^3 would return 8.
  • %%: The modulo operator returns the remainder of dividing one value by another. For example, 7 %% 2 would return 1.

Here are some examples of using these arithmetic operators in R:

x <- 2

y <- 3

z <- x + y # z is now 5

a <- x * y # a is now 6

b <- y^2 # b is now 9

c <- 10 / 3 # c is now 3.333333

d <- 7 %% 3 # d is now 1

In addition to these basic arithmetic operators, R also provides a number of other built-in functions for performing more advanced mathematical operations, such as sqrt() for calculating square roots, sin() and cos() for calculating trigonometric functions, and log() for calculating logarithms.

Order of Arithmetic Operators

In R, the order in which arithmetic operations are performed is determined by operator precedence. Operator precedence is a set of rules that dictate which operators are evaluated first in an expression. Here is the order of operator precedence in R:

  • Parenthesis ()
  • Exponentiation (^)
  • Multiplication, division, and modulo (*, /, and %%) Here as we have 3 operators on one level, R will perform them first from left to Right.
  • Addition and subtraction (+ and -) on same level, hence performed from left to right.

This means that if an expression contains multiple operators, the ones with higher precedence will be evaluated first. For example:

4 + 5 * 2

In this expression, multiplication (*) has higher precedence than addition (+), so 5 * 2 will be evaluated first, resulting in 10. Then the addition operation will be performed, resulting in 14.

If you want to change the order in which operations are evaluated, you can use parentheses to group parts of an expression together. Anything inside parentheses will be evaluated first. For example:

(4 + 5) * 2

In this expression, the addition inside the parentheses will be performed first, resulting in 9. Then the multiplication operation will be performed, resulting in 18.

It's important to keep operator precedence in mind when writing complex expressions in R to make sure that the operations are being performed in the correct order.

Comparison Operator

In R, comparison operators are used to compare values and return a logical value (TRUE or FALSE) depending on whether the comparison is true or false. Here are the comparison operators in R:

  • ==: Equals
  • !=: Not equals
  • <: Less than
  • >: Greater than
  • <=: Less than or equal to
  • >=: Greater than or equal to

Here are some examples of using comparison operators in R:

x <- 5

y <- 10

# Equals

x == y # FALSE

# Not equals

x != y # TRUE

# Less than

x < y # TRUE

# Greater than

x > y # FALSE

# Less than or equal to

x <= y # TRUE

# Greater than or equal to

x >= y # FALSE

Logical Operators

Logical operators are used to combine logical values and return a logical value. Here are the logical operators in R:

  • &: Element-wise AND
  • |: Element-wise OR
  • !: Negation (NOT)

Here are some examples of using logical operators in R:

x <- TRUE

y <- FALSE

# Element-wise AND

x & y # FALSE

# Element-wise OR

x | y # TRUE

# Negation (NOT)

!x # FALSE

It's important to keep in mind the order of operations when using logical operators in R. Just like arithmetic operators, logical operators have a specific order of precedence. ! is evaluated first, followed by &, and then |. If you want to change the order of evaluation, you can use parentheses to group parts of an expression together.

Escape Sequences

Escape sequences in R are special characters that are used to represent characters that cannot be typed directly into a string or text literal. They are represented by a backslash (\) followed by a character or sequence of characters.

Here are some common escape sequences in R:

  • \\: Represents a backslash character.
  • \': Represents a single quote character.
  • \": Represents a double quote character.
  • \n: Represents a newline character.
  • \t: Represents a tab character.
  • \r: Represents a carriage return character.
  • \b: Represents a backspace character.
  • \f: Represents a form feed character.

Here are some examples of using escape sequences in R:

# Using escape sequences in a string

message("Hello, world!\n")

message("This is a double quote: \"\n")

message("This is a single quote: '\n")

message("This is a backslash: \\n")

# Using escape sequences in regular expressions

pattern <- "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$"

Escape sequences can be particularly useful when working with strings that contain special characters or when writing regular expressions. However, it's important to use them correctly, as using the wrong escape sequence or failing to escape a special character can cause errors or unexpected behavior in your code.

In -> Work -> Out

So one basic concept, we should remember and never forget it, is that we should write a code in a way that it takes some input, some data or instructions and then it process that, work that out, and then gives the output. This should be the sequence that you should follow when developing some code or a program. You should always write it in away that it takes input or instructions, then process that and then returns the output. So never forget this basic rule.

Few Other terms:

So there are few more concepts that we will discuss in depth in future lessons and that are:

  • Control structures, they are used to control the flow of a program. They allow you to perform different actions depending on certain conditions. There are several types of control structures, including if/else statements, for loops, and while loops,
  • Subsetting very important, often you need subset of data for your analysis or work, so it’s always better to carry that subset only, as we know in R Dataset is stored or loaded in computer memory. So always better to subset the dataset according to our needs, so we should work efficiently.
  • Functions, these are set of instructions that you write and store for reuse.
  • Loops functions, there are some inbuilt loop functions that we will study in depth, and these functions are really useful, as you go ahead.


So these were some basic vocabulary or concepts that we often use in Programming world. So if you might be wondering what does these terms means in our last few lectures, then I have got you covered here with this in between lecture.

  • So if you haven't started practicing Swirl's lessons on R Programming, Start Now! Complete download process of swirl and R Programming is here, click on the link!
  • If you haven't downloaded the text book, click here to download the textbook.
  • Lecture slides can be downloaded from here. It would be great if you go through them too. 
See you all in the next lesson, where we will start subsetting in R.

Comments

Popular posts from this blog

Mastering Data Science Experimental Design: From Hypothesis to Results

The Beginner’s Guide to Data Science Experimental Design: Now that we’ve looked at the different types of data science questions, we are going to spend some time looking at experimental design concepts, in our last lesson of our first Course "The Data Science Toolbox" in our Data Science Specialization using R Programming. As a data scientist, you are a  scientist  and as such, need to have the ability to design proper experiments to best answer your data science questions! Previous lesson, if you haven't watched! What does experimental design mean? Experimental design is organizing an experiment so that you have the correct data (and enough of it!) to clearly and effectively answer your data science question. This process involves clearly formulating your question in advance of any data collection, designing the best set-up possible to gather the data to answer your question, identifying problems or sources of error in your design, and only then, collecting the app...

The Evolution of R: From S-Inspired Language to Statistical Powerhouse

The Beginner’s Guide to the History of R Programming: R Programming is basically the dialect of S Programming. S History: The S programming language was first developed in the late 1970s by John Chambers and his colleagues at Bell Laboratories. It was initially used for data analysis and graphics, and it served as the basis for the commercial software package S-PLUS, which was released in the early 1990s. While S-PLUS was popular in the statistical community for many years, it has since been largely replaced by the open-source software environment R, which was inspired by S and developed by some of the same people who worked on S-PLUS. As for the current version of S, it's not as widely used as R, and there are several different implementations of the S language that are still available today, including: S-PLUS: This is the commercial implementation of S that was developed by TIBCO Software Inc. It is still in use today, although it has been largely supplanted by R in the statisti...

Streamlining Your Workflow: Linking Git/GitHub with R Studio for Efficient Version Control

The Beginner’s Guide Linking Git/GitHub with R Studio: Now that we have both R Studio and Git set-up on your computer and a GitHub account, it’s time to link them together so that you can maximize the benefits of using R Studio in your version control pipelines. First we will link R studio and Git and then we will link R Studio and GitHub. We will also link an existing Project with Git and GitHub. Linking R Studio and Git In R Studio, go to Tools > Global Options > Git/SVN Use the Global Options menu to tell R Studio you are using Git as your version control system Sometimes the default path to the Git executable is not correct. Confirm that git.exe resides in the directory that R Studio has specified; if not, change the directory to the correct path. Otherwise, click OK or Apply. Confirm that the directory R Studio points to for the Git executable is correct R Studio and Git are now linked. Linking R Studio and GitHub In that same R Studio option window, clic...