3.6: Vectors

Statistical Thinking for the 21st Century · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC 4.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    3.6: Vectors

    You may have noticed that the my_random_numbers created above wasn’t like the variables that we had seen before — it contained a number of values in it. We refer to this kind of variable as a vector.

    If you want to create your own new vector, you can do that using the c() function:

    > my_vector <- c(4, 5, 6)
    > my_vector
    [1] 4 5 6
    

    You can access the individual elements within a vector by using square brackets along with a number that refers to the location within the vector. These index values start at 1, which is different from many other programming languages that start at zero. Let’s say we want to see the value in the second place of the vector:

    > my_vector[2]
    [1] 5
    

    You can also look at a range of positions, by putting the start and end locations with a colon in between:

    > my_vector[2:3]
    [1] 5 6
    

    You can also change the values of specific locations using the same indexing:

    > my_vector[3] <- 7
    > my_vector
    [1] 4 5 7
    

    This page titled 3.6: Vectors was last modified on Mon, 11 Jan 2021 05:16:31 GMT and is shared under a CC BY-NC 4.0 license and was authored, remixed, and/or curated by Russell A. Poldrack via source content that was edited to the style and standards of the LibreTexts platform.