myvec <- c(pi, 5, 3.6, 2, 9, 6000)
myvec[1] 3.141593 5.000000 3.600000 2.000000 9.000000 6000.000000
A vector in R is just an ordered set of values that have the same type. The easiest way to create one is to manually list out the values, separated by commas, inside c():
myvec <- c(pi, 5, 3.6, 2, 9, 6000)
myvec[1] 3.141593 5.000000 3.600000 2.000000 9.000000 6000.000000
If you want to list out all of the integers between some min and max, you can use this shortcut:
5:15 [1] 5 6 7 8 9 10 11 12 13 14 15
More generally, if you want a vector of evenly spaced numbers between a min and a max, do this:
a <- seq(0, 1, length.out = 11)
a [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
If you want to create a “blank” vector that just has zeros in it, here you go:
z <- numeric(10)
z [1] 0 0 0 0 0 0 0 0 0 0
In all of those cases, our vector contained numbers, but there is nothing special about numbers. Here is a vector where each value is a string (a piece of text):
poetry <- c("Mary", "had", "a", "little", "chainsaw")
poetry[1] "Mary" "had" "a" "little" "chainsaw"
If you have two vectors and you want to combine them into one, you can just use the c() command again. The “c” stands for “concatenate.” You’re concatenating, or joining, the vectors end-to-end: