Press "Enter" to skip to content

What is Vector in R?

Zigya Acadmey 0

Even when you write just one value in R, it becomes a vector of length 1 and belongs to one of the above vector types.

Vectors are generally created using the c() function.

c means “combine”. In R, numbers are just vectors of length one. All the things that can be done with a single number can also be done with a vector.

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(0, 1, 2, 3, 4)
> typeof(x)
[1] "double"
> length(x)
[1] 5
> x <- c(5, 2.4, TRUE, "hello world")
> x
[1] "5"     "2.4"   "TRUE"  "hello world"
> typeof(x)
[1] "character"

Using colon operator with numeric data

If we want to create a vector of consecutive numbers, we can this : operator.

# Create a sequence from 1 to 5.
> v <- 1:5
> print(v)
[1] 1 2 3 4 5

# Create a sequence from 5.5 to 10.5
> v <- 5.5:10.5
> print(v)
[1] 5.5 6.5 7.5 8.5 9.5 10.5

Using sequence (Seq.) operator

More complex sequences can be created using the seq() function, like defining number of points in an interval, or the step size.

# specify the step size
> seq(1, 2, by=0.2)          
[1] 1.0 1.2 1.4 1.6 1.8 2.0
# specify length of the vector creating 4 equal partition from 1 to 5
> seq(1, 5, length.out=4)    
[1] 1.000000 2.333333 3.666667 5.000000

Access Elements of a Vector

We can access elements of a Vector by using indexing. [ ] brackets are to do indexing. Indexing starts with position 1. Giving a negative value in the index drops that element from the result.TRUE, FALSE, or 0 and 1 can also be used for indexing.

> x
 <- seq(0, 10, by=2); x
[1]  0  2  4  6  8 10

# To access 3rd element
 
> x[3]           
[1] 4
 

# To access 2nd and 4th element
> x[c(2, 4)]     
[1] 2 6

# To access elements from 2nd to 4th 
> x[c(2:4)]
 [1] 2 4 6

# To access all except 2nd element
> x[-2]          
[1]  0  4  6  8 10

# real numbers will auto convert to integers
> x[c(2.4, 3.54)]    
[1] 2 4

Modify a vector 

We can modify a vector using the assignment operator.

> x
 <- c(1:5); x
[1] 1 2 3 4 5

# TO modify 2nd element
> x[2] <- 0; x        
[1] 1 0 3 4 5

# TO modify elements less than 3
> x[x<3] <- 0; x   
[1] 0 0 3 4 5

Delete a Vector

To delete any vector we simply assign a NULL to it

> x
 <- c(1:7)
[1] 1 2 3 4 5 6 7 

# To delete the vector assign NULL
> x <- NULL
> x
NULL

# Now the vector is NULL you cannot access any elements of it
> x[4]
NULL

Conclusion

We covered how to create a vector, what is c() function how to use it. How to use : operator in vector,
how to modify a vector, and delete a vector.

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

Leave a Reply

Your email address will not be published.