Please disable your adblock and script blockers to view this page

Search this blog

Monday, 13 August 2018

R Decision Making - IF...ELSE Statement in R Programming

We can write a simple IF statement, IF...ELSE statements or nested IF statements as per our requirement. If you know the basics of any other programming language then after this tutorial you'll get an idea about writing conditional statements in R programming.



Flow Chart of IF...ELSE is like this



Basic Syntax of IF-ELSE is like this in R

IF (Condition 1){
Statement to execute (if condition 1 is true)
}ELSE IF (Condition 2){
Statement to execute (if condition 2 is true)
}ELSE
Statement to execute (if condition 1& 2 both are false)

For a better understanding of decision-making, statements see this example

> #Declare Variables
> a <- 20
> b <- 40
> c <- 5
> 
> #Check conditions
> if(a>b && a>c){
+ print("Largest Value is A")
+ 
+ }else if(b>a && b>c){
+ print("Largest Value is B")
+ 
+ }else print("Largest Value is C-")
[1] "Largest Value is B"
>

This is how IF…ELSE works in R programming.

Cheers 🙂 Happy Learning

Saturday, 11 August 2018

R Operators – Arithmetic Operators, Relational Operators, Logical Operators

 In programming, an operator is a symbol that is used to perform any specific mathematics or logical operation. In simple language, we can say that the operator is used to perform some operations and like other programming languages R has a set of various types of R operators that can be divided into the following categories.

 

  • Arithmetic Operator
  • Relational Operator
  • Logical Operator
  • Assignment Operator

Arithmetic Operators in R

Arithmetic operators are used for performing mathematical operations like addition, subtraction etc.

OperatorDescription
+Addition (To Add two variables in R)
-Subtraction (To Subtract one variable from another)
*Multiplication (To Multiply variables)
/Division (To divide the first variable with the second)
^Exponent (The first variable's value raised to the exponent of second value)
%%Modulus (Gives the remainder of the first variable's value with the second)

See an example here

> # An addition
> 5 + 10 
[1] 15
> 
> # A subtraction
> 10 - 5 
[1] 5
> 
> # A multiplication
> 5 * 15
[1] 75
> 
> # A division
> 12 / 2 
[1] 6
> 
> # Exponentiation
> 2^6
[1] 64
> 
> # Modulus
> 25%%4
[1] 1
>

Relational Operators in R

Relational Operators are used for comparing values and produce a boolean result.

OperatorDescription
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
==Equal to
!=Not equal to

See an example here

> # Greater than
> a <- 10
> b <-20
> print(b>a)
[1] TRUE
> 
> # Less than
> a <- 10
> b <-20
> print(b<a)
[1] FALSE
> 
> # Greater than or equal to
> a <- c(10,20,5)
> b <- c(20,10,5)
> print(b>=a)
[1] TRUE FALSE TRUE
> 
> # Less than or equal to
> a <- c(10,20,5)
> b <- c(20,10,5)
> print(b<=a)
[1] FALSE TRUE TRUE
> 
> # Equal to
> a <- 10
> b <-10
> print(b==a)
[1] TRUE
> 
> # Not equal to
> a <- 10
> b <-10
> print(b!=a)
[1] FALSE


Logical Operators in R

The logical operators are used to carry out boolean operations between variables. Number zero (0) is considered FALSE and all numbers greater than 0 are considered TRUE.

OperatorDescription
&Element wise logical AND (It checks each value of both vector and gives output TRUE if both values are TRUE)
|Element wise logical OR (It checks each value of both vector and gives output TRUE if any one value is TRUE)
!Logical NOT (It takes each value of vector and reverse it's logical value)
&&Logical AND (It checks only first value of both vector and gives output TRUE if both values are TRUE)
||Logical OR (It checks only first value of both vector and gives output TRUE if any of them are TRUE)

See an example here

> #Take two vectors
> a <- c(0,20,30,0,FALSE)
> b <- c(12,23,42,0,TRUE)
> 
> #Print Element wise logical AND
> print(a&b)
[1] FALSE  TRUE  TRUE FALSE FALSE
> 
> #Print Element wise logical OR
> print(a|b)
[1]  TRUE  TRUE  TRUE FALSE  TRUE
> 
> #Print Logical AND
> print(a&&b)
[1] FALSE
> 
> #Print Logical OR
> print(a||b)
[1] TRUE
> 
> #Print Logical NOT
> print(!a)
[1]  TRUE FALSE FALSE  TRUE  TRUE
> print(!b)
[1] FALSE FALSE FALSE  TRUE FALSE
>

Assignment Operators in R

Learn more about R assignment operators here

Cheers :) Happy Learning

Saturday, 28 July 2018

R Variables and Constants - Variable Assignment, Search & Delete

Variables are the name given to a piece of data or information. Like many other programming languages, we use R variables to store data. R supports numeric, string, boolean and many other data types and we do not declare a variable with the data type, instead of that we assign a value to the variable and on basis of value R automatically sets the data type of variable.

A variable name consists of characters, numbers, and the special character (dot(.) and underscore(_) only) and can not start with any number.


See an example of variable naming in R programming

#Variables Naming in R Programming

#Valid Varibale Name
varone <- 20
print(varone)

#Valid Variable Name
.var_two <-30
print(.var_two)

#Invalid Variable Name
12months <- 12

#Invalid Variable Name (as dot is followed by number)
.1Var <- "Ashish"

The output on R Console



Variable Assignment in R

In R programming, the value of a variable can be assigned using the left arrow, right arrow or equal to the operator and the data type of a variable can be changed multiple times in a program that depends on its value.

# Assignment using  using left arrow
var1 <- 5
print(var1)

# Assignment using  using right arrow
"Ashish Awasthi" -> var2 
print(var2)

# Assignment using equal to operator
var3 = FALSE 
print(var3)



Data type of a Variable

This is how we can check the data type of any variable

# Declare Different types of Variables
var1 <- 5
print(var1)
"Ashish Awasthi" -> var2 
print(var2)
var3 = FALSE 
print(var3)

# Check class of var1
class(var1)

# Check class of var2
class(var2)

# Check class of var3
class(var3)



Searching Variable

We can find all variable available in the workspace using ls() function, and we can also use pattern in ls function to find the specific variable.

See an example here

#Declare some variables
var <- 10
var1 <- "Ashish Awasthi"
var2 <- 20.5
test_var <- TRUE

#Print all variables present in workspace
ls()

#Search specific variable
ls(pattern="test")

The output on R Console


Deleting a Variable

If you want to delete a variable that is no longer needed then there is rm() function, that removes variable from the workspace.

See an example

#Declare some variables
var <- 10
var1 <- "Ashish Awasthi"
var2 <- 20.5
test_var <- TRUE

#Delete a variable
rm(var2)

#Now print that variable
print(var2)

The output on R Console



Constants in R

As the name suggests constant means, an entity that value can not be changed. Constants can be numeric, character, boolean etc. All numbers are numeric constants, we can check it's using typeof() function.

#Numeric Constants
typeof(10)

#Character Constants
typeof("Ashish Awasthi")

#Buil-in Constants
print(pi)
typeof(pi)

print(LETTERS)
typeof(LETTERS)

print(letters)
typeof(letters)

print(month.name)
typeof(month.name)



Cheers :) Happy Learning