Functions and Operators in R

R Tutorial 7.0

We yet need to go too far on the journey of R, so better we learn more of the basic stuff about it now itself. In this article, we are covering operators and in-built functions available in R, few of which we have already used in previous articles. Now, let's let learn these in a well structured approach !

First let's understand the arithmetic operators available in R.

A  =  5+4 
gives 9
B  =  5-4 
gives 1
C  =  5*4 
gives 20
D  =  5/4 
gives 1.25
E  =  2**3    or     E = 2^3 
gives 8
F  =  9%%2
gives 1 as it basically is MOD(9,2) which means what is remainder when 9 is divided by 2.
G  =  9%/%
gives 4, basically it gives floor of the quotient, so when 9 is divided by 2, it gives 4.5, floor of which is 4.
G  =  -9%/%2 
give -5, which is fine with above stated logic.

These are basic logical operators :

<less than
<=less than or equal to
>greater than
>=greater than or equal to
==exactly equal to
!=not equal to
!xNot x
x | yx OR y
x & yx AND y

Let's now understand the basic numeric function available in R

abs(x)absolute value
sqrt(x)square root
ceiling(x)ceiling(3.475) is 4
floor(x)floor(3.475) is 3
trunc(x)trunc(5.99) is 5
round(x, digits=n)round(3.475, digits=2) is 3.48
signif(x, digits=n)signif(3.475, digits=2) is 3.5
cos(x), sin(x), tan(x)also acos(x), cosh(x), acosh(x), etc.
log(x)natural logarithm
log10(x)common logarithm

Let's learn these by example :

#Let's first clear the workspace
rm(list = ls())

A = abs(-4.5)     # Gives 4.5

B = sqrt(4)          # Gives 2

C = 4^0.5            # Again gives 2

D = ceiling(4.5)     # Gives closest integer on higher side i.e. 5
E = ceiling(-4.5)    # Gives closest integer on higher side i.e. -4

F = floor(4.5)        # Gives closest integer on lower side i.e.4
G = floor(-4.5)       # Gives closest integer on lower side i.e.-5

H = trunc(5.88999)    # Gives only integer part of the number i.e.  5
I = trunc(-5.88999)    # Gives only integer part of the number i.e.  -5

J = round(34.5564,digits = 2)    # Gives resultant number with 2 places after decimal i.e. 34.56 

K = signif(34.5564,digits = 3)    # Gives resultant number rounded off to 3 significant digits i.e. 34.6

it's time to learn character function available in R

This is my favorite part.

1. Substr Function

Sub-string function first : substr(x, start=n1, stop=n2)

It can be used two ways :

First for extraction :

x ="abcdef"
A = substr(x, 2, 4)   # would give "bcd"

Also for replacement of letters on the basis of position :

substr(x, 2, 4) = "123"   # would basically impute 2nd, 3rd, and 4th position with "1","2" and "3".

2.  Paste Function

This function is used to concatenate the strings:

List_1 = data.frame(Name = c("Rajat","Vinod","Shobhit","Arun"), 
                    Age = c(28,30,31,33), 
                  Education = c("Engineering","M.Sc.","Engineering","MBBS"))

List_1$Name_with_salutation = paste("Mr.",List_1$Name, sep = " " )
List_1$Name_with_Degree = paste("Mr.", List_1$Name,List_1$Education, sep = " " )

You can add N number of columns and "String" within a single paste command.

It is similar to Catx function of SAS.

3.   Functions for changing cases

List_1$All_caps = toupper(List_1$Name)
List_1$none_caps = tolower(List_1$Name)

4.   String replacement

List_1 = data.frame(Name = c("Rajat","Vin od","Shobhit","Arun"), 
                    Age = c(28,30,31,33), 
                  Education = c("Engineering","M.Sc.","Engineering","MBBS"))

List_1$Try = sub("no", "#$", List_1$Name)
List_1$Try = sub("a", "w", List_1$Name , =TRUE)

When fixed = TRUE, it would do not consider the case of the text.

We would be writing more about functions in the topic specific blog later.

Enjoy reading our other articles and stay tuned with us.

Kindly do provide your feedback in the 'Comments' Section and share as much as possible.

No comments:

Post a Comment

Do provide us your feedback, it would help us serve your better.