Chapter 2 Data Structures in R
Chapter 2 Data Structures in R
CHAPTER 2
DATA STRUCTURES IN R PROGRAMMING
TYPE OF DATA
2 • FACTORS
STRUCTURES
IN R 3 • MATRICES
4 • ARRAY
A way of organizing data
5
for use in the computer.
• DATA FRAME
6 • LIST
1 • VECTORS A vector is simply a list of items that are of
the same type.
# Vector of strings
fruits <- c("banana", "apple", "orange")
For example;
seq(from, to) seq(1,10)
seq(from, to, by=) seq(1,10,by=2)
seq(1,3,by=0.5)
Sequence seq( ) seq(20,0,by=-5)
seq(from, to, length=) seq(0,20,length=4)
seq(along) seq(5)
seq(1:5)
For example;
Replication rep( )
rep(value,no. of replication) rep(5,5)
1 • VECTORS Selecting element from vector:
x=c(1,4,5,3,9,10,12)
#Example
names = c(“ali", “abu", “siti", “sofea")
gender = c("male", "male", "female", "female")
age = c(25, 21, 30, 27)
occupation = c(“doctor", “lawyer", “doctor", “lawyer”)
Data=data.frame(names,gender,age,occupation)
Observe the above R code. Is there anything you would like to suggest?
6 • LIST A list in R can contain many different data
types inside it. Can be created using
list() function.
# List of strings
thislist <- list("apple", "banana", "cherry")