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:
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.
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.
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
- +: 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.
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.
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
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)
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.
# 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.
Comments
Post a Comment
Type your comment here.
However, Comments for this blog are held for moderation before they are published to blog.
Thanks!