1.8.
3 Overwriting (or) Replacing Elements
We can overwrite a specific subset of a vector (or) overwrite multiple ele-
ments at different positions. Like subsetting, logical selectors are also accepted
for overwriting.
1. b < −c(3, 2, 4, 4, 1, 2, 4, 1, 0, 0, 5)
Output: [1] 3 2 4 4 1 2 4 1 0 0 5
The code overwrites the third element of b, which was originally 4, with a
new value, 6.
b[3] < −6
Output: [1] 3 2 6 4 1 2 4 1 0 0 5
2. The following code overwrite the second, fourth, and sixth elements with
-2, -0.5,and -1, respectively; all else remains the same.
b[c(2, 4, 6)] < −c(−2, −0.5, −1)
Output: [1] 3.0 -2.0 6.0 -0.5 1.0 -1.0 4.0 1.0 0.0 0.0 5.0
3. The code overwrites elements 7 to 10 (inclusive), replacing them all with
100.
b[7 : 10] < −100
Output: [1] 3.0 -2.0 6.0 -0.5 1.0 -1.0 100.0 100.0 100.0 100.0 5.0
Named Vectors
A named vector is not a specific type of vector parallel to a numeric or
logical vector. It is a vector with names corresponding to the elements. We can
give names to a vector when we create it.
Example: x < −c(a = 1, b = 2, c = 3)
Output: a b c
123
We can access the elements with a single-valued character vector.
Examples: x[”a”]
Output: a
x[c(”a”, ”c”)]
Output: a c
13
We can get the names of aWe can get the names of a vector with names() :
Example: names(x).
Output: [1] ”a” ”b” ”c”
The names of a vector are not fixed. We can change the names of a vector
by assigning another character vector to its names.
names(x) < −c(”x”, ”y”, ”z”)
1.8. VECTOR 23
Output:
xyz
123
x[”z”]
Output:
If the names are no longer needed, we can simply remove the vector’s
names using NULL, a special object that represents undefined value:
names(x) < −NULL
Output: [1] 1 2 3
1.8.5 Extracting an element
While [] creates a subset of a vector, [[ ]] extracts an element from a vector.
For simple vectors, using [] and [[]] to get one element will produce the same
result.But it does not work with vectors of more than one element:
Example: x[[”a”]]
Output: [1] 1
Class of vectors We can obtain information about the type of object by using
the class() command. The class() function tells us the class of any R object.
Syntax: class(object)
Examples:
1. class(x)
Output: [1] ”numeric”
2.class(myvector)
Output:[1] ”logical”
3. class(t)
Output: [1] ”character”
1.8.6 Object-Checking Functions
Identifying the class of an object is essential for functions that operate on
stored objects, especially those that behave differently depending on the class
of the object. To check whether the object is a specific class or data type, you
can use the is-dot functions on the object and it will return a TRUE or FALSE
24 CHAPTER 1. INTRODUCTION TO R
logical value.
Examples:
1. is.numeric(x)
Output: [1] TRUE
2. is.numeric(myvector)
Output: [1] FALSE
3. is.character(t)
Output:[1] TRUE
1.8.7 Coercion Functions
The process of altering the data type of an object to another type is re-
ferred to as coercion or data type conversion. This is a common operation in
many programming languages that is used to alter data and perform various
computations. When coercion is required, the language normally performs it
automatically, whereas conversion is performed directly by the programmer.
Syntax: as.data type(object)
Examples:
strings < −c(”1”, ”2”, ”3”)
class(strings)
Output[1] ”character”
strings + 10
Error in strings + 10 : non-numeric argument to binary operator
numbers < −as.numeric(strings)
numbers
Output: [1] 1 2 3
numbers + 10
Output: [1] 11 12 13