R Vectors

R Vectors

📌 R Vectors

Vectors are the most basic and important data structure in R.

They store multiple values of the same data type in a one-dimensional form.


1. Creating Vectors

Vectors are created using the c() (combine) function.



 


 2. Types of R Vectors

Type Example
Numeric c(1, 2, 3.5)
Integer c(1L, 2L, 3L)
Character c("A", "B")
Logical c(TRUE, FALSE)

Check type:



 


3. Vector Indexing

R uses 1-based indexing.


Multiple elements:



Negative indexing (exclude elements):


 

 4. Vector Operations (Vectorized)


✔ Operation applied to each element automatically


 5. Vector Length


 

 6. Sequence Vectors


 

 7. Repeat Vectors


 

8. Named Vectors


 

9. Logical Indexing


Output:

[1] 30 40

 10. Vector Type Conversion (Coercion)


 

✔ All converted to character


 11. Useful Vector Functions


 


 Common Mistakes ❌

❌ Mixing data types unintentionally
❌ Forgetting R is 1-indexed


📌 Summary

  • Vectors are 1D data structures

  • Store same type of data

  • Support vectorized operations

  • Foundation of all R data structures

You may also like...