Vector in R programming.


Vectors in R
The term vector might sound intimidating. In R programming, a vector is the most-used data object. Like its counterpart in C++, it is a dynamic array of elements, each of the same data type. Another way to think of a vector is that it is a list of values, such as ratings, names, scores, prices, etc.
A vector’s type can be checked with the typeof() function.
Another important property of a vector is its length. This is the number of elements in the vector and can be checked with the function length().
How to Create Vector in R?
Vectors are generally created using the c() function.
Since, a vector must have elements of the same type, this function will try and coerce elements to the same type, if they are different.
Coercion is from lower to higher types from logical to integer to double to character.
> x <- c(1, 5, 4, 9, 0)
> typeof(x)
[1] "double"
> length(x)
[1] 5
> x <- c(1, 5.4, TRUE, "hello")
> x
[1] "1"     "5.4"   "TRUE"  "hello"
> typeof(x)
[1] "character"

How to access Elements of a Vector?

Elements of a vector can be accessed using vector indexing. The vector used for indexing can be logical, integer or character vector.

Using integer vector as index

Vector index in R starts from 1, unlike most programming languages where index start from 0.
We can use a vector of integers as index to access specific elements.
We can also use negative integers to return all elements except that those specified.
But we cannot mix positive and negative integers while indexing and real numbers, if used, are truncated to integers.
> x
[1]  0  2  4  6  8 10
> x[3]           # access 3rd element
[1] 4
> x[c(2, 4)]     # access 2nd and 4th element
[1] 2 6
> x[-1]          # access all but 1st element
[1]  2  4  6  8 10
> x[c(2, -4)]    # cannot mix positive and negative integers
Error in x[c(2, -4)] : only 0's may be mixed with negative subscripts
> x[c(2.4, 3.54)]    # real numbers are truncated to integers
[1] 2 4

Using logical vector as index

When we use a logical vector for indexing, the position where the logical vector is TRUE is returned.
This useful feature helps us in filtering of vector as shown below.
> x[c(TRUE, FALSE, FALSE, TRUE)]
[1] -3  3
> x[x < 0]  # filtering vectors based on conditions
[1] -3 -1
> x[x > 0]
[1] 3
In the above example, the expression x>0 will yield a logical vector (FALSE, FALSE, FALSE, TRUE) which is then used for indexing.

Using character vector as index

This type of indexing is useful when dealing with named vectors. We can name each elements of a vector.
> x <- c("first"=3, "second"=0, "third"=9)
> names(x)
[1] "first"  "second" "third" 
> x["second"]
second 
0 
> x[c("first", "third")]
first third 
3     9

How to modify a vector in R?

We can modify a vector using the assignment operator.
We can use the techniques discussed above to access specific elements and modify them.
If we want to truncate the elements, we can use reassignments.
> x
[1] -3 -2 -1  0  1  2
> x[2] <- 0; x        # modify 2nd element
[1] -3  0 -1  0  1  2
> x[x<0] <- 5; x   # modify elements less than 0
[1] 5 0 5 0 1 2
> x <- x[1:4]; x      # truncate x to first 4 elements
[1] 5 0 5 0

How to delete a Vector?

We can delete a vector by simply assigning a NULL to it.
> x
[1] -3 -2 -1  0  1  2
> x <- NULL
> x
NULL
> x[4]
NULL

Watch Video

Thank You,
Er. Shrikant Shejwal.

Comments

Popular posts from this blog

NSS CA1 Answers

Database Introduction and Relational Database ppt Notes

BC Mid Sem Ans