# What is vectors and indexing of its positions

I wanted to get more clear with more concepts of vector and indexing its position. Also, What are its data types and how to apply while creating a code.

Getting confused in these topics so far.

Hello @nishanta567,

This is an excellent question!

In R, a vector is a one-dimensional data type that holds several elements of the same type (logical, character, integer, double, …).

``````age <- c(10, 18, 9, 45, 35)
fruits <- c("apple", "cherry", "orange")
decisions <- c(TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE)
``````
• To index an element in a vector, we often use the position of the desired value in square brackets `[ ]`.
``````age #indexing the first element of the vector `age`. Output `10`
age #indexing the third element of the vector `age`. Output `9`
decisions #indexing the sixth element of the vector `decisions`. Output `FALSE`
``````
• It is possible to also index a vector with another vector of indices or a range of indices (which is a vector as well). The output will be a vector as well.
``````fruits[c(2, 3)]  #indexing the second and the third elements of the vector `fruits`. Output  "cherry", "orange"

indices <- c(1,3,5) #creating a vector of indices
age[indices] #using the vector of indices to index the first, third, and the fifth elements of the vector `age`. Output  10, 9, 35

decisions[3:5] #indexing the elements of the vector `decisions` between the position 3 and 5 (i.e., 3,4,5). Output  FALSE, TRUE, FALSE
``````
• You can filter values in a vector using a logical vector with the same length.
``````#Let's select even ages from the `age` vector.

age_is_even <- age %% 2 == 0 #this output a logical vector indicating where even values are with `TRUE` and `FALSE` otherwise. Output   TRUE, TRUE, FALSE, FALSE, FALSE

age[age_is_even] #selecting only even ages. Output  10, 18.
``````
• The basic data types in R are character, numeric (real or decimal), integer, logical, and complex.

• You can use `class()` function to find what kind of element a vector contains, and the `length()` function to find the length of a vector.

• If you try to mix types inside a vector. R will create a vector with a type that can most easily accommodate all the elements it contains. This is called ‘‘coercion’’.

``````c(10, 18, "20 and more") #Will be converted into vector of character. Output:  "10", "18",  "20 and more"
``````

I hope this help! I don’t have a useful reference in mind, but I’m pretty sure there are a lot of online resources online about it. Maybe @casey knows one? Maybe in our content?

Best,
John.

Hi John!!!

Thanks for the stuff. It really helped me a lot to understand.

Furthermore, If any assistance is required, will get back to you.

Thanks again!!!

1 Like

Hi @nishanta567. If you are still interested in learning more about vectors in R, one resource I’d recommend is the chapter on vectors in Hadley Wickham’s R for Data Science book. This chapter is available online for free here.

1 Like

Hey @casey,

Thanks for the stuff. If any further help needed, will let u know.

Thanks again!!!

1 Like