Professional Documents
Culture Documents
RStudio
RStudio
RStudio
Data Types
• A data type is a classification of data which tells the compiler or
interpreter how the programmer intends to use the data.
• Variables can store data of different types, and different types
can do different things.
• In R, variables do not need to be declared with any particular
type, and can even change type after they have been set:
• y_var <- 30 # my_var is type of numeric
my_var <- "Sally" # my_var is now of type character (a string)
OUTPUT:
• "Sally"
Basic Data Types
as an integer)
• complex - (9 + 3i, where "i" is the imaginary part)
• character (string) - ("k", "R is exciting", "FALSE", "11.5")
• logical ( boolean) - (TRUE or FALSE)
We can use the class() function to check the data type of a
variable:
# numeric
x <- 10.5
class(x)
# integer
x <- 1000L
class(x)
"numeric"
# complex "integer"
x <- 9i + 3
class(x) "complex"
# character/string "character"
x <- "R is exciting"
class(x)
"logical"
# logical/boolean
x <- TRUE
class(x)
Integer:
• Data without decimals.
• This is used when you are certain that you will never create a
variable that should contain decimals.
• To create an integer variable, you must use the letter L after the
integer value:
x <- 1000L
y <- 55L
# Print values of x and y
1000
x
55
y “integer"
# Print the class name of x and y "integer"
class(x)
class(y)
Type Conversion
• You can convert from one type to another with the following
functions:
x <- 1L # integer
as.numeric() y <- 2 # numeric
# convert from integer to numeric: "numeric"
as.integer() a <- as.numeric(x)
"integer"
as.complex() # convert from numeric to integer:
b <- as.integer(y)
# print values of x and y
x
y
# print the class name of a and b
class(a)
class(b)
Built-in Math Functions
• R also has many built-in math functions that allows you to perform
mathematical tasks on numbers.
• For example, the min() and max() functions can be used to find the lowest or
highest number in a set:
• min() and max() functions can be used to find the lowest or highest number
in a set.
• The function sqrt() returns square root of a number
sqrt(16)
R Strings
• Strings are used for storing text.
• A string is surrounded by either single quotation marks, or double quotation
marks:
• "hello" is the same as 'hello‘
• String Length: to find the number of characters in a string, use the nchar()
function:
nchar(str)
Operators
• R divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Miscellaneous operators
R Arithmetic Operators
• Arithmetic operators are used with numeric values to perform common mathematical operations:
R Assignment Operators
my_var <<- 3
3 -> my_var
3 ->> my_var
Note:
• <<- is a global assigner
• It is also possible to turn the direction of the assignment operator.
x <- 3 is equal to 3 -> x
R Comparison Operators
R Vectors
• A vector is simply a list of items that are of the same type.
• To combine the list of items to a vector, use the c() function and
separate the items by a comma.
# Print fruits
fruits
"banana" "apple" "orange"
# Vector with numerical decimals in a sequence 1.5 2.5 3.5 4.5 5.5 6.5
numbers1 <- 1.5:6.5 1.5 2.5 3.5 4.5 5.5
numbers1
length(fruits)
3
Sort a Vector
To sort items in a vector alphabetically
or numerically, use
the sort() function:
fruits <- c("banana", "apple", "orange", "mango", "lemon")
numbers <- c(13, 3, 5, 7, 20, 2)
Example
fruits <- c("banana", "apple", "orange", "mango", "lemon")
# Print fruits
fruits
Output :
"pear" "apple" "orange" "mango" "lemon"
R Lists
A list in R can contain many different data types inside it. A list is a collection of
data which is ordered and changeable.
To create a list, use the list() function:
Example :
list<-list("hello",c(1,2,3,4),45.6,"hi")
print(list)
• Example 2:
• thislist <- list("apple", "banana", "cherry")
thislist[1]
Naming elements in a list :
• list<-list("hello",c(1,2,3,"4"),45.6,"hi")
• names(list)<-c("string","vector","double","string")
• print(list)
Using unlist() to perform arithmetic:
list1=list(5:9)
list2=list(20:24)
v1=unlist(list1)
v2=unlist(list2)
print(v1)
print(v2)
result=v1+v2
print(result)
Merging of lists [[1]]
[1] 1
• Merging of list can be done by placing the lists in
the c() function or the list function. [[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] "Sun"
[[5]]
[1] "Mon"
[[6]]
[1] "Tue"
R – Matrices
Example
• # Create a matrix
thismatrix <- matrix(c(1,2,3,4,5,6), nrow = 3, ncol = 2)
thismatrix
Access Matrix Items
thismatrix[1, 2]
byrow : represent a logical clue . When set to
FALSE , then the elements in input vector are
organized by column
• thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow =
2, ncol = 2,byrow=FALSE)
• thismatrix
byrow : represent a logical clue . When set to
TRUE , then the elements in input vector are
organized by row
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2,
ncol = 2,byrow=TRUE)
thismatrix
dimname : is the name assigned to rows and
columns
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)
rownames=c('a','b')
colnames=c(1,2)
thismatrix
How to access elements in a matrix?
rownames=c("row1","row2","row3")
colnames=c("col1","col2","col3","col4")
p=matrix(c(2:13),nrow=3,byrow=TRUE, dimnames=list(rownames,
colnames))
print(p[1,3])
print(p[3,2])
Matrix where no data source is provided
Y<-matrix(nrow=2, ncol=2)
Y
Arithmetic operations on matrices
• Arithmetic operations such as addition, subtraction, multiplication &
Division can be performed on Matrices
• For performing arithmetic operations, the two matrices must be of
the same dimensions.
• The result generated is also a matrix.
MATRIX 1
[,1] [,2] [,3]
[1,] 0 -2 8
[2,] 5 3 7
MATRIX 2
[,1] [,2] [,3]
[1,] 9 0 5
[2,] 2 1 3
Result of Addition
[,1] [,2] [,3]
[1,] 9 -2 13
[2,] 7 4 10
Result of Subraction
[,1] [,2] [,3]
[1,] -9 -2 3
[2,] 3 2 4
Result of Multiplication
[,1] [,2] [,3]
[1,] 0 0 40
[2,] 10 3 21
Result of DIvision
[,1] [,2] [,3]
[1,] 0.0 -Inf 1.600000
[2,] 2.5 3 2.333333
# create a vector of elements
vector1=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)
# display matrix
print(matrix1)
# display matrix
print(matrix2)
# add matrices
print(matrix1+matrix2)
Creating Matrix taking vector of numbers as input
# Elements arranged sequentially by row
print(M)
print(N)
print(P)
Arithmetic operations on Matrices :
Matrix1 <- matrix(c(1,3,-4,5), nrow = 2, ncol = 2)
print(Matrix1)
Matrix2 <- matrix(c(3,-5,7,6), nrow = 2, ncol = 2)
print(Matrix2)
result=Matrix1+Matrix2
print(result)
result=Matrix1-Matrix2
print(result)
Arrays
• Array are R objects which can store data in more than two
dimensions.
• Function array() function takes vectors as input & uses dim parameter
values to create array.
,,1
,,2
multiarray[2, 3, 2]
thisarray <- c(1:24)
multiarray <- array(thisarray, dim = c(4, 3, 2))
print(multiarray)
multiarray[,, 2]
FACTORS IN R PROGRAMMING :
• Factors are implemented to categorize the data or represent categorical data
and store it on multiple levels.
• Can store both integers and strings
• R factor accepts only a restricted number of distinct values.
• For example, a data field such as gender may contain values only from female,
male, or other.
• These distinct values are known as levels.
• After a factor is created it only consists of levels that are by default sorted
alphabetically.
• Helpful in the data analysis for statistical modelling
• Using a function called as factor()
• Input as vector values.
Creating a Factor in R Programming Language