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

Introduction to R Markdown

The Beginner’s Guide to R Markdown! We’ve spent a lot of time getting R and R Studio working, learning about Functionalities of R Studio and R Packages - you are practically an expert at this! There is one major functionality of R/R Studio that we would be remiss to not include in your introduction to R -  Markdown! Functionalities in R Studio Introduction to R Packages What is R Markdown? R Markdown is a way of creating fully reproducible documents, in which both text and code can be combined. In fact, these lessons are written using R Markdown! That’s how we make things: bullets bold italics links or run inline r code And by the end of this lesson, you should be able to do each of those things too, and more! Despite these documents all starting as plain text, you can render them into HTML pages, or PDFs, or Word documents, or slides! The symbols you use to signal, for example,  bold  or  italics  is compatible with all of those formats. Wh...

What is Data? And What is Data Science Process?

The Beginner’s Guide to Data & Data Science Process About Data: In our First Video today we talked about Data and how the Cambridge English Dictionary and Wikipedia defines Data, then we looked on few forms of Data that are: Sequencing data   Population census data ( Here  is the US census website and  some tools to help you examine it , but if you aren’t from the US, I urge you to check out your home country’s census bureau (if available) and look at some of the data there!) Electronic medical records (EMR), other large databases Geographic information system (GIS) data (mapping) Image analysis and image extrapolation (A fun example you can play with is the  DeepDream software  that was originally designed to detect faces in an image, but has since moved on to more  artistic  pursuits.) Language and translations Website traffic Personal/Ad data (e.g.: Facebook, Netflix predictions, etc.) These data forms need a lot of preprocessin...

Introduction to Functions and Arguments in R Programming: Part 2

The Beginner’s Guide to Functions in R Programming: Functions are an essential part of programming, and they play a critical role in R programming. In R, a function is a set of instructions that perform a specific task. Functions in R can have several arguments, and their evaluation can be lazy or eager. In this blog post, we will explore functions in R, including their  "dot-dot-dot" or ellipsis  argument, lazy evaluation, and more . Ellipsis or "dot-dot-dot" Argument in R Functions The "dot-dot-dot" or ellipsis argument in R programming is a special argument that can be used in functions to represent a variable number of additional arguments that are not explicitly defined in the function. The ellipsis argument is represented by three dots ... and is typically used at the end of the function's argument list. When the function is called, any additional arguments provided by the user after the defined arguments are collected by the ellipsis argument an...