Please disable your adblock and script blockers to view this page

Search this blog

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

Monday, 23 July 2018

R Data Types – Vectors, Matrices, Lists, Factors, Data Frames

 Like other programming languages, R supports many different data types. You must have seen that variables are used to store data in a program and a data type is assigned to a variable and that variable can hold only that type of data. In this post, we'll learn about R data types and R objects.

Basic data types in R programming are Numeric, Integer, Character, Logical and Complex and other than this R has some unique data types that are called R Objects.



Vectors in R

A Vector is basically a set of values of the same basic data type like numeric character etc. Vector in R is created using the c() function that represents a combination of elements.

See this example

# A Numeric Vector
numeric_vector <- c(10, 20, 30)

# A Character Vector
character_vector <- c("a", "b", "c")

# A Boolean Vector
boolean_vector <-c(TRUE,FALSE,TRUE)

The output on R Console is


Matrices in R

R supports Matrices and a Matrix is a collection of data values in 2 dimensions of the same basic data type, R creates a matrix of values using a matrix() function.

See this example

Here c(1,2,3,4,5,6,7,8,9) is a numeric vector
nrow is the number of rows in the matrix
ncol is the number of columns in the matrix


#Create a matrix using Vector
test_matrix<-matrix(c(1,2,3,4,5,6,7,8,9), nrow=3, ncol=3)
#Print matrix on console
print(test_matrix)

The output on R Console is


Arrays in R

Arrays are the same as any other programming language and in R, an array is the same as a matrix but it can have more than two dimensions. Array in R is created using the array() function and uses a vector as input and dim value to create arrays.

Here dim =c(2,2,4) means that 4 arrays will be created of 2x2.

#We have two vectors here
v1 <- c(1,2,3,7,8,9)
v2 <- c(4,5,6,10,11,12,13,14,15,16)

#Create array using vectors
test_array <- array(c(v1,v2),dim = c(2,2,4))

#print the array on the console
print(test_array)

The output on R Console is


Lists in R

A List is a set of values that can have different basic data types, In R List is created using the list() function.

#A list with different data types
#Declare a numeric vector
numeric_vector<-c(1,2,3)

#Create list 
test_list<- list("Ashish Awasthi", numeric_vector, 5.3)

#Print list
print(test_list)

The output on R Console


Factors in R

Factors are created using vectors as base and stores unique values as levels, In R Factor object is created using factor() function.


# Create a vector with duplicate values
emp_names <- c('James','Ram','James','Ashish','Ram','Ashish','James','Ram')

# Create a factor object using vector
factor_emp <- factor(emp_names)

# Print the factor object
print(factor_emp)

The output on R Console is



Data Frames in R

Data Frame is used for storing data in tables, and this tabular data can have multiple types of vectors like numeric, characters etc. Data Frame can be created using data.frame() function.

#A Character Vector
string_vector <-c("Ashish", "Awasthi","R")

#A Numeric Vector
numeric_vector <- c(10, 20, 30.5)

#A Boolean Vector
boolean_vector<- c(TRUE, FALSE, TRUE)

# Create the data frame using all 3 vectors
test_df <- 	data.frame(string_vector,numeric_vector,boolean_vector)

#Print result
print(test_df)

The output on R Console



Though this post gives an idea of using variables, In the next post, we'll learn more about using variables in R programming.

Cheers :) Happy Learning