<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/how-to-create-your-first-vector-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/05/01/how-to-create-your-first-vector-in-r/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-create-your-first-vector-in-r"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/www.r-exercises.com/wp-content/uploads/2015/10/cooltext150717148122234.png"><img sizes="(max-width: 150px) 100vw, 150px" alt="" class="wp-image-117" src="https://i2.wp.com/www.r-exercises.com/wp-content/uploads/2015/10/cooltext150717148122234-150x150.png?resize=150%2C150" data-recalc-dims="1"></a>Are you an expert R programmer? If so, this is *not* for you. This is a short tutorial for R novices, explaining vectors, a basic R data structure. Here’s an example:</p>
<p><code>10 150 30 45 20.3</code></p>
<p>And here’s another one:</p>
<p><code>-5 -4 -3 -2 -1 0 1 2 3</code></p>
<p>still another one:</p>
<p><code>&quot;Darth Vader&quot; &quot;Luke Skywalker&quot; &quot;Han Solo&quot;</code></p>
<p>and our final example:</p>
<p><code>389.3491</code></p>
<p>These examples show that a vector is, simply speaking, just a collection of one (fourth example) or more (first and second example) numbers or character strings (third example). In R, a vector is considered a data structure (it’s a very simple data structure, and we’ll cover more complex structures in another tutorial).</p>
<h1>So, how can we set up and use a vector in R?</h1>
<p>We can construct a vector from a series of individual elements, using the <code>c()</code> function, as follows:</p>
<div>
<div>
<div>
<pre><span>c</span><span>(</span><span>10</span><span>,</span> <span>150</span><span>,</span> <span>30</span><span>,</span> <span>45</span><span>,</span> <span>20.3</span><span>)</span>
</pre>
</div>
<div>
<pre>## [1]  10.0 150.0  30.0  45.0  20.3
</pre>
</div>
</div>
</div>
<p>(In examples like these, lines starting with <code>##</code> show the output from R on the screen).</p>
<h1>Assigning a vector</h1>
<p>As you’ll see, once you have entered the vector, R will respond by displaying its elements. In many cases it will be convenient to refer to this vector using a name, instead of having to enter it over and over again. We can accomplish this using the <code>assign()</code> function, which is equivalent to the <code>&lt;-</code> and <code>=</code> operators:</p>
<div>
<div>
<div>
<pre><span>assign</span><span>(</span><span>'a'</span><span>,</span> <span>c</span><span>(</span><span>10</span><span>,</span> <span>150</span><span>,</span> <span>30</span><span>,</span> <span>45</span><span>,</span> <span>20.3</span><span>))</span>
<span>a</span> <span>&lt;-</span> <span>c</span><span>(</span><span>10</span><span>,</span> <span>150</span><span>,</span> <span>30</span><span>,</span> <span>45</span><span>,</span> <span>20.3</span><span>)</span>
<span>a</span> <span>=</span> <span>c</span><span>(</span><span>10</span><span>,</span> <span>150</span><span>,</span> <span>30</span><span>,</span> <span>45</span><span>,</span> <span>20.3</span><span>)</span>
</pre>
</div>
</div>
</div>
<p>The second statement (using the &lt;<code>-</code> operator) is the most common way of assigning in R, and we’ll therefore use this form rather than the <code>=</code> operator or the <code>assign()</code> function.</p>
<p>Once we have assigned a vector to a name, we can refer to the vector using this name. For example, if we type a, R will now show the elements of vector a.</p>
<div>
<div>
<div>
<pre><span>a</span>
</pre>
</div>
<div>
<pre>## [1]  10.0 150.0  30.0  45.0  20.3
</pre>
</div>
</div>
</div>
<p>Instead of a, we could have chosen any other name, e.g.:</p>
<div>
<div>
<div>
<pre><span>aVeryLongNameWhichIsCaseSensitive_AndDoesNotContainSpaces</span> <span>&lt;-</span> <span>c</span><span>(</span><span>10</span><span>,</span> <span>150</span><span>,</span> <span>30</span><span>,</span> <span>45</span><span>,</span> <span>20.3</span><span>)</span>
</pre>
</div>
</div>
</div>
<p>Strictly speaking, we call this “name” an object.</p>
<p>To familiarize yourself with the vector data structure, now try to construct a couple of vectors in R and assign them to a named object, as in the example above.</p>
<p><strong>To summarize: A vector is a data structure, which can be constructed using the <code>c()</code> function, and assigned to a named object using the <code>&lt;-</code> operator.</strong></p>
<p>Now, let’s move on to the first set of real <a target="_blank" rel="nofollow" href="http://www.r-exercises.com/2015/10/09/vector-exercises/">exercises on vectors</a>!</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a title="Data Structures Exercises" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/03/15/data-structures-exercises/">Data Structures Exercises </a></li>
<li><a title="Unit testing in R using testthat library Exercises" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/03/12/unit-testing-in-r-using-testthat-library/">Unit testing in R using testthat library Exercises </a></li>
<li><a title="Descriptive Analytics-Part 1: Data Formatting Exercises" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2016/10/26/descriptive-analytics-part-1-data-formatting/">Descriptive Analytics-Part 1: Data Formatting Exercises </a></li>
<li><a target="_blank" rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/">Explore all our (>1000) R exercises</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.r-exercises.com/r-courses/">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.r-exercises.com/2017/05/01/how-to-create-your-first-vector-in-r/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-create-your-first-vector-in-r"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/7Zw0BnE2oPY"></a></div>