0% found this document useful (0 votes)
5 views43 pages

Data Structures in R: Vectors and Factors

The document provides an overview of basic data structures in R, including vectors, factors, matrices, lists, and data frames. It explains the definitions, creation methods, and characteristics of each data structure, along with examples and syntax for implementation in R. The content is structured to aid understanding for students in the Department of Mathematics and Statistics at the College of Science.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views43 pages

Data Structures in R: Vectors and Factors

The document provides an overview of basic data structures in R, including vectors, factors, matrices, lists, and data frames. It explains the definitions, creation methods, and characteristics of each data structure, along with examples and syntax for implementation in R. The content is structured to aid understanding for students in the Department of Mathematics and Statistics at the College of Science.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Engineering Data Analysis

Data Structures in R

Department of Mathematics and Statistics


College of Science

1/14
Basic Data Structures in R

Vector

Factor

Matrix

List

Data Frame

2/14
What is a Vector?

A vector is a type of array that is one dimensional.

Vector 3/14
What is a Vector?

A vector is a type of array that is one dimensional.


Vectors are logical element in programming languages that are used for storing data.

Vector 3/14
What is a Vector?

A vector is a type of array that is one dimensional.


Vectors are logical element in programming languages that are used for storing data.
How to create a vector in R?

Vector 3/14
What is a Vector?

A vector is a type of array that is one dimensional.


Vectors are logical element in programming languages that are used for storing data.
How to create a vector in R?
I In R, you create a vector using this syntax
c(<element>, <element>, <element>, ..., <element>).
Note: all elements must be of the same type.

Vector 3/14
What is a Vector?

A vector is a type of array that is one dimensional.


Vectors are logical element in programming languages that are used for storing data.
How to create a vector in R?
I In R, you create a vector using this syntax
c(<element>, <element>, <element>, ..., <element>).
Note: all elements must be of the same type.
I Vector of consecutive numbers
Syntax: <start value>:<end value>

Vector 3/14
What is a Vector?

A vector is a type of array that is one dimensional.


Vectors are logical element in programming languages that are used for storing data.
How to create a vector in R?
I In R, you create a vector using this syntax
c(<element>, <element>, <element>, ..., <element>).
Note: all elements must be of the same type.
I Vector of consecutive numbers
Syntax: <start value>:<end value>
I More complex sequences
Syntax: seq(<start value>, <end value>, by = <interval>)
Syntax: seq(<start value>, <end value>, [Link] = <length of vector>)

Vector 3/14
What is a Vector?

How to create a vector in R?


I Vector Selection
Syntax: <variable>[<index>]
Syntax: <variable>[c(<index>, <index>, <index>, ..., <index>)]

Vector 4/14
What is a Vector?

How to create a vector in R?


I Vector Selection
Syntax: <variable>[<index>]
Syntax: <variable>[c(<index>, <index>, <index>, ..., <index>)]
I Naming a Vector
Syntax: names(<variable>) = c(<element>, ... .<element>)
Syntax: names(<variable>) <- c(<element>, ... .<element>)

Vector 4/14
What is a Factor?

The term factor refers to a statistical data type used to store categorical variables.
The difference between a categorical variable and a continuous variable is that a
categorical variable can belong to a limited number of categories. A continuous
variable, on the other hand, can correspond to an infinite number of values.

Factor 5/14
What is a Factor?

The term factor refers to a statistical data type used to store categorical variables.
The difference between a categorical variable and a continuous variable is that a
categorical variable can belong to a limited number of categories. A continuous
variable, on the other hand, can correspond to an infinite number of values.
Two types of categorical variables:

Factor 5/14
What is a Factor?

The term factor refers to a statistical data type used to store categorical variables.
The difference between a categorical variable and a continuous variable is that a
categorical variable can belong to a limited number of categories. A continuous
variable, on the other hand, can correspond to an infinite number of values.
Two types of categorical variables:
I Nominal categorical variable

Factor 5/14
What is a Factor?

The term factor refers to a statistical data type used to store categorical variables.
The difference between a categorical variable and a continuous variable is that a
categorical variable can belong to a limited number of categories. A continuous
variable, on the other hand, can correspond to an infinite number of values.
Two types of categorical variables:
I Nominal categorical variable is a categorical variable without an implied order.
This means that it is impossible to say that ’one is worth more than the other’.

Factor 5/14
What is a Factor?

The term factor refers to a statistical data type used to store categorical variables.
The difference between a categorical variable and a continuous variable is that a
categorical variable can belong to a limited number of categories. A continuous
variable, on the other hand, can correspond to an infinite number of values.
Two types of categorical variables:
I Nominal categorical variable is a categorical variable without an implied order.
This means that it is impossible to say that ’one is worth more than the other’.
I Ordinal categorical variable

Factor 5/14
What is a Factor?

The term factor refers to a statistical data type used to store categorical variables.
The difference between a categorical variable and a continuous variable is that a
categorical variable can belong to a limited number of categories. A continuous
variable, on the other hand, can correspond to an infinite number of values.
Two types of categorical variables:
I Nominal categorical variable is a categorical variable without an implied order.
This means that it is impossible to say that ’one is worth more than the other’.
I Ordinal categorical variable has a natural ordering

Factor 5/14
What is a Factor?

How to create a Factor in R?


I To create factors in R, you make use of the function factor().
Syntax: factor(<vector>)

Factor 6/14
What is a Factor?

How to create a Factor in R?


I To create factors in R, you make use of the function factor().
Syntax: factor(<vector>)
First thing that you have to do is create a vector that contains all the
observations that belong to a limited number of categories.

Factor 6/14
What is a Factor?

How to create a Factor in R?


I To create factors in R, you make use of the function factor().
Syntax: factor(<vector>)
First thing that you have to do is create a vector that contains all the
observations that belong to a limited number of categories.
Example: Out of 60 respondents considered in the survey, thirty of them
indicates that they are single, twenty of them are married and ten of them are
separated. Create a factor, based on the information given.

Factor 6/14
Factor
How to create a Factors in R?
I To create an ordered factor, you have to add two additional arguments: ordered
and levels. By setting the argument ordered to TRUE in the function factor(),
you indicate that the factor is ordered. With the argument levels you give the
values of the factor in the correct order.
Syntax: factor(<vector>, ordered = TRUE, levels = c(<ordered
vector>)).

Factor 7/14
Factor
How to create a Factors in R?
I To create an ordered factor, you have to add two additional arguments: ordered
and levels. By setting the argument ordered to TRUE in the function factor(),
you indicate that the factor is ordered. With the argument levels you give the
values of the factor in the correct order.
Syntax: factor(<vector>, ordered = TRUE, levels = c(<ordered
vector>)).
Example: I asked 70 students if they like watching television. Fifty of them
answered ”Most of the Time”, fifteen of them answered ”Sometimes” and only
five student answered the question as ”Hardly Ever”. Create a factor, based on
the information given.

Factor 7/14
What is a Matrix?

Matrix 8/14
What is a Matrix?
I A matrix is a two-dimensional array of numbers, having a fixed number of rows
and columns, and containing a number at the intersection of each row and each
column.

Matrix 8/14
What is a Matrix?
I A matrix is a two-dimensional array of numbers, having a fixed number of rows
and columns, and containing a number at the intersection of each row and each
column.
I By storing values in a matrix rather than as individual variables, a program can
access and perform operations on the data more efficiently.

Matrix 8/14
What is a Matrix?
I A matrix is a two-dimensional array of numbers, having a fixed number of rows
and columns, and containing a number at the intersection of each row and each
column.
I By storing values in a matrix rather than as individual variables, a program can
access and perform operations on the data more efficiently.
How to create a matrix in R?
I Syntax: matrix(<data>, nrow = <n>, ncol =<n>, byrow = <T/F>, dimnames = NULL)

Matrix 8/14
What is a Matrix?
I A matrix is a two-dimensional array of numbers, having a fixed number of rows
and columns, and containing a number at the intersection of each row and each
column.
I By storing values in a matrix rather than as individual variables, a program can
access and perform operations on the data more efficiently.
How to create a matrix in R?
I Syntax: matrix(<data>, nrow = <n>, ncol =<n>, byrow = <T/F>, dimnames = NULL)
Example: Create matrix based on table:

Matrix 8/14
Matrix

How to create a matrix in R?


I Example: Create a 6 x 5 matrix that contains a number from 1 to 30. Fill
matrix by rows. Make sure that the names of the rows are A, B, C, D, E, and
F, and the names of the columns are Blue, Red, White, Green, and Yellow.

Matrix 9/14
Matrix

How to create a matrix in R?


I Example: Create a 6 x 5 matrix that contains a number from 1 to 30. Fill
matrix by rows. Make sure that the names of the rows are A, B, C, D, E, and
F, and the names of the columns are Blue, Red, White, Green, and Yellow.
I Extract a Row and Column from a Matrix

Matrix 9/14
Matrix

How to create a matrix in R?


I Example: Create a 6 x 5 matrix that contains a number from 1 to 30. Fill
matrix by rows. Make sure that the names of the rows are A, B, C, D, E, and
F, and the names of the columns are Blue, Red, White, Green, and Yellow.
I Extract a Row and Column from a Matrix
I The entire mth row of a matrix can be extracted as <matrixname>[m,].

Matrix 9/14
Matrix

How to create a matrix in R?


I Example: Create a 6 x 5 matrix that contains a number from 1 to 30. Fill
matrix by rows. Make sure that the names of the rows are A, B, C, D, E, and
F, and the names of the columns are Blue, Red, White, Green, and Yellow.
I Extract a Row and Column from a Matrix
I The entire mth row of a matrix can be extracted as <matrixname>[m,].
I The entire nth column of a matrix can be extracted by <matrixname>[,n].

Matrix 9/14
Matrix

How to create a matrix in R?


I Example: Create a 6 x 5 matrix that contains a number from 1 to 30. Fill
matrix by rows. Make sure that the names of the rows are A, B, C, D, E, and
F, and the names of the columns are Blue, Red, White, Green, and Yellow.
I Extract a Row and Column from a Matrix
I The entire mth row of a matrix can be extracted as <matrixname>[m,].
I The entire nth column of a matrix can be extracted by <matrixname>[,n].
I To extract more than one rows at a time: matrixname[c(<index>),]

Matrix 9/14
Matrix

How to create a matrix in R?


I Example: Create a 6 x 5 matrix that contains a number from 1 to 30. Fill
matrix by rows. Make sure that the names of the rows are A, B, C, D, E, and
F, and the names of the columns are Blue, Red, White, Green, and Yellow.
I Extract a Row and Column from a Matrix
I The entire mth row of a matrix can be extracted as <matrixname>[m,].
I The entire nth column of a matrix can be extracted by <matrixname>[,n].
I To extract more than one rows at a time: matrixname[c(<index>),]
I To extract more than one columns at a time: matrixname[,c(<index>)]

Matrix 9/14
Matrix

How to create a matrix in R?


I Example: Create a 6 x 5 matrix that contains a number from 1 to 30. Fill
matrix by rows. Make sure that the names of the rows are A, B, C, D, E, and
F, and the names of the columns are Blue, Red, White, Green, and Yellow.
I Extract a Row and Column from a Matrix
I The entire mth row of a matrix can be extracted as <matrixname>[m,].
I The entire nth column of a matrix can be extracted by <matrixname>[,n].
I To extract more than one rows at a time: matrixname[c(<index>),]
I To extract more than one columns at a time: matrixname[,c(<index>)]

Matrix 9/14
What is List in R?

When data cannot be represented as an array or a data frame, the list is the best
choice. This is because lists can contain all kinds of other objects, including other
lists, matrix or data frames, and in that sense, they are very flexible.

List 10/14
What is List in R?

When data cannot be represented as an array or a data frame, the list is the best
choice. This is because lists can contain all kinds of other objects, including other
lists, matrix or data frames, and in that sense, they are very flexible.
How to create a list in R?
You can create a list in R using list(<arguments>) function in R.
Example: Create a list that contains a numeric vector (1 to 30), a sequence of
number from 1 to 10 with step size of 0.5, and a character vector that contains your
top 3 favourite subject.

List 10/14
What is Data Frame in R?

A data frame is a table or a two-dimensional array-like structure in which each


column contains values of one variable and each row contains one set of values from
each column.

Data Frame 11/14


What is Data Frame in R?

A data frame is a table or a two-dimensional array-like structure in which each


column contains values of one variable and each row contains one set of values from
each column.
Characteristics of a data frame:
I The column names should be non-empty.

I The row names should be unique.

I The data stored in a data frame can be of numeric, factor or character type.

I Each column should contain same number of data items.

Data Frame 11/14


What is Data Frame in R?

How to create a data frame in R?


I To create a data frame, use the [Link](<vector>,...,<vector>)
function.
Example: Create a data frame of 5 variables with 3 observations.

Data Frame 12/14


What is Data Frame in R?

How to create a data frame in R?


I To create a data frame, use the [Link](<vector>,...,<vector>)
function.
Example: Create a data frame of 5 variables with 3 observations.
I If you want to extract a particular variable from a data frame, use
<dataname>$<variablename>.

Data Frame 12/14


What is Data Frame in R?
How to create a data frame in R?
I To take a subset from a data frame, first create a new data frame and use the
subset command: subset(<dataname>, <condition>)

Data Frame 13/14


What is Data Frame in R?
How to create a data frame in R?
I To take a subset from a data frame, first create a new data frame and use the
subset command: subset(<dataname>, <condition>)
Logical Comparison Operators

I < less than I >= greater than or equal


I > greater than I == for equal to each other
I <= less than or equal I ! = not equal to each other

The operator & are used to denote multiple conditions.

Data Frame 13/14


What is Data Frame in R?
How to create a data frame in R?
I To take a subset from a data frame, first create a new data frame and use the
subset command: subset(<dataname>, <condition>)
Logical Comparison Operators

I < less than I >= greater than or equal


I > greater than I == for equal to each other
I <= less than or equal I ! = not equal to each other

The operator & are used to denote multiple conditions.


Example: Create a data frame from ”Encoded Data” file. Include only the male
respondents that are single, at most 40 years old, and high school graduate.
Data Frame 13/14
References

I [Link]
I [Link]
I [Link] data analysis r4/introducing-r-and-
[Link]

Thank You!

Data Frame 14/14

You might also like