411 REFERENCE · Wikibooks
Statistical Analysis: an Introduction using R/R/Constructing vectors
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Constructing vectors
One of the most fundamental objects in R is the vector. This is a collection of multiple measurements of the same type, so can be used to store data variables. R provides several ways to create a vector
For a simple sequence of numbers from a to b, the easiest way is to use the notation a:b.
Many functions return vectors. Two of the most useful are "seq()" and "c()". The function "seq()" produces sequences of numbers, similar to a:b, but with more flexibility. The function "c" concatenates objects together into a vector, and so can be used to construct an arbitrary vector. 0:10 #Creates a vector of 10 numbers consisting of the integers 0 to 10
seq(0,10) #Creates a vector of 10 numbers consisting of the integers 0 to 10
my.data <- 0:10
c(2,3,6,6,6) #Creates a vector of 5 numbers
c(2,3,6,6,6) + 1 #N.B. you can do all the normal calculations on vectors > 99:150 #Creates a vector of 52 numbers consisting of the integers 99 to 150
foo
[1] 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
[26] 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
[51] 149 150
> c(2,3,6,6,6) #Creates a vector of 5 numbers
[1] 2 3 6 6 6
> c(2,3,6,6,6) + 1 #N.B. you can do all the normal calculations on vectors
[1] 3 4 7 7 7