R Objects
R Objects
R Objects
accessing the data stored in memory. R does not provide direct access
to the computer’s memory but rather provides a number of specialized
data structures we will refer to as objects. These objects are referred to
through symbols or variables. In R, however, the symbols are
themselves objects and can be manipulated in the same way as any
other object.
• To each objects in R , there are some attributes and functions
associated with it.
• Attributes refer to the intrinsic properties possessed by a particular
object in R.
• Some of the objects in R are:-
Arrributes Description
tsp Start time for an object. Useful for time series data.
Vectors Matrices
Data Objects in
Frame R Array
List Factors
1. Vectors
The atomic vector (or vector for short) is the simplest data structure in R
which consists of an ordered set of values of the same type (e.g. numeric,
character, date, etc).
R has six basic (‘atomic’) vector types: logical, integer, real, complex, string (or
character) and raw.
typeof mode storage.mode
logical logical logical
integer numeric integer
double numeric double
complex complex complex
character character character
raw raw raw
We can also select a subset of elements by index values using the combine
function c().
x[c(1,3,4)]
To create a vector from a simple sequence of integers, for example, you use
the colon operator (:).
For a vector with the numbers 4.5 to 2.5 in steps of ‐0.5, for example, you use
the following code:
> seq (from = 4.5, to = 2.5, by = -0.5)
Alternatively, you can specify the length of the sequence by using the
argument length.out. R calculates the step size itself. You make a vector of
nine values going from –2.7 to 1.3 like this:
[1] -2.7 -2.2 -1.7 -1.2 -0.7 -0.2 0.3 0.8 1.3
Numeric Vector
In case of character vectors, a vector can store numerals
Character Vector
In case of character vectors, a vector can store characters.
x <- c("all", "b", "olive")
x
Integer Vector
In case of integer vectors, a vector can store integers.
v1
[1] 1 4 2 5
Logical Vector
Logical vectors are the simplest type of atomic vector as they take only three
possible values: FALSE, TRUE, and NA.
v1 <- c(TRUE, FALSE, TRUE, NA)
v1
Complex Vector
The complex data type is to store numbers with an imaginary component
v1 <- c(1+2i, 3i, 4-5i, -12+6i)
v1
2. Matrices
Matrices are the R objects in which elements of same data types are arranged in 2D
layout. An matrix can be considered as a 2 dimensional array in R.
A Matrix is created using the matrix() function.
The basic syntax for creating a matrix in R is −
matrix(data, nrow, ncol, byrow, dimnames)
Following is the description of the parameters used −
• data is the input vector which becomes the data elements of the matrix.
• nrow is the number of rows to be created.
• ncol is the number of columns to be created.
• byrow is a logical clue. If TRUE then the input vector elements are arranged by row.
• dimname is the names assigned to the rows and columns.
For Ex-
P <- matrix(1:12, ncol = 4)
>P
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
Elements of a matrix can be accessed by using the column and row index of the element.
Access the element at 3rd column and 1st row.
print(P[1,3])
[1] 5
# Access the element at 2nd column and 4th row.
print(P[4,2])
[1] 13
# Access only the 2nd row.
print(P[2,])
col1 col2 col3
6 7 8
# Transpose of a matrix
T(P)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 8
[3,] 7 8 9
[,4] 10 11 12
3. Array
An array is a multi-dimensional object in R containing only a single type of value.
An array can be considered as a generalized form of vectors and matrices in R.
You can create an array easily with the array() function, where you give the data
as the first argument and a vector with the sizes of the dimensions as the second
> my.array[2, 3, 1]
[1] 8
4. Factors
Factors are the data objects which are used to categorize the data and store
it as levels. They can store both strings and integers. They are useful in the
columns which have a limited number of unique values. Like "Male, "Female"
and True, False etc. They are useful in data analysis for statistical modeling.
Factors are created using the factor () function by taking a vector as input.
> x
> x
5. Lists
An R list (“generic vectors”) is an object consisting of an ordered collection of
objects known as its components. Lists can contain any type of R object, i.e. the
elements of a list do not have to be of the same type. Lists provide a convenient
way to return the results of a statistical computation.
OR
A list is an object in R composed of ordered set of components stored in a 1D
vector. In fact, it’s another kind of vector called a recursive vector where each
vector element can be of different data type and structure, i.e., a list can hold
complex objects such as matrices, data frames, strings, numbers, vectors,
matrices, functions and a nested list inside it and other list objects too.
# Creating a list.
Glist <- list("Gk", "RList”, c(65, 21, 80), TRUE, 27.02, 10.3)
print(Glist)
[[1]]
[1] "G"
[[2]]
[1] "RList"
[[3]]
[1] 65 21 80
[[4]]
[1] TRUE
[[5]]
[1] 27.02
[[6]]
[1] 10.3
6. Data Frames
Dataframe is a 2-dimensional object in R wherein each column consists of
the value of variable and each row consists of a value set from each
column. It can be thought as an extension of the matrix object in that, unlike
a matrix, a data frame can mix data types across columns (e.g. both
numeric and character columns can coexist in a data frame) but data type
remains the same across rows. We can store numeric data, factor data, and
so on. The most important distinction is that in a data frame that the
members must all be vectors of equal length.
Next, we combine the three vectors into a data frame using the following code:
The result of this is a data frame, employ.data, with the following structure:
> str(employ.data)
'data.frame': 3 obs. of 3 variables:
$ employee : Factor w/ 3 levels "John Doe","Jolie Hope",..: 1 3 2
$ salary : num 21000 23400 26800
$ startdate: Date, format: "2010-11-01" "2008-03-25" ...