Basic Objects
Data objects are the fundamental items that you work with in R.Every task
involves various different types of objects. Each object has a different goal and
behavior. There are many ways to manipulate your data, and understanding
how to do this is important in learning about R because the more you know
about the way R handles objects, the better use you can make of R as an ana-
lytical tool.
R objects
1. Vector
2. Matrix
3. Array
4. List
5. Factors
6. Data frames
Vector
A vector is the basic data structure in R, or we can say vectors are the most
basic R data objects. A vector is a group of primitive values of the same type.
It can be a group of numbers, true/false values, texts, and values of some other
type. It is one of the building blocks of all R objects.
There are several types of vectors in R. They are distinct from each other
in the type of elements they store.
Types of vectors in R
1. Numeric vector
2. Logical vector
3. Character vector
4. Complex Vector
5. Raw Vector
Creating Vectors
Numeric vector A numeric vector is a vector of numeric values. A scalar
number is the simplest numeric vector.
Example: x < −1.5 (or) x = 1.5
Vectors of numeric type can be created by using any one of the following
commands.
Vectors are generally created using the c() function.
numeric() is used to create a zero vector of a given length.
A sequence of vectors can be created by using colon operator (:).
A more general way to produce a numeric sequence is seq().
Examples:
1. x < −c(1, 5, 4, 9, 0)
Output: [1] 1 5 4 9 0
2. x1 < −numeric(10)
Output: [1] 0 0 0 0 0 0 0 0 0 0
3.x < −1 : 7
Output: [1] 1 2 3 4 5 6 7
4. y < −2 : −2
Output: [1] 2 1 0 -1 -2
5. z < −seq(1, 10, 2)
Output: [1] 1 3 5 7 9
Logical vector: A logical vector stores a group of TRUE or FALSE values.
They are basically yes or no to denote the answers to a group of logical ques-
tions.The simplest logical vectors are TRUE and FALSE themselves.
Examples:
1. x < −T RUE
Output: [1] TRUE
2. myvector < −c(T RUE, F ALSE, T RUE, T RUE, T RUE, F ALSE, F ALSE, T RUE)
Output: [1] TRUE FALSE TRUE TRUE TRUE FALSE FALSE TRUE
3. x1 < −(1 > 2)
Output:[1] FALSE
4.y < −c(1, 2) > 2
Output: [1] FALSE FALSE
5. y1 < −c(1, 2) > c(2, 1)
Output: [1] FALSE TRUE
6. z < −c(2, 3) > c(1, 2, −1, 3)