3. R - objects
In R programming language,
• Variables are not declared as some data types
• Variables are assigned with R – objects
• The data type of R-object becomes the data
type of the variable.
NOTE: -> everything is just R - object
4. Basic Data Types in R
R Programming works with various data types,
–Scalars
–Vectors
–Matrices
–Factors
–Data frames
–Lists
6. VECTORS
• A vector is a one-dimensional array.
• We can create a vector with all the basic data
type we learnt before.
• The simplest way to build a vector in R, is to
use the ‘ c ‘ command.
Ex: num_vec <- c(1, 2, 3, 4)
chr_vec <- c(“a”, ”b”, ”c”)
7. MATRIX
• A matrix is a 2-dimensional array that has m
number of rows and n number of columns.
• In other words, matrix is a combination of two or
more vectors with the same data type.
NOTE: In R, more than two-dimensional arrays
can also be created
Semantic:
matrix( data, nrow, ncol, byrow = TRUE/FALSE)
8. FACTORS
• Factor is a variable in R which take on a limited
number of different values; such variables are
often referred to as categorical variables.
• In other words, R stores categorical variables into
a factor.
Semantic:
factor(x = character(), levels, labels = levels,
ordered = is.ordered(x) )
9. Data frames
• A data frame is a list of vectors which are of
equal length.
• A matrix contains only one type of data, while
a data frame accepts different data types
(numeric, character, factor, etc.).
Semantic:
data.frame( df, stringsAsFactors = TRUE)
10. Lists
• A list store many kinds of object in the order expected.
• It can include matrices, vectors data frames or lists.
• A list is similar; we can store a collection of objects and
use them when we need them.
Semantic:
list( element_1, element_2, ... )
arguments: -element_1: store any type of R object -...:
pass as many objects as specifying.
Each object needs to be separated by a comma.