R Beginner Tutorial – Arrays
R Academy Menu ggplot2 basics: learn ggplot2 in 15 minutes!R Beginner TutorialR Beginner Tutorial – Basic SyntaxR Beginner Tutorial – R ObjectsR Beginner Tutorial –
Home » R Beginner Tutorial – Vectors
Vectors are the most basic R data structure. We already know vectors can be of different classes: numeric, integer, complex, character and logical. Elements of a vector must be of the same type.
Let’s review how vectors compare to other data structures.
As seen on the image above, vectors are building blocks of other data structures. Matrices and arrays must contain vectors of the same type, while data frames and lists can contain vectors of different types.
We create a vector with the c() function.
> v1 = c(1, 2, 3)
> v1
[1] 1 2 3
> v2 = c("I", "love", "R!")
> v2
[1] "I" "love" "R!"
We add elements to a vector by creating a new vector and inputing the first vector as the first element.
> v1 = c(1, 2, 3)
> v1
[1] 1 2 3
> v2 = c(v1, 4)
> v2
[1] 1 2 3 4
Vectors are always flat, even if you nest them. Notice a nested vector returns the same result as a flat one.
> c(1, c(2, c(3, 4, 5)))
[1] 1 2 3 4 5
> c(1, 2, 3, 4, 5)
[1] 1 2 3 4 5
We use in element index and [ ] brackets to access elements. Indexing in R starts with position 1.
> c(1, c(2, c(3, 4, 5))) [1] 1 2 3 4 5 > v1 = c("Jan", "Feb", "Mar", "Apr", "May", "Jun") > v1 [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" > v1[1] [1] "Jan" > v1[3] [1] "Mar" > v1[c(1, 3)] [1] "Jan" "Mar"
Negative index drops an element.
> v1 = c("Jan", "Feb", "Mar", "Apr", "May", "Jun") > v1 [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" > v1[-1] [1] "Feb" "Mar" "Apr" "May" "Jun" > v1[-3] [1] "Jan" "Feb" "Apr" "May" "Jun" > v1[c(-1, -3)] [1] "Feb" "Apr" "May" "Jun"
We create sequences using colons, for example 3:15.
> # Creating a sequence from 3 to 15.
> s = 3:15
> s
[1] 3 4 5 6 7 8 9 10 11 12 13 14 15
> # Creating a sequence from 3.3 to 15.3.
> s = 3.3:15.3
> s
[1] 3.3 4.3 5.3 6.3 7.3 8.3 9.3 10.3 11.3 12.3 13.3 14.3 15.3
If end element doesn’t decimally match up to the first one, it’s dropped from the sequence.
> # Creating a sequence from 3.3 to 15.1.
> s = 3.3:15.1
> s
[1] 3.3 4.3 5.3 6.3 7.3 8.3 9.3 10.3 11.3 12.3 13.3 14.3
Two vectors of same length can be added, subtracted, multiplied or divided giving the result as a vector output.
> # Create two vectors.
> v1 = c(1, 2, 3, 4, 5)
> v2 = c(6, 7, 8, 9, 0)
> v1
[1] 1 2 3 4 5
> v2
[1] 6 7 8 9 0
> # Add two vectors.
> sum = v1 + v2
> sum
[1] 7 9 11 13 5
> # Subtract two vectors.
> diff = v1 - v2
> diff
[1] -5 -5 -5 -5 5
> # Multiply two vectors.
> mltp = v1*v2
> mltp
[1] 6 14 24 36 0
> # Divise two vectors.
> div = v2/v1
> div
[1] 6.000000 3.500000 2.666667 2.250000 0.000000
Meanwhile, in a hurry? If you’re already running late, we recommend you head over to our two R crash courses:
For even more amazing tips check out our awesome ExcelOlympics YouTube channel!
1 Ceballos, Maite and Nicolás Cardiel. 2013. “Data structure.” First Steps in R. Accessed 2021-10-12.