R Objects

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

• In every computer language variables provide a means of

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

class The class of the object.


A comment on the object; often a description of what
comment the object means.

dim Dimensions of the object.


Names associated with each dimension of the
dimnames object.
Returns the names attribute of an object. Results
depend on object type; for example, it returns the
name of each data column in a data frame or each
names named object in an array.
The name of each row in an object (related to
row.names dimnames).

tsp Start time for an object. Useful for time series data.

levels Levels of a factor.


R consists of a number of data objects to perform various functions. There
are 6 types of objects in R Programming. They include vector, list, matrix,
array, factor, and data frame.

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

A vector can be created using the combine function c() as in


x <- c (674 , 4186 , 5308 , 5083 , 6140 , 6381)
x
[1] 674 4186 5308 5083 6140 6381

We can also select a subset of elements by index values using the combine
function c().
x[c(1,3,4)]

[1] 674 5308 5083

Or, if we are interested in a range of indexed values such as index 2 through 4,


we use the : operator.
x[2:4]

[1] 4186 5308 5083

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)

[1] 4.5 4.0 3.5 3.0 2.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:

> seq (from = -2.7, to = 1.3, length.out = 9)

[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

x <- c (674 , 4186 , 5308 , 5083 , 6140 , 6381)


x
[1] 674 4186 5308 5083 6140 6381

Character Vector
In case of character vectors, a vector can store characters.
x <- c("all", "b", "olive")
x

[1] "all" "b" "olive"

Integer Vector
In case of integer vectors, a vector can store integers.

v1 <- c(1L, 4L, 2L, 5L)

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

[1] TRUE FALSE TRUE NA

Complex Vector
The complex data type is to store numbers with an imaginary component
v1 <- c(1+2i, 3i, 4-5i, -12+6i)
v1

[1] 1+2i 0+3i 4-5i -12+6i

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

# Access only the 3rd column.


print(P[,3])
row1 row2 row3 row4
5 8 11 14

The transpose of matrix is given by t(x)

# 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 <- array(1:24, dim = c(3, 4, 2))


> my.array
,,1
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
,,2
[,1] [,2] [,3] [,4]
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24
Using dimensions to extract values
Extracting values from an array with any number of dimensions is completely equivalent to
extracting values from a matrix. You separate the dimension you want to retrieve with commas,

> 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 <- factor(c("single", "married", "married", "single"));

> x

[1] single married married single

Levels: married single

> x <- factor(c("single", "married", "married", "single"), levels =


c("single", "married", "divorced"));

> x

[1] single married married single

Levels: single married divorced

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

Accessing elements of a List


In order to access the list elements, use the index number, and in case of
named list, elements can be accessed using its name also.
# To access the first element of the list.
print(Glist[1])
"G"

# To access the third element.


print(Glist[3])
65 21 80

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.

Following are the characteristics of a data frame.

• The column names should be non-empty.


• The row names should be unique.
• The data stored in a data frame can be of numeric, factor or character
type.
• Each column should contain same number of data items.

We can create a data frame using the data.frame() function.

> employee <- c('John Doe','Peter Gynn','Jolie Hope')


> salary <- c(21000, 23400, 26800)
> startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))

Next, we combine the three vectors into a data frame using the following code:

> employ.data <- data.frame(employee, salary, startdate)

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" ...

You might also like