1.4 Getting Data Into R 2
1.4 Getting Data Into R 2
Introduction to R Programming
for Data Science
Getting Data into R 2
Objective
Create vectors in the R programming language,
Create factors in the R programming language,
Create a list in the R programming language,
Access and modify the list elements.
3
Vector
A vector is a one-dimensional array of objects, used for storing
data.
Create vectors in R
4
Vector
Create vectors in R: using c()
5
Vector
Create vectors in R: using a:b
a:b function takes two numeric scalars a and b as arguments,
and returns a vector of numbers from the starting point a to
the ending point b in steps of 1
Example
6
Vector
Create vectors in R: using seq()
seq(): create a sequence from a starting number to an ending
number
Argument Definition
from The start of the sequence
to The end of the sequence
by The step-size of the sequence
The desired length of the final sequence (only use if you don’t
length.out
specify by)
7
Factors
Factor in R is a variable used to categorize and store the data, having a limited
number of different values. It can stores both string and integer data values as
levels.
They are useful in data analysis for statistical modeling.
Syntax
factor(x = character(), levels, labels = levels, ordered = is.ordered(x))
8
Factors
9
Vector Operations
11
Vector Operations
12
Vector Operations
13
Arithmetic operations on vectors
Arithmetic operations on vectors
Basic arithmetic operations: +, -, *
Example
14
Vector Operations
15
Lists
is a collection of objects in which the elements inside can
differ in terms of data type
16
Lists
17
Lists
18
Lists
20
Summary
At this point in the course, you know:
• A vector is a string of numbers, characters, or logical data.
• Factors (also known as categorical variables) are variables that take on
a limited number of different values that can be nominal or ordinal.
• You can use R to perform operations on a vector, such as sorting the
items, finding the smallest or largest number, or performing arithmetic
on its values.
• Lists can store different types of data, unlike vectors, which can only
store data of a single type.
21
Q&A