2. Contents
2
Variable assignment in R
Numerical Operators in R
In built functions in R
Infinity, NA and NAN values in R
Atomic data types in R
Coercion
3. Variable Assignment in R
3
Variables:
Variables are used to store data, which can vary
Unique name given to variable (function and objects as well) is
known as identifier.
To assign value to a variable named ‘x’
x <- value or x = value
x <<- value or value -> x or value ->> x
4. Variable Names
4
Some Rules:
✓ An identifier can contain letters, numbers, dots(.) and underscores ( _ )
✓ It should not have other special characters or blank space in the name
✓ It should not start with a dot followed by a number (eg:- .2iota)
✓ It should not start with an underscore or a number (eg:- _iota or 1more)
✓ It should not be a reserved keyword like for, if, else, in, next, etc.
Note: R is case-sensitive
5. Variable Names
5
Not Allowed Allowed
monthly salary monthly_salary or
monthly.salary or
MonthlySalary
%growth perc_growth
This&that This.that
2ndhighest Second.highest
Some Examples:
6. Numerical Operators in R
6
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
%/% Integer division
%% Modulo (estimates remainder in a division)
^ or ** Exponentiation
7. Relational Operators in R
7
Operator Description
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Exactly equal to
!= Not equal to
8. Logical Operators in R
8
Operator Description
x |y Element-wise OR operator
x & y Element-wise AND operator
! x Not Operator
X || y Takes only first element of iterables
X && y Takes only first element of iterables
9. Inbuilt Mathematical Functions
9
pi; exp(1)
log(x) # log to base e of x
log10(x) # log to base 10 of x
log(x,n) # log to base n of x
floor(x) # greatest integer <x
ceiling(x) # smallest integer >x
lgamma(x) # natural log of gamma (x)
choose(n,x) # Binomial coefficient nCx
sqrt(x); factorial(x); gamma(x)
10. Inbuilt Mathematical Functions
10
trunc(x) # closest integer to x between x and 0
E.g., trunc(1.5) =1, trunc(-1.5) = -1
NOTE: trunc is like floor for positive values and like ceiling for
negative values
round(x, digits=0) # round the value of x to an integer
signif(x, digits=6) # give x to 6 digits in scientific notation
runif(n) # generates n random numbers
between 0 and 1 from a uniform distribution
11. Inbuilt Trigonometrically Functions
11
cos(x) # cosine of x in radians
sin(x) # sine of x in radians
tan(x) # tangent of x in radians
acos(x), asin(x), atan(x) # inverse trigonometric
transformations of real or complex numbers
acosh(x), asinh(x), atanh(x) # inverse hyperbolic
trigonometric transformations of real or complex numbers
abs(x) # the absolute value of x,
ignoring the minus sign if there is one
12. 12
Data types in R
✓ Logical, for example, TRUE, FALSE
✓ Numeric (sometimes called double, usually treated as floating
point number/real number), for example, 11.7, -3, 99.0, 1000
✓ Integer, for example, 25L, 0L, -33L
Specify L suffix to get integer (i.e. 1L gives integer 1)
✓ Complex, for example, 3 – 4i, 4+5i
✓ Character, for example, “abc”, “34”, “TRUE”, “3-4i”, ‘3L’
13. 13
Data types in R
✓ To check the class of variables, class() command can be
used
For example:
class(7); class(7L); class(T); class(‘T’); class(3+0i)
✓ Special numbers such as Inf and NAN are of numeric
class
For example: class(8/0); class(0/0)
14. 14
Coercion
All elements of a vector must be the same type, so when we
attempt to combine different types they will be coerced to the
most flexible type.
Types from least to most flexible are:
.
Logical
Integer
Double/ Numeric
Complex
Character
15. 15
Coercion
When a logical vector is coerced to an integer or double, TRUE
becomes 1 and FALSE becomes 0
x <- c(FALSE, FALSE, TRUE); as.numeric(x)
Total number of TRUEs
sum(x)
Proportion that are TRUE
mean(x)
16. 16
Coercion in R
✓ To forcefully coerce a variable class into other, following
functions are used
as.numeric(), as.logical(), etc.
In Python, we call it ‘typecasting’
https://youtu.be/FJ6IkFycCdA
17. 17
NA’s and NAN’s in R
Inf
Infinity
NA
Not available, generally interpreted as a missing value
The default type of NA is logical, unless coerced to some other type,
so the appearance of a missing value may trigger logical rather than
numeric indexing.
Numeric and logical calculations with NA generally return NA.
NAN
Not a number, e.g., 0/0
18. 18
NA’s and NAN’s in R
✓ is.nan() is used to test for NaN's
✓ is.na() is used to test, if objects are NA's
✓ A NAN value can also be NA but not conversely.
✓ It means is.na also returns TRUE for NaN’s
✓ Functions like mean() and sum() on vector
containing NA or NaN, they will return NA and NaN.
✓ This behavior alerts you to the presence of the bad value.
19. 19
NULL in R
✓ NULL is different from the NA and NAN
✓ NULL means that there is no value, while NA and NaN
mean that there is some value, although one that is
perhaps not usable.
✓ is.null() is used to test for NULL
✓ Functions like mean() and sum() on vector
containing NULL doesn’t pose any issue as NULL value
simply doesn’t exists
20. 20
Data Type Conversions
Use is.foo to test for data type foo.
Returns TRUE or FALSE
Use as.foo to explicitly convert it.
Examples:
is.numeric(), is.character(), is.vector(), is.matrix(), is.data.frame()
as.numeric(), as.character(), as.vector(), as.matrix(), as.data.frame)
21. 21
Handling of missing values
X <- c(1:8,NA)
✓ Removing missing vlaues
mean(X, na.rm = T) or mean(X ,na.rm=TRUE)
✓ To check for the location of missing values within a vector
which(is.na(X))
✓ To assign this a large number, say, 999
X[which(is.na(X))] = 999
22. 22
Handling of missing values
x <- c(1, 2, NA, 4, NA, 5)
✓ Identify missing values
bad <- is.na(x)
✓ To remove missing values
x[!bad]
23. 23
Handling of missing values
x <- c(1, 2, NA, 4, NA, 5); y <- c("a", "b", NA, "d", "e", NA)
df = data.frame(x,y)
✓ To take the subset of data frame with no missing value
good = complete.cases(x,y); good
✓ To take the subset of vector x with no missing value
x[good]
✓ To take the subset of vector y with no missing value
y[good]
24. Function Composition Pipe Operator
https://www.linkedin.com/posts/drnishaarora_programming-r-rstats-activity-6783610405076914176-23p-
https://github.com/arora123/R-for-Data-Science/blob/master/pipe%20operator%20in%20R.R
25. Books
25
✓ Crowley, M. J. (2007). The R Book. Chichester, New
England: John Wiley & Sons, Ltd.
✓ An Introduction to R by W. N. Venables, D. M. Smith and
the R Core Team
✓ R in a Nutshell by Joseph Adler: O’Reilly
✓ Teetor, P. (2011). R cookbook. Sebastopol, CA: O’Reilly
Media Inc.
26. Books
26
✓ Bio Statistics - https://www.middleprofessor.com/files/applied-
biostatistics_bookdown/_book/
✓ Advanced R - https://adv-r.hadley.nz/
✓ Data Visualization - https://rkabacoff.github.io/datavis/
✓ R for Data Science - https://r4ds.had.co.nz/index.html
✓ Data Exploration & Analysis -
https://bookdown.org/mikemahoney218/IDEAR/
✓ https://bookdown.org/mikemahoney218/LectureBook/
29. 29
Reach Out to Me
http://stats.stackexchange.com/users/79100/learner
https://www.researchgate.net/profile/Nisha_Arora2/contributions
https://www.quora.com/profile/Nisha-Arora-9
https://github.com/arora123/
https://www.youtube.com/channel/UCniyhvrD_8AM2jXki3eEErw
https://www.linkedin.com/in/drnishaarora/
Dr.aroranisha@gmail.com