<?xml version="1.0" encoding="UTF-8" ?>
<!-- RSS generated by PHPBoost on Mon, 04 May 2026 03:13:06 +0200 -->

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Easy Guides]]></title>
		<atom:link href="https://www.sthda.com/english/syndication/rss/wiki/37" rel="self" type="application/rss+xml"/>
		<link>https://www.sthda.com</link>
		<description><![CDATA[Last articles of the category: R Basics: Quick and Easy]]></description>
		<copyright>(C) 2005-2026 PHPBoost</copyright>
		<language>en</language>
		<generator>PHPBoost</generator>
		
		
		<item>
			<title><![CDATA[R Built-in Data Sets]]></title>
			<link>https://www.sthda.com/english/wiki/r-built-in-data-sets</link>
			<guid>https://www.sthda.com/english/wiki/r-built-in-data-sets</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#preleminary-tasks">Preleminary tasks</a></li>
<li><a href="#list-of-pre-loaded-data">List of pre-loaded data</a></li>
<li><a href="#loading-a-built-in-r-data">Loading a built-in R data</a></li>
<li><a href="#most-used-r-built-in-data-sets">Most used R built-in data sets</a><ul>
<li><a href="#mtcars-motor-trend-car-road-tests">mtcars: Motor Trend Car Road Tests</a></li>
<li><a href="#iris">iris</a></li>
<li><a href="#toothgrowth">ToothGrowth</a></li>
<li><a href="#plantgrowth">PlantGrowth</a></li>
<li><a href="#usarrests">USArrests</a></li>
</ul></li>
<li><a href="#summary">Summary</a></li>
<li><a href="#related-articles">Related articles</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<p><br/> <strong>R</strong> comes with several <strong>built-in data sets</strong>, which are generally used as demo data for playing with R functions.</p>
<br/>
<div class="block">
In this article, we’ll first describe how load and use R built-in data sets. Next, we’ll describe some of the most used R demo data sets: <strong>mtcars</strong>, <strong>iris</strong>, <strong>ToothGrowth</strong>, <strong>PlantGrowth</strong> and <strong>USArrests</strong>.
</div>
<p><br/></p>
<div id="preleminary-tasks" class="section level1">
<h1>Preleminary tasks</h1>
<p><strong>Launch RStudio</strong> as described here: <a href="https://www.sthda.com/english/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming">Running RStudio and setting up your working directory</a></p>
</div>
<div id="list-of-pre-loaded-data" class="section level1">
<h1>List of pre-loaded data</h1>
<p>To see the list of pre-loaded data, type the function <strong>data</strong>():</p>
<pre class="r"><code>data()</code></pre>
<p>The output is as follow:</p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-data-sets.png" alt="R data sets" /></p>
</div>
<div id="loading-a-built-in-r-data" class="section level1">
<h1>Loading a built-in R data</h1>
<p>Load and print mtcars data as follow:</p>
<pre class="r"><code># Loading
data(mtcars)

# Print the first 6 rows
head(mtcars, 6)</code></pre>
<pre><code>                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1</code></pre>
<p>If you want learn more about mtcars data sets, type this:</p>
<pre class="r"><code>?mtcars</code></pre>
</div>
<div id="most-used-r-built-in-data-sets" class="section level1">
<h1>Most used R built-in data sets</h1>
<div id="mtcars-motor-trend-car-road-tests" class="section level2">
<h2>mtcars: Motor Trend Car Road Tests</h2>
<p>The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models)</p>
<ul>
<li>View the content of <em>mtcars</em> data set:</li>
</ul>
<pre class="r"><code># 1. Loading 
data("mtcars")
# 2. Print
head(mtcars)</code></pre>
<ul>
<li>It contains 32 observations and 11 variables:</li>
</ul>
<pre class="r"><code># Number of rows (observations)
nrow(mtcars)</code></pre>
<pre><code>[1] 32</code></pre>
<pre class="r"><code># Number of columns (variables)
ncol(mtcars)</code></pre>
<pre><code>[1] 11</code></pre>
<ul>
<li>Description of variables:</li>
</ul>
<ol style="list-style-type: decimal">
<li>mpg: Miles/(US) gallon</li>
<li>cyl: Number of cylinders</li>
<li>disp: Displacement (cu.in.)</li>
<li>hp: Gross horsepower</li>
<li>drat: Rear axle ratio</li>
<li>wt: Weight (1000 lbs)</li>
<li>qsec: 1/4 mile time</li>
<li>vs: V/S</li>
<li>am: Transmission (0 = automatic, 1 = manual)</li>
<li>gear: Number of forward gears</li>
<li>carb: Number of carburetors</li>
</ol>
<p>If you want to learn more about <em>mtcars</em>, type this:</p>
<pre class="r"><code>?mtcars</code></pre>
</div>
<div id="iris" class="section level2">
<h2>iris</h2>
<p><strong>iris</strong> data set gives the measurements in centimeters of the variables sepal length, sepal width, petal length and petal width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica.</p>
<pre class="r"><code>data("iris")

head(iris)</code></pre>
<pre><code>  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa</code></pre>
</div>
<div id="toothgrowth" class="section level2">
<h2>ToothGrowth</h2>
<p>ToothGrowth data set contains the result from an experiment studying the effect of vitamin C on tooth growth in 60 Guinea pigs. Each animal received one of three dose levels of vitamin C (0.5, 1, and 2 mg/day) by one of two delivery methods, (orange juice or ascorbic acid (a form of vitamin C and coded as VC).</p>
<pre class="r"><code>data("ToothGrowth")
  
head(ToothGrowth)</code></pre>
<pre><code>   len supp dose
1  4.2   VC  0.5
2 11.5   VC  0.5
3  7.3   VC  0.5
4  5.8   VC  0.5
5  6.4   VC  0.5
6 10.0   VC  0.5</code></pre>
<ol style="list-style-type: decimal">
<li>len: Tooth length</li>
<li>supp: Supplement type (VC or OJ).</li>
<li>dose: numeric Dose in milligrams/day</li>
</ol>
</div>
<div id="plantgrowth" class="section level2">
<h2>PlantGrowth</h2>
<p>Results obtained from an experiment to compare yields (as measured by dried weight of plants) obtained under a control and two different treatment condition.</p>
<pre class="r"><code>data("PlantGrowth")
  
head(PlantGrowth)</code></pre>
<pre><code>  weight group
1   4.17  ctrl
2   5.58  ctrl
3   5.18  ctrl
4   6.11  ctrl
5   4.50  ctrl
6   4.61  ctrl</code></pre>
</div>
<div id="usarrests" class="section level2">
<h2>USArrests</h2>
<p>This data set contains statistics about violent crime rates by us state.</p>
<pre class="r"><code>data("USArrests")
     
head(USArrests)</code></pre>
<pre><code>           Murder Assault UrbanPop Rape
Alabama      13.2     236       58 21.2
Alaska       10.0     263       48 44.5
Arizona       8.1     294       80 31.0
Arkansas      8.8     190       50 19.5
California    9.0     276       91 40.6
Colorado      7.9     204       78 38.7</code></pre>
<ol style="list-style-type: decimal">
<li>Murder: Murder arrests (per 100,000)</li>
<li>Assault: Assault arrests (per 100,000)</li>
<li>UrbanPop: Percent urban population</li>
<li>Rape: Rape arrests (per 100,000)</li>
</ol>
</div>
</div>
<div id="summary" class="section level1">
<h1>Summary</h1>
<br/>
<div class="block">
<ul>
<li><p>Load a built-in R data set: <strong>data</strong>(“dataset_name”)</p></li>
<li>Inspect the data set: <strong>head</strong>(dataset_name)</li>
</ul>
</div>
<p><br/></p>
</div>
<div id="related-articles" class="section level1">
<h1>Related articles</h1>
<ul>
<li>Previous chapters
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/what-is-r-and-why-learning-r-programming">What’is R and why learning R?</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/installing-r-and-rstudio-easy-r-programming">Installing R and RStudio</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming">Running RStudio and setting up your working directory</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/easy-r-programming-basics">R programming basics</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/getting-help-with-functions-in-r-programming">Getting help with functions in R programming</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/installing-and-using-r-packages">Installing and using R packages</a></li>
</ul></li>
<li>Next chapters
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/importing-data-into-r">Importing data into R</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/exporting-data-from-r">Exporting data from R</a></li>
</ul></li>
</ul>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using R (ver. 3.2.3). </span></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->


<!-- END HTML -->]]></description>
			<pubDate>Sun, 10 Apr 2016 08:39:21 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[R Basics: Quick and Easy]]></title>
			<link>https://www.sthda.com/english/wiki/r-basics-quick-and-easy</link>
			<guid>https://www.sthda.com/english/wiki/r-basics-quick-and-easy</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<p><br/></p>
<style>#rdoc .course_material a{font-size:1.5em;} #rdoc .readmore a{font-size:1em;}</style>
<p><strong>R</strong> is a free and powerful statistical software for <strong>analyzing</strong> and <strong>visualizing</strong> data. In this chapter, we provide a quick and easy introduction to <strong>R programming</strong>.</p>
<div class="block">
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/what-is-r-and-why-learning-r-programming">What’is R and why learning R?</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/installing-r-and-rstudio-easy-r-programming">Installing R and RStudio</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming">Running RStudio and setting up your working directory</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/easy-r-programming-basics">R programming basics</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/getting-help-with-functions-in-r-programming">Getting help with functions in R programming</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/installing-and-using-r-packages">Installing and using R packages</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/r-built-in-data-sets">R Built-in data sets</a></li>
</ul>
</div>
<br/>
<hr/>
<p><br/></p>
<br/>
<div class="course_material">
<ol style="list-style-type: decimal">
<li><a href="https://www.sthda.com/english/english/wiki/what-is-r-and-why-learning-r-programming"><i class="fa fa-folder-open"></i> <strong>What’is R and why learning R?</strong></a></li>
</ol>
<p><span class="success readmore">Read more: <a href="https://www.sthda.com/english/english/wiki/what-is-r-and-why-learning-r-programming"><i class="fa fa-play"></i> What’is R and why learning R?</a></span> 
</p>
<ol start="2" style="list-style-type: decimal">
<li><a href="https://www.sthda.com/english/english/wiki/installing-r-and-rstudio-easy-r-programming"><i class="fa fa-folder-open"></i> <strong>Installing R and RStudio</strong></a></li>
</ol>
<ul>
<li>Install R and RStudio on windows</li>
<li>Install R and RStudio for MAC OSX</li>
<li>Install R and RStudio on Linux</li>
</ul>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-logo.png" alt="R logo" /> <img src="https://www.sthda.com/english/sthda/RDoc/images/rstudio-logo.png" alt="Rstudio logo" /></p>
<p><span class="success readmore">Read more: <a href="https://www.sthda.com/english/english/wiki/installing-r-and-rstudio-easy-r-programming"><i class="fa fa-play"></i> Installing R and RStudio</a></span></p>
<p>
</p>
<ol start="3" style="list-style-type: decimal">
<li><a href="https://www.sthda.com/english/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming"><i class="fa fa-folder-open"></i> <strong>Running RStudio and setting up your working directory</strong></a></li>
</ol>
<ul>
<li><strong>Use R outside RStudio</strong></li>
<li><strong>Use R inside RStudio</strong>
<ul>
<li>Launch RStudio under Windows, MAC OSX and Linux</li>
<li>Set up your working directory
<ul>
<li>Change your working directory</li>
<li>Set up a default working directory</li>
</ul></li>
</ul></li>
<li><strong>Close your R/RStudio session</strong></li>
<li>Functions: <strong>setwd</strong>(), <strong>getwd</strong>()</li>
</ul>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/rstudio.png" alt="RStudio" /></p>
<p><span class="success readmore">Read more: <a href="https://www.sthda.com/english/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming"><i class="fa fa-play"></i>Running RStudio and setting up your working directory</a></span></p>
<p>
</p>
<ol start="4" style="list-style-type: decimal">
<li><a href="https://www.sthda.com/english/english/wiki/easy-r-programming-basics"><i class="fa fa-folder-open"></i> <strong>R programming basics</strong></a></li>
</ol>
<ul>
<li><strong>Basic arithmetic operations</strong>: + (addition), - (subtraction), * (multiplication), / (division), ^ (exponentiation)</li>
</ul>
<pre class="r"><code>7 + 4 # => 11
7 - 4 # => 3
7 / 2 # => 3.5
7 * 2 # => 14</code></pre>
<p><br/></p>
<ul>
<li><strong>Basic arithmetic functions</strong>:
<ul>
<li>Logarithms and exponentials: <strong>log2</strong>(x), <strong>log10</strong>(x), <strong>exp</strong>(x)</li>
<li>Trigonometric functions: <strong>cos</strong>(x), <strong>sin</strong>(x), <strong>tan</strong>(x), <strong>acos</strong>(x), <strong>asin</strong>(x), <strong>atan</strong>(x)</li>
<li>Other mathematical functions: <strong>abs</strong>(x): absolute value; <strong>sqrt</strong>(x): square root.</li>
</ul></li>
</ul>
<pre class="r"><code>log2(4) # => 2
abs(-4) # => 4
sqrt(4) # => 2</code></pre>
<p><br/></p>
<ul>
<li><strong>Assigning values to variables</strong>:</li>
</ul>
<pre class="r"><code>lemon_price <- 2</code></pre>
<p><br/></p>
<ul>
<li><strong>Basic data types</strong>: <strong>numeric</strong>, <strong>character</strong> and <strong>logical</strong></li>
</ul>
<pre class="r"><code>my_age <- 28 # Numeric variable
my_name <- "Nicolas" # Character variable
#  Are you a data scientist?: (yes/no) <=> (TRUE/FALSE)
is_datascientist <- TRUE # logical variable</code></pre>
<p><br/></p>
<ul>
<li><strong>Vectors</strong>: a combination of multiple values (numeric, character or logical)
<ul>
<li>Create a vector: <strong>c</strong>() for concatenate</li>
<li>Case of missing values: <strong>NA</strong> (not available) and <strong>NaN</strong> (not a number)</li>
<li>Get a subset of a vector: my_vector[i] to get the ith element</li>
<li>Calculations with vectors: <strong>max</strong>(x), <strong>min</strong>(x), <strong>range</strong>(x), <strong>length</strong>(x), <strong>sum</strong>(x), <strong>mean</strong>(x), <strong>prod</strong>(x): product of the elements in x, <strong>sd</strong>(x): standard deviation, <strong>var</strong>(x): variance, <strong>sort</strong>(x)
</li>
</ul></li>
</ul>
<pre class="r"><code># Create a numeric vector
friend_ages <- c(27, 25, 29, 26)
mean(friend_ages) # => 26.75
max(friend_ages) # => 29</code></pre>
<p><br/></p>
<ul>
<li><strong>Matrices</strong>: like an Excel sheet containing multiple rows and columns. Combination of multiple vectors with the same types (numeric, character or logical).
<ul>
<li>Create and naming matrix: <strong>matrix</strong>(), <strong>cbind</strong>(), <strong>rbind</strong>(), <strong>rownames</strong>(), <strong>colnames</strong>()</li>
<li>Check and convert: <strong>is.matrix</strong>(), <strong>as.matrix</strong>()</li>
<li>Transpose a matrix: <strong>t</strong>()</li>
<li>Dimensions of a matrix: <strong>ncol</strong>(), <strong>nrow</strong>(), <strong>dim</strong>()</li>
<li>Get a subset of a matrix: my_data[row, col]</li>
<li>Calculations with numeric matrices: <strong>rowSums</strong>(), <strong>colSums</strong>(), <strong>rowMeans</strong>(), <strong>colMeans</strong>(), <strong>apply</strong>()</li>
</ul></li>
</ul>
<pre><code>     col1 col2 col3
row1    5    2    7
row2    6    4    3
row3    7    5    4
row4    8    9    8
row5    9    8    7</code></pre>
<p><br/></p>
<ul>
<li><strong>Factors</strong>: grouping variables in your data
<ul>
<li>Create a factor: <strong>factor</strong>(), <strong>levels</strong>()</li>
<li>Check and convert: <strong>is.factor</strong>(x), <strong>as.factor</strong>(x)</li>
<li>Calculations with factors:
<ul>
<li>Number of elements in each category: <strong>summary</strong>(), <strong>table</strong>()</li>
<li>Compute some statistics by groups (for example, mean by groups): <strong>tapply</strong>()</li>
</ul></li>
</ul></li>
</ul>
<pre class="r"><code># Create a factor
friend_groups <- factor(c("grp1", "grp2", "grp1", "grp2"))
levels(friend_groups) # => "grp1", "grp2"</code></pre>
<pre><code>[1] "grp1" "grp2"</code></pre>
<pre class="r"><code># Compute the mean age by groups
friend_ages <- c(27, 25, 29, 26)
tapply(friend_ages, friend_groups, mean)</code></pre>
<pre><code>grp1 grp2 
28.0 25.5 </code></pre>
<p><br/></p>
<ul>
<li><strong>Data frames</strong>: like a matrix but can have columns with different types
<ul>
<li>Create a data frame: <strong>data.frame</strong>()</li>
<li>Check and convert: <strong>is.data.frame</strong>(), <strong>as.data.frame</strong>()</li>
<li>Transpose a data frame: <strong>t</strong>()</li>
<li>Subset a data frame: my_data[row, col], <strong>subset</strong>(), <strong>attach</strong>() and <strong>detach</strong>()</li>
<li>Extend a data frame: <strong>$</strong>, <strong>cbind</strong>(), <strong>rbind</strong>()</li>
<li>Calculations with numeric data frames: <strong>rowSums</strong>(), <strong>colSums</strong>(), <strong>rowMeans</strong>(), <strong>colMeans</strong>(), <strong>apply</strong>()</li>
</ul></li>
</ul>
<pre><code>     name age height married
1 Nicolas  27    180    TRUE
2 Thierry  25    170   FALSE
3 Bernard  29    185    TRUE
4  Jerome  26    169    TRUE</code></pre>
<p><br/></p>
<ul>
<li><strong>Lists</strong>: collection of objects, which can be vectors, matrices, data frames,
<ul>
<li>Create a list: <strong>list</strong>()</li>
<li>Subset a list</li>
<li>Extend a list</li>
</ul></li>
</ul>
<pre class="r"><code>my_family <- list(
  mother = "Veronique", 
  father = "Michel",
  sisters = c("Alicia", "Monica"),
  sister_age = c(12, 22)
  )
# Print
my_family</code></pre>
<pre><code>$mother
[1] "Veronique"

$father
[1] "Michel"

$sisters
[1] "Alicia" "Monica"

$sister_age
[1] 12 22</code></pre>
<p><span class="success readmore">Read more: <a href="https://www.sthda.com/english/english/wiki/easy-r-programming-basics"><i class="fa fa-play"></i>R programming basics</a></span> 
</p>
<ol start="5" style="list-style-type: decimal">
<li><a href="https://www.sthda.com/english/english/wiki/getting-help-with-functions-in-r-programming"><i class="fa fa-folder-open"></i> <strong>Getting help with functions in R programming</strong></a></li>
</ol>
<ul>
<li>Getting help on a specific function: <strong>help</strong>(mean), <strong>example</strong>(mean)</li>
<li>General help about R: <strong>help_start()</strong></li>
<li>Others functions: <strong>apropos</strong>() and <strong>help.search</strong>()</li>
</ul>
<p><span class="success readmore">Read more: <a href="https://www.sthda.com/english/english/wiki/getting-help-with-functions-in-r-programming"><i class="fa fa-play"></i> Getting help with functions in R programming</a></span></p>
<p><br/></p>
<ol start="6" style="list-style-type: decimal">
<li><a href="https://www.sthda.com/english/english/wiki/installing-and-using-r-packages"><i class="fa fa-folder-open"></i> <strong>Installing and using R packages</strong></a></li>
</ol>
<ul>
<li><p><strong>What is R packages?</strong></p></li>
<li><strong>Installing</strong> R packages
<ul>
<li>Install a package from CRAN: <strong>install.packages</strong>()</li>
<li>Install a package from Bioconductor: <strong>biocLite</strong>()</li>
<li>Install a package from GitHub: <strong>devtools::install_github</strong>()</li>
<li>View the list of installed packages: <strong>installed.packages</strong>()</li>
<li>Folder containing installed packages: <strong>.libPaths</strong>()</li>
</ul></li>
<li><p><strong>Load</strong> and use an R package: <strong>library</strong>()</p></li>
<li><p><strong>View</strong> loaded R packages: <strong>search</strong>()</p></li>
<li><p><strong>Unload</strong> an R package: <strong>detach</strong>(pkg_name, unload = TRUE)</p></li>
<li><p><strong>Remove</strong> installed packages: <strong>remove.packages</strong>()</p></li>
<li><p><strong>Update</strong> installed packages: <strong>update.packages</strong>()</p></li>
</ul>
<p><span class="success readmore">Read more: <a href="https://www.sthda.com/english/english/wiki/installing-and-using-r-packages"><i class="fa fa-play"></i>Installing and using R packages</a></span></p>
<p><br/></p>
<ol start="7" style="list-style-type: decimal">
<li><a href="https://www.sthda.com/english/english/wiki/r-built-in-data-sets"><i class="fa fa-folder-open"></i> <strong>R Built-in data sets</strong></a></li>
</ol>
<ul>
<li>List of pre-loaded data</li>
<li>Loading a built-in R data</li>
<li>Most used R built-in data sets
<ul>
<li>mtcars: Motor Trend Car Road Tests</li>
<li>iris</li>
<li>ToothGrowth</li>
<li>PlantGrowth</li>
<li>USArrests</li>
</ul></li>
</ul>
<p><span class="success readmore">Read more: <a href="https://www.sthda.com/english/english/wiki/r-built-in-data-sets"><i class="fa fa-play"></i>R Built-in data sets</a></span></p>
</div>
<p><br/></p>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->

<!-- END HTML -->]]></description>
			<pubDate>Thu, 07 Apr 2016 19:18:53 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Installing and Using R Packages]]></title>
			<link>https://www.sthda.com/english/wiki/installing-and-using-r-packages</link>
			<guid>https://www.sthda.com/english/wiki/installing-and-using-r-packages</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#what-is-r-packages">What is R packages?</a></li>
<li><a href="#installing-r-packages">Installing R packages</a><ul>
<li><a href="#install-a-package-from-cran">Install a package from CRAN</a></li>
<li><a href="#install-a-package-from-bioconductor">Install a package from Bioconductor</a></li>
<li><a href="#install-a-package-from-github">Install a package from Github</a></li>
<li><a href="#view-the-list-of-installed-packages">View the list of installed packages</a></li>
<li><a href="#folder-containing-installed-packages">Folder containing installed packages</a></li>
</ul></li>
<li><a href="#load-and-use-an-r-package">Load and use an R package</a></li>
<li><a href="#view-loaded-r-packages">View loaded R packages</a></li>
<li><a href="#remove-installed-packages">Remove installed packages</a></li>
<li><a href="#update-installed-packages">Update installed packages</a></li>
<li><a href="#summary">Summary</a></li>
<li><a href="#related-articles">Related articles</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<p><br/></p>
<p>In our previous articles, we published i) guides for <a href="https://www.sthda.com/english/wiki/(installing-r-and-rstudio-easy-r-programming)">installing</a> and <a href="https://www.sthda.com/english/wiki/(running-rstudio-and-setting-up-your-working-directory-easy-r-programming)">launching R/RStudio</a>, ii) the <a href="https://www.sthda.com/english/english/wiki/easy-r-programming-basics">basics of R programming</a>, and ii) guides for <a href="https://www.sthda.com/english/english/wiki/getting-help-with-functions-in-r-programming">finding help in R</a>.</p>
<br/>
<div class="block">
<p>Here, we’ll describe:</p>
<ul>
<li>what is an <strong>R package</strong></li>
<li>and how to <strong>install</strong> and use <strong>R packages</strong></li>
</ul>
</div>
<p><br/></p>
<div id="what-is-r-packages" class="section level1">
<h1>What is R packages?</h1>
<p>An R package is an <strong>extension of R</strong> containing data sets and specific functions to solve specific questions.</p>
<p>R comes with standard (or base) packages, which contain the basic functions and data sets as well as standard statistical and graphical functions that allow R to work.</p>
<p>There are also thousands other R packages available for download and installation from <a href="https://cran.r-project.org/">CRAN</a>, <a href="https://www.bioconductor.org/">Bioconductor</a> and <a href="https://github.com/">GitHub</a> repositories.</p>
<p>After installation, you must first load the package for using the functions in the package.</p>
</div>
<div id="installing-r-packages" class="section level1">
<h1>Installing R packages</h1>
<p><span class="success">Packages can be installed either from <strong>CRAN</strong> (for general packages), from <strong>Bioconductor</strong> (for biology-related packages) or from <strong>Github</strong> (developing versions of packages). </span></p>
<div id="install-a-package-from-cran" class="section level2">
<h2>Install a package from CRAN</h2>
<p>The function <strong>install.packages</strong>() is used to install a package from CRAN. The syntax is as follow:</p>
<pre class="r"><code>install.packages("package_name")</code></pre>
<p>For example, to install the package named <a href="https://cran.r-project.org/web/packages/readr/index.html"><strong>readr</strong></a>, type this:</p>
<pre class="r"><code>install.packages("readr")</code></pre>
<p><span class="error">Note that, every time you install an R package, R may ask you to specify a CRAN mirror (or server). Choose one that’s close to your location, and R will connect to that server to download and install the package files.</span></p>
<p>It’s also possible to install multiple packages at the same time, as follow:</p>
<pre class="r"><code>install.packages(c("readr", "ggplot2"))</code></pre>
</div>
<div id="install-a-package-from-bioconductor" class="section level2">
<h2>Install a package from Bioconductor</h2>
<p>Bioconductor contains packages for analyzing biological related data. In the following R code, we want to install the R/Bioconductor package <strong>limma</strong>, which is dedicated to analyse genomic data.</p>
<p>To install a package from <strong>Bioconductor</strong>, use this:</p>
<pre class="r"><code>source("https://bioconductor.org/biocLite.R")
biocLite("limma")</code></pre>
</div>
<div id="install-a-package-from-github" class="section level2">
<h2>Install a package from Github</h2>
<p>GitHub is a repository useful for all software development and data analysis, including R packages. It makes sharing your package easy. You can read more about GitHub here: <a href="http://r-pkgs.had.co.nz/git.html">Git and GitHub, by Hadley Wickham</a>.</p>
<p>To install a package from GitHub, the R package <strong>devtools</strong> (by Hadley Wickham) can be used. You should first install <strong>devtools</strong> if you don’t have it installed on your computer.</p>
<p>For example, the following R code installs the latest version of <a href="https://github.com/kassambara/survminer"><strong>survminer</strong></a> R package developed by A. Kassambara (<a href="https://github.com/kassambara/survminer" class="uri">https://github.com/kassambara/survminer</a>).</p>
<pre class="r"><code>install.packages("devtools")
devtools::install_github("kassambara/survminer")</code></pre>
</div>
<div id="view-the-list-of-installed-packages" class="section level2">
<h2>View the list of installed packages</h2>
<p>To view the list of the already <strong>installed packages</strong> on your computer, type :</p>
<pre class="r"><code>installed.packages()</code></pre>
<p><span class="success">Note that, in RStudio, the list of installed packages are available in the lower right window under Packages tab (see the image below).</span></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/rstudio-installed-packages.png" alt="installed packages, RStudio" /></p>
</div>
<div id="folder-containing-installed-packages" class="section level2">
<h2>Folder containing installed packages</h2>
<p>R packages are installed in a directory called <strong>library</strong>. The R function <strong>.libPaths</strong>() can be used to get the path to the <strong>library</strong>.</p>
<pre class="r"><code>.libPaths()</code></pre>
<pre><code>[1] "/Library/Frameworks/R.framework/Versions/3.2/Resources/library"</code></pre>
</div>
</div>
<div id="load-and-use-an-r-package" class="section level1">
<h1>Load and use an R package</h1>
<p>To use a specific function available in an R package, you have to load the R package using the function <strong>library</strong>().</p>
<p>In the following R code, we want to import a file (“<a href="https://www.sthda.com/upload/decathlon.txt" class="uri">https://www.sthda.com/upload/decathlon.txt</a>”) into R using the R package <strong>readr</strong>, which has been installed in the previous section.</p>
<p>The function <strong>read_tsv</strong>() [in <strong>readr</strong>] can be used to import a tab separated .txt file:</p>
<pre class="r"><code># Import my data
library("readr")
my_data <- read_tsv("https://www.sthda.com/upload/decathlon.txt")

# View the first 6 rows and tge first 6 columns
# syntax: my_data[row, column]
my_data[1:6, 1:6]</code></pre>
<pre><code>     name  100m Long.jump Shot.put High.jump  400m
1  SEBRLE 11.04      7.58    14.83      2.07 49.81
2    CLAY 10.76      7.40    14.26      1.86 49.37
3  KARPOV 11.02      7.30    14.77      2.04 48.37
4 BERNARD 11.02      7.23    14.25      1.92 48.93
5  YURKOV 11.34      7.09    15.19      2.10 50.42
6 WARNERS 11.11      7.60    14.31      1.98 48.68</code></pre>
</div>
<div id="view-loaded-r-packages" class="section level1">
<h1>View loaded R packages</h1>
<p>To view the list of loaded (or attached) packages during an R session, use the function <strong>search</strong>():</p>
<pre class="r"><code>search()</code></pre>
<pre><code> [1] ".GlobalEnv"        "package:readr"     "package:stats"     "package:graphics" 
 [5] "package:grDevices" "package:utils"     "package:datasets"  "package:methods"  
 [9] "Autoloads"         "package:base"     </code></pre>
<p>If you’re done with the package <strong>readr</strong> and you want to unload it, use the function <strong>detach</strong>():</p>
<pre class="r"><code>detach("readr", unload = TRUE)</code></pre>
</div>
<div id="remove-installed-packages" class="section level1">
<h1>Remove installed packages</h1>
<p>To remove an installed R package, use the function <strong>remove.packages</strong>() as follow:</p>
<pre class="r"><code>remove.packages("package_name")</code></pre>
</div>
<div id="update-installed-packages" class="section level1">
<h1>Update installed packages</h1>
<p>If you want to update all installed R packages, type this:</p>
<pre class="r"><code>update.packages()</code></pre>
<p>To update specific installed packages, say <strong>readr</strong> and <strong>ggplot2</strong>, use this:</p>
<pre class="r"><code>update.packages(oldPkgs = c("readr", "ggplot2"))</code></pre>
</div>
<div id="summary" class="section level1">
<h1>Summary</h1>
<br/>
<div class="block">
<ul>
<li><p><strong>install.packages</strong>(“package_name”): Install a package</p></li>
<li><p><strong>library</strong>(“package_name”): Load and use a package</p></li>
<li><p><strong>detach</strong>(“package_name”, unload = TRUE): Unload a package</p></li>
<li><p><strong>remove.packages</strong>(“package_name”): Remove an installed package from your computer</p></li>
<li><strong>update.packages</strong>(oldPkgs = “package_name”): Update a package</li>
</ul>
</div>
<p><br/></p>
</div>
<div id="related-articles" class="section level1">
<h1>Related articles</h1>
<ul>
<li>Previous chapters
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/what-is-r-and-why-learning-r-programming">What’is R and why learning R?</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/installing-r-and-rstudio-easy-r-programming">Installing R and RStudio</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming">Running RStudio and setting up your working directory</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/easy-r-programming-basics">R programming basics</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/getting-help-with-functions-in-r-programming">Getting help with functions in R programming</a></li>
</ul></li>
<li>Next chapters
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/r-built-in-data-sets">R Built-in data sets</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/importing-data-into-r">Importing data into R</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/exporting-data-from-r">Exporting data from R</a></li>
</ul></li>
</ul>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using <strong>R software</strong> (ver. 3.2.3). </span></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->


<!-- END HTML -->]]></description>
			<pubDate>Thu, 07 Apr 2016 08:41:23 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Getting Help With Functions In R Programming]]></title>
			<link>https://www.sthda.com/english/wiki/getting-help-with-functions-in-r-programming</link>
			<guid>https://www.sthda.com/english/wiki/getting-help-with-functions-in-r-programming</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#getting-help-on-a-specific-function">Getting help on a specific function</a></li>
<li><a href="#general-help">General help</a></li>
<li><a href="#others">Others</a></li>
<li><a href="#related-articles">Related articles</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<p><br/></p>
<p>In our previous articles we described how to <a href="https://www.sthda.com/english/english/wiki/installing-r-and-rstudio-easy-r-programming">install</a> and <a href="https://www.sthda.com/english/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming">start using R/RStudio</a>. We also provide the <a href="https://www.sthda.com/english/english/wiki/easy-r-programming-basics">essentials of R programming</a>.</p>
<br/>
<div class="block">
Here, we’ll describe how to get <strong>help</strong> about a specific function in <strong>R</strong>
</div>
<p><br/></p>
<div id="getting-help-on-a-specific-function" class="section level1">
<h1>Getting help on a specific function</h1>
<p>To read more about a given function, for example <strong>mean</strong>, the R function <strong>help</strong>() can be used as follow:</p>
<pre class="r"><code>help(mean)</code></pre>
<p>Or use this:</p>
<pre class="r"><code>?mean</code></pre>
<p>The output look like this:</p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/help-mean.png" alt="Getting help in R" /></p>
<p>If you want to see some examples of how to use the function, type this: <strong>example</strong>(function_name).</p>
<pre class="r"><code>example(mean)</code></pre>
<p>Note that, typical R help files contain the following sections:</p>
<ul>
<li><strong>Title</strong></li>
<li><strong>Description</strong>: a short description of what the function does.</li>
<li><strong>Usage</strong>: the syntax of the function.</li>
<li><strong>Arguments</strong>: the description of the arguments taken by the function.</li>
<li><strong>Value</strong>: the value returned by the function</li>
<li><strong>Examples</strong>: provide examples on how to use the function</li>
</ul>
</div>
<div id="general-help" class="section level1">
<h1>General help</h1>
<p>If you want to read the general documentation about R, use the function <strong>help.start</strong>():</p>
<pre class="r"><code>help.start()</code></pre>
<p>The output is a web page, on most R installations, which can be browsed by clicking the hyperlinks.</p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/help-start.png" alt="Getting help in R, help.start" /></p>
</div>
<div id="others" class="section level1">
<h1>Others</h1>
<ul>
<li><strong>apropos</strong>(): returns a list of object, containing the pattern you searched, by partial matching. This is useful when you don’t remember exactly the name of the function:</li>
</ul>
<pre class="r"><code># Returns the list of object containing "med"
apropos("med")</code></pre>
<pre><code>[1] ".__C__namedList" "elNamed"         "elNamed<-"       "median"          "median.default" 
[6] "medpolish"       "runmed"         </code></pre>
<ul>
<li><strong>healp.search</strong>() (alternatively <strong>??</strong>): Search for documentation matching a given character in different ways. It returns a list of function containing your searched term with a short description of the function.</li>
</ul>
<pre class="r"><code>help.search("mean")

# Or use this
??mean</code></pre>
</div>
<div id="related-articles" class="section level1">
<h1>Related articles</h1>
<ul>
<li>Previous chapters
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/what-is-r-and-why-learning-r-programming">What’is R and why learning R?</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/installing-r-and-rstudio-easy-r-programming">Installing R and RStudio</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming">Running RStudio and setting up your working directory</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/easy-r-programming-basics">R programming basics</a></li>
</ul></li>
<li>Next chapters
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/installing-and-using-r-packages">Installing and using R packages</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/r-built-in-data-sets">R Built-in data sets</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/importing-data-into-r">Importing data into R</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/exporting-data-from-r">Exporting data from R</a></li>
</ul></li>
</ul>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using <strong>R software</strong> (ver. 3.2.3). </span></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->


<!-- END HTML -->]]></description>
			<pubDate>Thu, 07 Apr 2016 08:39:19 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Easy R Programming Basics]]></title>
			<link>https://www.sthda.com/english/wiki/easy-r-programming-basics</link>
			<guid>https://www.sthda.com/english/wiki/easy-r-programming-basics</guid>
			<description><![CDATA[<!-- START HTML -->
  
  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#basic-arithmetic-operations">Basic arithmetic operations</a></li>
<li><a href="#basic-arithmetic-functions">Basic arithmetic functions</a></li>
<li><a href="#assigning-values-to-variables">Assigning values to variables</a></li>
<li><a href="#basic-data-types">Basic data types</a></li>
<li><a href="#vectors">Vectors</a><ul>
<li><a href="#create-a-vector">Create a vector</a></li>
<li><a href="#case-of-missing-values">Case of missing values</a></li>
<li><a href="#get-a-subset-of-a-vector">Get a subset of a vector</a></li>
<li><a href="#calculations-with-vectors">Calculations with vectors</a></li>
</ul></li>
<li><a href="#matrices">Matrices</a><ul>
<li><a href="#create-and-naming-matrix">Create and naming matrix</a></li>
<li><a href="#dimensions-of-a-matrix">Dimensions of a matrix</a></li>
<li><a href="#get-a-subset-of-a-matrix">Get a subset of a matrix</a></li>
<li><a href="#calculations-with-matrices">Calculations with matrices</a></li>
</ul></li>
<li><a href="#factors">Factors</a><ul>
<li><a href="#create-a-factor">Create a factor</a></li>
<li><a href="#calculations-with-factors">Calculations with factors</a></li>
</ul></li>
<li><a href="#data-frames">Data frames</a><ul>
<li><a href="#create-a-data-frame">Create a data frame</a></li>
<li><a href="#subset-a-data-frame">Subset a data frame</a></li>
<li><a href="#extend-a-data-frame">Extend a data frame</a></li>
</ul></li>
<li><a href="#calculations-with-data-frame">Calculations with data frame</a></li>
<li><a href="#lists">Lists</a><ul>
<li><a href="#create-a-list">Create a list</a></li>
<li><a href="#subset-a-list">Subset a list</a></li>
<li><a href="#extend-a-list">Extend a list</a></li>
</ul></li>
<li><a href="#related-articles">Related articles</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<p><br/></p>
<p>Previously, we described how to <a href="https://www.sthda.com/english/english/wiki/installing-r-and-rstudio-easy-r-programming">install R/RStudio</a> as well as how to <a href="https://www.sthda.com/english/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming">launch R/RStudio and set up your working directory</a>.</p>
<br/>
<div class="block">
<p>Here, we described the basics you should know about <strong>R programming</strong>, including :</p>
<ul>
<li>Performing basic arithmetic operations and using basic arithmetic functions</li>
<li>Creating and subsetting basic data types in R</li>
</ul>
</div>
<p><br/></p>
<div id="basic-arithmetic-operations" class="section level1">
<h1>Basic arithmetic operations</h1>
<br/>
<div class="block">
R can be used as a calculator.
</div>
<p><br/></p>
<p>The basic arithmetic operators are:</p>
<ol style="list-style-type: decimal">
<li><strong>+</strong> (addition)</li>
<li><strong>-</strong> (subtraction)</li>
<li><strong>*</strong> (multiplication)</li>
<li><strong>/</strong> (division)</li>
<li>and <strong>^</strong> (exponentiation).</li>
</ol>
<p>Type directly the command below in the console:</p>
<pre class="r"><code># Addition
3 + 7</code></pre>
<pre><code>[1] 10</code></pre>
<pre class="r"><code># Substraction
7 - 3</code></pre>
<pre><code>[1] 4</code></pre>
<pre class="r"><code># Multiplication
3 * 7</code></pre>
<pre><code>[1] 21</code></pre>
<pre class="r"><code># Divison
7/3</code></pre>
<pre><code>[1] 2.333333</code></pre>
<pre class="r"><code># Exponentiation
2^3</code></pre>
<pre><code>[1] 8</code></pre>
<pre class="r"><code># Modulo: returns the remainder of the division of 8/3
8 %% 3</code></pre>
<pre><code>[1] 2</code></pre>
<p><span class="success">Note that, in R, ‘#’ is used for adding comments to explain what the R code is about.</span></p>
</div>
<div id="basic-arithmetic-functions" class="section level1">
<h1>Basic arithmetic functions</h1>
<ol style="list-style-type: decimal">
<li><strong>Logarithms and Exponentials</strong>:</li>
</ol>
<pre class="r"><code>log2(x) # logarithms base 2 of x
log10(x) # logaritms base 10 of x
exp(x) # Exponential of x</code></pre>
<ol start="3" style="list-style-type: decimal">
<li><strong>Trigonometric functions</strong>:</li>
</ol>
<pre class="r"><code>cos(x) # Cosine of x
sin(x) # Sine of x
tan(x) #Tangent of x

acos(x) # arc-cosine of x
asin(x) # arc-sine of x
atan(x) #arc-tangent of x</code></pre>
<ol start="4" style="list-style-type: decimal">
<li><strong>Other mathematical functions</strong></li>
</ol>
<pre class="r"><code>abs(x) # absolute value of x

sqrt(x) # square root of x</code></pre>
</div>
<div id="assigning-values-to-variables" class="section level1">
<h1>Assigning values to variables</h1>
<br/>
<div class="block">
A variable can be used to store a value.
</div>
<p><br/></p>
<p>For example, the R code below will store the price of a lemon in a variable, say “lemon_price”:</p>
<pre class="r"><code># Price of a lemon = 2 euros
lemon_price <- 2
# or use this
lemon_price = 2</code></pre>
<p><span class="success">Note that, it’s possible to use <strong><-</strong> or <strong>=</strong> for variable assignments.</span></p>
<p><span class="error"> Note that, R is case-sensitive. This means that <em>lemon_price</em> is different from <em>Lemon_Price</em>.</span></p>
<p>To print the value of the created object, just type its name:</p>
<pre class="r"><code>lemon_price</code></pre>
<pre><code>[1] 2</code></pre>
<p>or use the function <strong>print()</strong>:</p>
<pre class="r"><code>print(lemon_price)</code></pre>
<pre><code>[1] 2</code></pre>
<p><span class="success">R saves the object <em>lemon_price</em> (also known as a variable) in memory. It’s possible to make some operations with it.</span></p>
<pre class="r"><code># Multiply lemon price by 5
5 * lemon_price</code></pre>
<pre><code>[1] 10</code></pre>
<p>You can change the value of the object:</p>
<pre class="r"><code># Change the value
lemon_price <- 5
# Print again
lemon_price</code></pre>
<pre><code>[1] 5</code></pre>
<p>The following R code creates two variables holding the width and the height of a rectangle. These two variables will be used to compute of the rectangle.</p>
<pre class="r"><code># Rectangle height
height <- 10

# rectangle width
width <- 5

# compute rectangle area
area <- height*width
print(area)</code></pre>
<pre><code>[1] 50</code></pre>
<p>The function <strong>ls()</strong> can be used to see the list of objects we have created:</p>
<pre class="r"><code>ls()</code></pre>
<pre><code>[1] "area"        "height"      "info"        "lemon_price" "PACKAGES"    "R_VERSION"  
[7] "width"      </code></pre>
<p><span class="success"> The collection of objects currently stored is called the <strong>workspace</strong>.</span></p>
<p><span class="warning">Note that, each variable takes some place in the computer memory. If you work on a big project, it’s good to clean up your workspace.</span></p>
<p>To remove a variable, use the function <strong>rm</strong>():</p>
<pre class="r"><code># Remove height and width variable
rm(height, width)

# Display the remaining variables
ls()</code></pre>
<pre><code>[1] "area"        "info"        "lemon_price" "PACKAGES"    "R_VERSION"  </code></pre>
</div>
<div id="basic-data-types" class="section level1">
<h1>Basic data types</h1>
<br/>
<div class="block">
Basic data types are <strong>numeric</strong>, <strong>character</strong> and <strong>logical</strong>.
</div>
<p><br/></p>
<pre class="r"><code># Numeric object: How old are you?
my_age <- 28

# Character  object: What&amp;#39;s your name?
my_name <- "Nicolas"

# logical object: Are you a data scientist?
# (yes/no) <=> (TRUE/FALSE)
is_datascientist <- TRUE</code></pre>
<p><span class="warning"> Note that, character vector can be created using double (“) or single (’) quotes. If your text contains quotes, you should escape them using”\" as follow.</span></p>
<pre class="r"><code>&amp;#39;My friend\&amp;#39;s name is "Jerome"&amp;#39;</code></pre>
<pre><code>[1] "My friend&amp;#39;s name is \"Jerome\""</code></pre>
<pre class="r"><code># or use this
"My friend&amp;#39;s name is \"Jerome\""</code></pre>
<pre><code>[1] "My friend&amp;#39;s name is \"Jerome\""</code></pre>
<p>It’s possible to use the function <strong>class</strong>() to see what type a variable is:</p>
<pre class="r"><code>class(my_age)</code></pre>
<pre><code>[1] "numeric"</code></pre>
<pre class="r"><code>class(my_name)</code></pre>
<pre><code>[1] "character"</code></pre>
<p>You can also use the functions <strong>is.numeric</strong>(), <strong>is.character</strong>(), <strong>is.logical</strong>() to check whether a variable is numeric, character or logical, respectively. For instance:</p>
<pre class="r"><code>is.numeric(my_age)</code></pre>
<pre><code>[1] TRUE</code></pre>
<pre class="r"><code>is.numeric(my_name)</code></pre>
<pre><code>[1] FALSE</code></pre>
<p>If you want to change the type of a variable to another one, use the <strong>as.*</strong> functions, including: <strong>as.numeric</strong>(), <strong>as.character</strong>(), <strong>as.logical</strong>(), etc.</p>
<pre class="r"><code>my_age</code></pre>
<pre><code>[1] 28</code></pre>
<pre class="r"><code># Convert my_age to a character variable
as.character(my_age)</code></pre>
<pre><code>[1] "28"</code></pre>
<p><span class="error">Note that, the conversion of a character to a numeric will output NA (for not available). R doesn’t know how to convert a numeric variable to a character variable.</span></p>
</div>
<div id="vectors" class="section level1">
<h1>Vectors</h1>
<br/>
<div class="block">
A vector is a combination of multiple values (numeric, character or logical) in the same object. In this case, you can have <strong>numeric vectors</strong>, <strong>character vectors</strong> or <strong>logical vectors</strong>.
</div>
<p><br/></p>
<div id="create-a-vector" class="section level2">
<h2>Create a vector</h2>
<p>A vector is created using the function <strong>c()</strong> (for <em>concatenate</em>), as follow:</p>
<pre class="r"><code># Store your friends&amp;#39;age in a numeric vector
friend_ages <- c(27, 25, 29, 26) # Create
friend_ages # Print</code></pre>
<pre><code>[1] 27 25 29 26</code></pre>
<pre class="r"><code># Store your friend names in a character vector
my_friends <- c("Nicolas", "Thierry", "Bernard", "Jerome")
my_friends </code></pre>
<pre><code>[1] "Nicolas" "Thierry" "Bernard" "Jerome" </code></pre>
<pre class="r"><code># Store your friends marital status in a logical vector
# Are they married? (yes/no <=> TRUE/FALSE)
are_married <- c(TRUE, FALSE, TRUE, TRUE)
are_married</code></pre>
<pre><code>[1]  TRUE FALSE  TRUE  TRUE</code></pre>
<p><span class="success">It’s possible to give a name to the elements of a vector using the function <strong>names()</strong>.</span></p>
<pre class="r"><code># Vector without element names
friend_ages</code></pre>
<pre><code>[1] 27 25 29 26</code></pre>
<pre class="r"><code># Vector with element names
names(friend_ages) <- c("Nicolas", "Thierry", "Bernard", "Jerome")
friend_ages</code></pre>
<pre><code>Nicolas Thierry Bernard  Jerome 
     27      25      29      26 </code></pre>
<pre class="r"><code># You can also create a named vector as follow
friend_ages <- c(Nicolas = 27, Thierry = 25, 
                 Bernard = 29, Jerome = 26)
friend_ages</code></pre>
<pre><code>Nicolas Thierry Bernard  Jerome 
     27      25      29      26 </code></pre>
<p><span class="warning">Note that a vector can only hold elements of the same type. For example, you cannot have a vector that contains both characters and numeric values.</span></p>
<ul>
<li><strong>Find the length of a vector</strong> (i.e., the number of elements in a vector)</li>
</ul>
<pre class="r"><code># Number of friends
length(my_friends)</code></pre>
<pre><code>[1] 4</code></pre>
</div>
<div id="case-of-missing-values" class="section level2">
<h2>Case of missing values</h2>
<p>I know that some of my friends (Nicolas and Thierry) have 2 child. But this information is not available (NA) for the remaining friends (Bernard and Jerome).</p>
<p>In R <strong>missing values</strong> (or missing information) are represented by NA:</p>
<pre class="r"><code>have_child <- c(Nicolas = "yes", Thierry = "yes", 
                Bernard = NA, Jerome = NA)
have_child</code></pre>
<pre><code>Nicolas Thierry Bernard  Jerome 
  "yes"   "yes"      NA      NA </code></pre>
<p>It’s possible to use the function <strong>is.na</strong>() to check whether a data contains missing value. The result of the function <strong>is.na</strong>() is a logical vector in which, the value TRUE specifies that the corresponding element in x is NA.</p>
<pre class="r"><code># Check if have_child contains missing values
is.na(have_child)</code></pre>
<pre><code>Nicolas Thierry Bernard  Jerome 
  FALSE   FALSE    TRUE    TRUE </code></pre>
<p><span class="error">Note that, there is a second type of <strong>missing values</strong> named <strong>NaN</strong> (“Not a Number”). This is produced in a situation where mathematical function won’t work properly, for example 0/0 = NaN.</span></p>
<p><span class="success">Note also that, the function <strong>is.na</strong>() is TRUE for both NA and NaN values. To differentiate these, the function <strong>is.nan</strong>() is only TRUE for NaNs. </span></p>
</div>
<div id="get-a-subset-of-a-vector" class="section level2">
<h2>Get a subset of a vector</h2>
<p><span class="success">Subsetting a vector consists of selecting a part of your vector.</span></p>
<ul>
<li><strong>Selection by positive indexing</strong>: select an element of a vector by its position (index) in square brackets</li>
</ul>
<pre class="r"><code># Select my friend number 2
my_friends[2]</code></pre>
<pre><code>[1] "Thierry"</code></pre>
<pre class="r"><code># Select my friends number 2 and 4 
my_friends[c(2, 4)]</code></pre>
<pre><code>[1] "Thierry" "Jerome" </code></pre>
<pre class="r"><code># Select my friends number 1 to 3
my_friends[1:3]</code></pre>
<pre><code>[1] "Nicolas" "Thierry" "Bernard"</code></pre>
<p><span class="warning">Note that, <strong>R indexes from 1</strong>, NOT 0. So your first column is at [1] and not [0].</span></p>
<p>If you have a named vector, it’s also possible to use the name for selecting an element:</p>
<pre class="r"><code>friend_ages["Bernard"]</code></pre>
<pre><code>Bernard 
     29 </code></pre>
<ul>
<li><strong>Selection by negative indexing</strong>: Exclude an element</li>
</ul>
<pre class="r"><code># Exclude my friend number 2
my_friends[-2]</code></pre>
<pre><code>[1] "Nicolas" "Bernard" "Jerome" </code></pre>
<pre class="r"><code># Exclude my friends number 2 and 4
my_friends[-c(2, 4)]</code></pre>
<pre><code>[1] "Nicolas" "Bernard"</code></pre>
<pre class="r"><code># Exclude my friends number 1 to 3
my_friends[-(1:3)]</code></pre>
<pre><code>[1] "Jerome"</code></pre>
<ul>
<li><strong>Selection by logical vector</strong>: Only, the elements for which the corresponding value in the selecting vector is TRUE, will be kept in the subset.</li>
</ul>
<pre class="r"><code># Select only married friends
my_friends[are_married == TRUE]</code></pre>
<pre><code>[1] "Nicolas" "Bernard" "Jerome" </code></pre>
<pre class="r"><code># Friends with age >=27
my_friends[friend_ages >= 27]</code></pre>
<pre><code>[1] "Nicolas" "Bernard"</code></pre>
<pre class="r"><code># Friends with age different from 27
my_friends[friend_ages != 27]</code></pre>
<pre><code>[1] "Thierry" "Bernard" "Jerome" </code></pre>
<p>If you want to remove missing data, use this:</p>
<pre class="r"><code># Data with missing values
have_child</code></pre>
<pre><code>Nicolas Thierry Bernard  Jerome 
  "yes"   "yes"      NA      NA </code></pre>
<pre class="r"><code># Keep only values different from NA (!is.na())
have_child[!is.na(have_child)]</code></pre>
<pre><code>Nicolas Thierry 
  "yes"   "yes" </code></pre>
<pre class="r"><code># Or, replace NA value by "NO" and then print
have_child[!is.na(have_child)] <- "NO"
have_child</code></pre>
<pre><code>Nicolas Thierry Bernard  Jerome 
   "NO"    "NO"      NA      NA </code></pre>
<br/>
<div class="success">
<p>Note that, the “logical” comparison operators available in R are:</p>
<ul>
<li><strong><</strong>: for less than</li>
<li><strong>></strong>: for greater than</li>
<li><strong><=</strong>: for less than or equal to</li>
<li><strong>>=</strong>: for greater than or equal to</li>
<li><strong>==</strong>: for equal to each other</li>
<li><strong>!=</strong>: not equal to each other</li>
</ul>
</div>
<p><br/></p>
</div>
<div id="calculations-with-vectors" class="section level2">
<h2>Calculations with vectors</h2>
<p><span class="success"> Note that, all the basic arithmetic operators (+, -, *, / and ^ ) as well as the common arithmetic functions (log, exp, sin, cos, tan, sqrt, abs, …), described in the previous sections, can be applied on a numeric vector.</span></p>
<p>If you perform an operation with vectors, the operation will be applied to each element of the vector. An example is provided below:</p>
<pre class="r"><code># My friends&amp;#39; salary in dollars
salaries <- c(2000, 1800, 2500, 3000)
names(salaries) <- c("Nicolas", "Thierry", "Bernard", "Jerome")
salaries</code></pre>
<pre><code>Nicolas Thierry Bernard  Jerome 
   2000    1800    2500    3000 </code></pre>
<pre class="r"><code># Multiply salaries by 2
salaries*2</code></pre>
<pre><code>Nicolas Thierry Bernard  Jerome 
   4000    3600    5000    6000 </code></pre>
<p><span class="success">As you can see, R multiplies each element in the salaries vector with 2.</span></p>
<p>Now, suppose that you want to multiply the salaries by different coefficients. The following R code can be used:</p>
<pre class="r"><code># create coefs vector with the same length as salaries
coefs <- c(2, 1.5, 1, 3)

# Multiply salaries by coeff
salaries*coefs</code></pre>
<pre><code>Nicolas Thierry Bernard  Jerome 
   4000    2700    2500    9000 </code></pre>
<p><span class="success">Note that the calculation is done element-wise. The first element of salaries vector is multiplied by the first element of coefs vector, and so on.</span></p>
<p>Compute the square root of a numeric vector:</p>
<pre class="r"><code>my_vector <- c(4, 16, 9)
sqrt(my_vector)</code></pre>
<pre><code>[1] 2 4 3</code></pre>
<p>Other useful functions are:</p>
<pre class="r"><code>max(x) # Get the maximum value of x
min(x) # Get the minimum value of x

# Get the range of x. Returns a vector containing
# the minimum and the maximum of x
range(x) 
   
length(x) # Get the number of elements in x
  
sum(x) # Get the total of the elements in x
  
prod(x) # Get the product of the elements in x
 
# The mean value of the elements in x
# sum(x)/length(x)
mean(x) 

sd(x) # Standard deviation of x
var(x) # Variance of x

# Sort the element of x in ascending order
sort(x)</code></pre>
<p>For example, if you want to compute the total <strong>sum</strong> of salaries, type this:</p>
<pre class="r"><code>sum(salaries)</code></pre>
<pre><code>[1] 9300</code></pre>
<p>Compute the <strong>mean</strong> of salaries:</p>
<pre class="r"><code>mean(salaries)</code></pre>
<pre><code>[1] 2325</code></pre>
<p>The range (minimum, maximum) of salaries is:</p>
<pre class="r"><code>range(salaries)</code></pre>
<pre><code>[1] 1800 3000</code></pre>
</div>
</div>
<div id="matrices" class="section level1">
<h1>Matrices</h1>
<br/>
<div class="block">
A <strong>matrix</strong> is like an Excel sheet containing multiple rows and columns. It’s used to combine vectors with the same type, which can be either numeric, character or logical. Matrices are used to store a data table in R. The rows of a matrix are generally individuals/observations and the columns are variables.
</div>
<p><br/></p>
<div id="create-and-naming-matrix" class="section level2">
<h2>Create and naming matrix</h2>
<p>To create easily a matrix, use the function <strong>cbind</strong>() or <strong>rbind</strong>() as follow:</p>
<pre class="r"><code># Numeric vectors
col1 <- c(5, 6, 7, 8, 9)
col2 <- c(2, 4, 5, 9, 8)
col3 <- c(7, 3, 4, 8, 7)
# Combine the vectors by column
my_data <- cbind(col1, col2, col3)
my_data</code></pre>
<pre><code>     col1 col2 col3
[1,]    5    2    7
[2,]    6    4    3
[3,]    7    5    4
[4,]    8    9    8
[5,]    9    8    7</code></pre>
<pre class="r"><code># Change rownames
rownames(my_data) <- c("row1", "row2", "row3", "row4", "row5")
my_data</code></pre>
<pre><code>     col1 col2 col3
row1    5    2    7
row2    6    4    3
row3    7    5    4
row4    8    9    8
row5    9    8    7</code></pre>
<br/>
<div class="block">
<ul>
<li><strong>cbind()</strong>: combine R objects by columns</li>
<li><strong>rbind()</strong>: combine R objects by rows</li>
<li><strong>rownames()</strong>: retrieve or set row names of a matrix-like object</li>
<li><strong>colnames()</strong>: retrieve or set column names of a matrix-like object</li>
</ul>
</div>
<p><br/></p>
<p>If you want to transpose your data, use the function <strong>t</strong>():</p>
<pre class="r"><code>t(my_data)</code></pre>
<pre><code>     row1 row2 row3 row4 row5
col1    5    6    7    8    9
col2    2    4    5    9    8
col3    7    3    4    8    7</code></pre>
<p><span class="success">Note that, it’s also possible to construct a matrix using the function <strong>matrix()</strong>.</span></p>
<p>The simplified format of <strong>matrix()</strong> is as follow:</p>
<pre class="r"><code>matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE,
       dimnames = NULL)</code></pre>
<br/>
<div class="block">
<ul>
<li><strong>data</strong>: an optional data vector</li>
<li><strong>nrow</strong>, <strong>ncol</strong>: the desired number of rows and columns, respectively.</li>
<li><strong>byrow</strong>: logical value. If FALSE (the default) the matrix is filled by columns, otherwise the matrix is filled by rows.</li>
<li><strong>dimnames</strong>: A list of two vectors giving the row and column names respectively.</li>
</ul>
</div>
<p><br/></p>
<p>In the R code below, the input data has length 6. We want to create a matrix with two columns. You don’t need to specify the number of rows (here nrow = 3). R will infer this automatically. The matrix is filled column by column when the argument <strong>byrow = FALSE</strong>. If you want to fill the matrix by rows, use <strong>byrow = TRUE</strong>.</p>
<pre class="r"><code>mdat <- matrix(
           data = c(1,2,3, 11,12,13), 
           nrow = 2, byrow = TRUE,
           dimnames = list(c("row1", "row2"), c("C.1", "C.2", "C.3"))
           )
mdat</code></pre>
<pre><code>     C.1 C.2 C.3
row1   1   2   3
row2  11  12  13</code></pre>
</div>
<div id="dimensions-of-a-matrix" class="section level2">
<h2>Dimensions of a matrix</h2>
<p>The R functions <strong>nrow</strong>() and <strong>ncol</strong>() return the number of rows and columns present in the data, respectively.</p>
<pre class="r"><code>ncol(my_data) # Number of columns</code></pre>
<pre><code>[1] 3</code></pre>
<pre class="r"><code>nrow(my_data) # Number of rows</code></pre>
<pre><code>[1] 5</code></pre>
<pre class="r"><code>dim(my_data) # Number of rows and columns</code></pre>
<pre><code>[1] 5 3</code></pre>
</div>
<div id="get-a-subset-of-a-matrix" class="section level2">
<h2>Get a subset of a matrix</h2>
<ul>
<li><strong>Select rows/columns</strong> by positive indexing</li>
</ul>
<p>rows and/or columns can be selected as follow: my_data[row, col]</p>
<pre class="r"><code># Select row number 2
my_data[2, ]</code></pre>
<pre><code>col1 col2 col3 
   6    4    3 </code></pre>
<pre class="r"><code># Select row number 2 to 4
my_data[2:4, ]</code></pre>
<pre><code>     col1 col2 col3
row2    6    4    3
row3    7    5    4
row4    8    9    8</code></pre>
<pre class="r"><code># Select multiple rows that aren&amp;#39;t contiguous
# e.g.: rows 2 and 4 but not 3
my_data[c(2,4), ]</code></pre>
<pre><code>     col1 col2 col3
row2    6    4    3
row4    8    9    8</code></pre>
<pre class="r"><code># Select column number 3
my_data[, 3]</code></pre>
<pre><code>row1 row2 row3 row4 row5 
   7    3    4    8    7 </code></pre>
<pre class="r"><code># Select the value at row 2 and column  3
my_data[2, 3]</code></pre>
<pre><code>[1] 3</code></pre>
<ul>
<li><strong>Select by row/column names</strong></li>
</ul>
<pre class="r"><code># Select column 2
my_data[, "col2"]</code></pre>
<pre><code>row1 row2 row3 row4 row5 
   2    4    5    9    8 </code></pre>
<pre class="r"><code># Select by index and names: row 3 and olumn 2
my_data[3, "col2"]</code></pre>
<pre><code>[1] 5</code></pre>
<ul>
<li><strong>Exclude rows/columns</strong> by negative indexing</li>
</ul>
<pre class="r"><code># Exclude column 1
my_data[, -1]</code></pre>
<pre><code>     col2 col3
row1    2    7
row2    4    3
row3    5    4
row4    9    8
row5    8    7</code></pre>
<ul>
<li><strong>Selection by logical</strong>: In the R code below, we want to keep only rows where col3 >=4:</li>
</ul>
<pre class="r"><code>col3 <- my_data[, "col3"]
my_data[col3 >= 4, ]</code></pre>
<pre><code>     col1 col2 col3
row1    5    2    7
row3    7    5    4
row4    8    9    8
row5    9    8    7</code></pre>
</div>
<div id="calculations-with-matrices" class="section level2">
<h2>Calculations with matrices</h2>
<ul>
<li>It’s also possible to perform <strong>simple operations on matrice</strong>. For example, the following R code multiplies each element of the matrix by 2:</li>
</ul>
<pre class="r"><code>my_data*2</code></pre>
<pre><code>     col1 col2 col3
row1   10    4   14
row2   12    8    6
row3   14   10    8
row4   16   18   16
row5   18   16   14</code></pre>
<p>Or, compute the log2 values:</p>
<pre class="r"><code>log2(my_data)</code></pre>
<pre><code>         col1     col2     col3
row1 2.321928 1.000000 2.807355
row2 2.584963 2.000000 1.584963
row3 2.807355 2.321928 2.000000
row4 3.000000 3.169925 3.000000
row5 3.169925 3.000000 2.807355</code></pre>
<ul>
<li><strong>rowSums()</strong> and <strong>colSums()</strong> functions: Compute the total of each row and the total of each column, respectively.</li>
</ul>
<pre class="r"><code># Total of each row
rowSums(my_data)</code></pre>
<pre><code>row1 row2 row3 row4 row5 
  14   13   16   25   24 </code></pre>
<pre class="r"><code># Total of each column
colSums(my_data)</code></pre>
<pre><code>col1 col2 col3 
  35   28   29 </code></pre>
<p>If you are interested in row/column means, you can use the function <strong>rowMeans</strong>() and <strong>colMeans</strong>() for computing row and column means, respectively.</p>
<p><span class="warning">Note that, it’s also possible to use the function <strong>apply</strong>() to apply any statistical functions to rows/columns of matrices. </span></p>
<p>The simplified format of <strong>apply</strong>() is as follow:</p>
<pre class="r"><code>apply(X, MARGIN, FUN)</code></pre>
<ul>
<li>X: your data matrix</li>
<li>MARGIN: possible values are 1 (for rows) and 2 (for columns)</li>
<li>FUN: the function to apply on rows/columns</li>
</ul>
<p>Use <strong>apply</strong>() as follow:</p>
<pre class="r"><code># Compute row means
apply(my_data, 1, mean)</code></pre>
<pre><code>    row1     row2     row3     row4     row5 
4.666667 4.333333 5.333333 8.333333 8.000000 </code></pre>
<pre class="r"><code># Compute row medians
apply(my_data, 1, median)</code></pre>
<pre><code>row1 row2 row3 row4 row5 
   5    4    5    8    8 </code></pre>
<pre class="r"><code># Compute column means
apply(my_data, 2, mean)</code></pre>
<pre><code>col1 col2 col3 
 7.0  5.6  5.8 </code></pre>
</div>
</div>
<div id="factors" class="section level1">
<h1>Factors</h1>
<br/>
<div class="block">
Factor variables represent categories or groups in your data. The function <strong>factor</strong>() can be used to create a factor variable.
</div>
<p><br/></p>
<div id="create-a-factor" class="section level2">
<h2>Create a factor</h2>
<pre class="r"><code># Create a factor variable
friend_groups <- factor(c(1, 2, 1, 2))
friend_groups</code></pre>
<pre><code>[1] 1 2 1 2
Levels: 1 2</code></pre>
<p><span class="success">The variable <em>friend_groups</em> contains two categories of friends: 1 and 2. In R terminology, categories are called <strong>factor levels</strong>.</span></p>
<p>It’s possible to access to the factor levels using the function <strong>levels()</strong>:</p>
<pre class="r"><code># Get group names (or levels)
levels(friend_groups)</code></pre>
<pre><code>[1] "1" "2"</code></pre>
<pre class="r"><code># Change levels
levels(friend_groups) <- c("best_friend", "not_best_friend")
friend_groups</code></pre>
<pre><code>[1] best_friend     not_best_friend best_friend     not_best_friend
Levels: best_friend not_best_friend</code></pre>
<p><span class="warning">Note that, R orders factor levels alphabetically. If you want a different order in the levels, you can specify the levels argument in the factor function as follow.</span></p>
<pre class="r"><code># Change the order of levels
friend_groups <- factor(friend_groups, 
                      levels = c("not_best_friend", "best_friend"))
# Print
friend_groups</code></pre>
<pre><code>[1] best_friend     not_best_friend best_friend     not_best_friend
Levels: not_best_friend best_friend</code></pre>
<br/>
<div class="warning">
<p>Note that:</p>
<ul>
<li>The function <strong>is.factor</strong>() can be used to check whether a variable is a factor. Results are TRUE (if factor) or FALSE (if not factor)</li>
<li>The function <strong>as.factor</strong>() can be used to convert a variable to a factor.</li>
</ul>
</div>
<p><br/></p>
<pre class="r"><code># Check if friend_groups is a factor
is.factor(friend_groups)</code></pre>
<pre><code>[1] TRUE</code></pre>
<pre class="r"><code># Check if "are_married" is a factor
is.factor(are_married)</code></pre>
<pre><code>[1] FALSE</code></pre>
<pre class="r"><code># Convert "are_married" as a factor
as.factor(are_married)</code></pre>
<pre><code>[1] TRUE  FALSE TRUE  TRUE 
Levels: FALSE TRUE</code></pre>
</div>
<div id="calculations-with-factors" class="section level2">
<h2>Calculations with factors</h2>
<ul>
<li>If you want to know the number of individuals in each levels, use the function <strong>summary()</strong>:</li>
</ul>
<pre class="r"><code>summary(friend_groups)</code></pre>
<pre><code>not_best_friend     best_friend 
              2               2 </code></pre>
<ul>
<li>In the following example, I want to compute the mean salary of my friends by groups. The function <strong>tapply</strong>() can be used to apply a function, here <strong>mean</strong>(), to each group.</li>
</ul>
<pre class="r"><code># Salaries of my friends
salaries</code></pre>
<pre><code>Nicolas Thierry Bernard  Jerome 
   2000    1800    2500    3000 </code></pre>
<pre class="r"><code># Friend groups
friend_groups</code></pre>
<pre><code>[1] best_friend     not_best_friend best_friend     not_best_friend
Levels: not_best_friend best_friend</code></pre>
<pre class="r"><code># Compute the mean salaries by groups
mean_salaries <- tapply(salaries, friend_groups, mean)
mean_salaries</code></pre>
<pre><code>not_best_friend     best_friend 
           2400            2250 </code></pre>
<pre class="r"><code># Compute the size/length of each group
tapply(salaries, friend_groups, length)</code></pre>
<pre><code>not_best_friend     best_friend 
              2               2 </code></pre>
<ul>
<li>It’s also possible to use the function <strong>table</strong>() to create a frequency table, also known as a contingency table of the counts at each combination of factor levels.</li>
</ul>
<pre class="r"><code>table(friend_groups)</code></pre>
<pre><code>friend_groups
not_best_friend     best_friend 
              2               2 </code></pre>
<pre class="r"><code># Cross-tabulation between 
# friend_groups and are_married variables
table(friend_groups, are_married)</code></pre>
<pre><code>                 are_married
friend_groups     FALSE TRUE
  not_best_friend     1    1
  best_friend         0    2</code></pre>
</div>
</div>
<div id="data-frames" class="section level1">
<h1>Data frames</h1>
<br/>
<div class="block">
A data frame is like a matrix but can have columns with different types (numeric, character, logical). Rows are observations (individuals) and columns are variables.
</div>
<p><br/></p>
<div id="create-a-data-frame" class="section level2">
<h2>Create a data frame</h2>
<p>A data frame can be created using the function <strong>data.frame()</strong>, as follow:</p>
<pre class="r"><code># Create a data frame
friends_data <- data.frame(
  name = my_friends,
  age = friend_ages,
  height = c(180, 170, 185, 169),
  married = are_married
)
# Print
friends_data</code></pre>
<pre><code>           name age height married
Nicolas Nicolas  27    180    TRUE
Thierry Thierry  25    170   FALSE
Bernard Bernard  29    185    TRUE
Jerome   Jerome  26    169    TRUE</code></pre>
<p>To check whether a data is a data frame, use the <strong>is.data.frame</strong>() function. Returns TRUE if the data is a data frame:</p>
<pre class="r"><code>is.data.frame(friends_data)</code></pre>
<pre><code>[1] TRUE</code></pre>
<pre class="r"><code>is.data.frame(my_data)</code></pre>
<pre><code>[1] FALSE</code></pre>
<p>The object “friends_data” is a data frame, but not the object “my_data”. We can convert-it to a data frame using the <strong>as.data.frame</strong>() function:</p>
<pre class="r"><code># What is the class of my_data? --> matrix
class(my_data)</code></pre>
<pre><code>[1] "matrix"</code></pre>
<pre class="r"><code># Convert it as a data frame
my_data2 <- as.data.frame(my_data)
# Now, the class is data.frame
class(my_data2)</code></pre>
<pre><code>[1] "data.frame"</code></pre>
<p>As described in <strong>matrix</strong> section, you can use the function <strong>t</strong>() to transpose a data frame:</p>
<pre class="r"><code>t(friends_data)</code></pre>
</div>
<div id="subset-a-data-frame" class="section level2">
<h2>Subset a data frame</h2>
<p><span class="success">To select just certain columns from a data frame, you can either refer to the columns by name or by their location (i.e., column 1, 2, 3, etc.).</span></p>
<ol style="list-style-type: decimal">
<li><strong>Positive indexing</strong> by name and by location</li>
</ol>
<pre class="r"><code># Access the data in &amp;#39;name&amp;#39; column
# dollar sign is used
friends_data$name</code></pre>
<pre><code>[1] Nicolas Thierry Bernard Jerome 
Levels: Bernard Jerome Nicolas Thierry</code></pre>
<pre class="r"><code># or use this
friends_data[, &amp;#39;name&amp;#39;]</code></pre>
<pre><code>[1] Nicolas Thierry Bernard Jerome 
Levels: Bernard Jerome Nicolas Thierry</code></pre>
<pre class="r"><code># Subset columns 1 and 3
friends_data[ , c(1, 3)]</code></pre>
<pre><code>           name height
Nicolas Nicolas    180
Thierry Thierry    170
Bernard Bernard    185
Jerome   Jerome    169</code></pre>
<ol start="2" style="list-style-type: decimal">
<li><strong>Negative indexing</strong></li>
</ol>
<pre class="r"><code># Exclude column 1
friends_data[, -1]</code></pre>
<pre><code>        age height married
Nicolas  27    180    TRUE
Thierry  25    170   FALSE
Bernard  29    185    TRUE
Jerome   26    169    TRUE</code></pre>
<ol start="3" style="list-style-type: decimal">
<li><strong>Index by characteristics</strong></li>
</ol>
<p>We want to select all friends with age >= 27.</p>
<pre class="r"><code># Identify rows that meet the condition
friends_data$age >= 27</code></pre>
<pre><code>[1]  TRUE FALSE  TRUE FALSE</code></pre>
<p><span class="success">TRUE specifies that the row contains a value of age >= 27.</span></p>
<pre class="r"><code># Select the rows that meet the condition
friends_data[friends_data$age >= 27, ]</code></pre>
<pre><code>           name age height married
Nicolas Nicolas  27    180    TRUE
Bernard Bernard  29    185    TRUE</code></pre>
<p><span class="success">The R code above, tells R to get all rows from friends_data where age >= 27, and then to return all the columns.</span></p>
<p>If you don’t want to see all the column data for the selected rows but are just interested in displaying, for example, friend names and age for friends with age >= 27, you could use the following R code:</p>
<pre class="r"><code># Use column locations
friends_data[friends_data$age >= 27,  c(1, 2)]</code></pre>
<pre><code>           name age
Nicolas Nicolas  27
Bernard Bernard  29</code></pre>
<pre class="r"><code># Or use column names
friends_data[friends_data$age >= 27, c("name", "age")]</code></pre>
<pre><code>           name age
Nicolas Nicolas  27
Bernard Bernard  29</code></pre>
<p>If you’re finding that your selection statement is starting to be inconvenient, you can put your row and column selections into variables first, such as:</p>
<pre class="r"><code>age27 <- friends_data$age >= 27
cols <- c("name", "age")</code></pre>
<p>Then you can select the rows and columns with those variables:</p>
<pre class="r"><code>friends_data[age27, cols]</code></pre>
<pre><code>           name age
Nicolas Nicolas  27
Bernard Bernard  29</code></pre>
<p><span class="success">It’s also possible to use the function <strong>subset</strong>() as follow.</span></p>
<pre class="r"><code># Select friends data with age >= 27
subset(friends_data, age >= 27)</code></pre>
<pre><code>           name age height married
Nicolas Nicolas  27    180    TRUE
Bernard Bernard  29    185    TRUE</code></pre>
<p><span class="success">Another option is to use the functions <strong>attach</strong>() and <strong>detach</strong>(). The function <strong>attach</strong>() takes a data frame and makes its columns accessible by simply giving their names. </span></p>
<p>The functions <strong>attach</strong>() and <strong>detach</strong>() can be used as follow:</p>
<pre class="r"><code># Attach a data frame
attach(friends_data)

# === Data manipulation ====
friends_data[age>=27, ]
# === End of data manipulation ====

# Detach the data frame
detach(friends_data)</code></pre>
</div>
<div id="extend-a-data-frame" class="section level2">
<h2>Extend a data frame</h2>
<p><strong>Add new column in a data frame</strong></p>
<pre class="r"><code># Add group column to friends_data
friends_data$group <- friend_groups
friends_data</code></pre>
<pre><code>           name age height married           group
Nicolas Nicolas  27    180    TRUE     best_friend
Thierry Thierry  25    170   FALSE not_best_friend
Bernard Bernard  29    185    TRUE     best_friend
Jerome   Jerome  26    169    TRUE not_best_friend</code></pre>
<p><span class="success">It’s also possible to use the functions <strong>cbind</strong>() and <strong>rbind</strong>() to extend a data frame.</span></p>
<pre class="r"><code>cbind(friends_data, group = friend_groups)</code></pre>
</div>
</div>
<div id="calculations-with-data-frame" class="section level1">
<h1>Calculations with data frame</h1>
<p>With numeric data frame, you can use the function <strong>rowSums</strong>(), <strong>colSums</strong>(), <strong>colMeans</strong>(), <strong>rowMeans</strong>() and <strong>apply</strong>() as described in <strong>matrix</strong> section.</p>
</div>
<div id="lists" class="section level1">
<h1>Lists</h1>
<br/>
<div class="block">
A list is an ordered collection of objects, which can be vectors, matrices, data frames, etc. In other words, a list can contain all kind of R objects.
</div>
<p><br/></p>
<div id="create-a-list" class="section level2">
<h2>Create a list</h2>
<pre class="r"><code># Create a list
my_family <- list(
  mother = "Veronique", 
  father = "Michel",
  sisters = c("Alicia", "Monica"),
  sister_age = c(12, 22)
  )
# Print
my_family</code></pre>
<pre><code>$mother
[1] "Veronique"

$father
[1] "Michel"

$sisters
[1] "Alicia" "Monica"

$sister_age
[1] 12 22</code></pre>
<pre class="r"><code># Names of elements in the list
names(my_family)</code></pre>
<pre><code>[1] "mother"     "father"     "sisters"    "sister_age"</code></pre>
<pre class="r"><code># Number of elements in the list
length(my_family)</code></pre>
<pre><code>[1] 4</code></pre>
<p>The list object “my_family”, contains four components, which may be individually referred to as my_family[[1]], as_family[[2]] and so on.</p>
</div>
<div id="subset-a-list" class="section level2">
<h2>Subset a list</h2>
<p>It’s possible to select an element, from a list, by its name or its index:</p>
<ul>
<li>my_family$mother is the same as my_family[[1]]</li>
<li>my_family$father is the same as my_family[[2]]</li>
</ul>
<pre class="r"><code># Select by name (1/2)
my_family$father</code></pre>
<pre><code>[1] "Michel"</code></pre>
<pre class="r"><code># Select by name (2/2)
my_family[["father"]]</code></pre>
<pre><code>[1] "Michel"</code></pre>
<pre class="r"><code># Select by index
my_family[[1]]</code></pre>
<pre><code>[1] "Veronique"</code></pre>
<pre class="r"><code>my_family[[3]]</code></pre>
<pre><code>[1] "Alicia" "Monica"</code></pre>
<pre class="r"><code># Select a specific element of a component
# select the first ([1]) element of my_family[[3]]
my_family[[3]][1] </code></pre>
<pre><code>[1] "Alicia"</code></pre>
</div>
<div id="extend-a-list" class="section level2">
<h2>Extend a list</h2>
<p><span class="success">Note that, it’s possible to extend an original list.</span></p>
<p>In the R code below, we want to add the components “grand_father” and “grand_mother” to <em>my_family</em> list object:</p>
<pre class="r"><code># Extend the list
my_family$grand_father <- "John"
my_family$grand_mother <- "Mary"
# Print
my_family</code></pre>
<pre><code>$mother
[1] "Veronique"

$father
[1] "Michel"

$sisters
[1] "Alicia" "Monica"

$sister_age
[1] 12 22

$grand_father
[1] "John"

$grand_mother
[1] "Mary"</code></pre>
<p>You can also concatenate two lists as follow:</p>
<pre class="r"><code>list_abc <- c(list_a, list_b, list_c)</code></pre>
<p>The result is a list also, whose components are those of the argument lists joined together in sequence.</p>
</div>
</div>
<div id="related-articles" class="section level1">
<h1>Related articles</h1>
<ul>
<li>Previous chapters
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/what-is-r-and-why-learning-r-programming">What’is R and why learning R?</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/installing-r-and-rstudio-easy-r-programming">Installing R and RStudio</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming">Running RStudio and setting up your working directory</a></li>
</ul></li>
<li>Next chapters
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/getting-help-with-functions-in-r-programming">Getting help with functions in R programming</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/installing-and-using-r-packages">Installing and using R packages</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/r-built-in-data-sets">R Built-in data sets</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/importing-data-into-r">Importing data into R</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/exporting-data-from-r">Exporting data from R</a></li>
</ul></li>
</ul>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using <strong>R software</strong> (ver. 3.2.3). </span></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->


<!-- END HTML -->]]></description>
			<pubDate>Thu, 07 Apr 2016 08:27:27 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Running RStudio and Setting Up Your Working Directory - Easy R Programming]]></title>
			<link>https://www.sthda.com/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming</link>
			<guid>https://www.sthda.com/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming</guid>
			<description><![CDATA[<!-- START HTML -->

            
  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#use-r-outside-rstudio">Use R outside RStudio</a><ul>
<li><a href="#under-windows-and-mac-osx">Under Windows and MAC OSX</a></li>
<li><a href="#under-linux">Under Linux</a></li>
</ul></li>
<li><a href="#use-r-inside-rstudio">Use R inside RStudio</a><ul>
<li><a href="#launch-rstudio-under-windows-mac-osx-and-linux">Launch RStudio under Windows, MAC OSX and Linux</a></li>
<li><a href="#set-your-working-directory">Set your working directory</a><ul>
<li><a href="#change-your-working-directory">Change your working directory</a></li>
<li><a href="#set-a-default-working-directory">Set a default working directory</a></li>
</ul></li>
</ul></li>
<li><a href="#close-your-rrstudio-session">Close your R/RStudio session</a></li>
<li><a href="#related-articles">Related articles</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<p><br/></p>
<p>After <a href="https://www.sthda.com/english/english/wiki/installing-r-and-rstudio-easy-r-programming">installing R and RStudio</a>, the question is now how to start using <strong>R/RStudio</strong>. In this article, we’ll describe how to run <strong>RStudio</strong> and to set up your <strong>working directory</strong>.</p>
<p><span class="warning">Note that, it’s possible to use R outside or inside RStudio. However, we highly recommend to <strong>use R inside RStudio</strong>. RStudio allows users to run R in a more user-friendly environment.</span></p>
<div id="use-r-outside-rstudio" class="section level1">
<h1>Use R outside RStudio</h1>
<div id="under-windows-and-mac-osx" class="section level2">
<h2>Under Windows and MAC OSX</h2>
<p>For the first time you use R, the suggested procedure, under Windows and MAC OSX, is as follow:</p>
<ol style="list-style-type: decimal">
<li><p>Create a sub-directory, say <strong>R</strong>, in your “Documents” folder. This sub-folder, also known as <strong>working directory</strong>, will be used by R to read and save files.</p></li>
<li><p>Launch R by double-clicking on the icon.</p></li>
<li>Specify your working directory to R:
<ul>
<li>On Windows: File –> Change directory</li>
<li>On MAC OSX: Tools –> Change the working directory</li>
</ul></li>
</ol>
</div>
<div id="under-linux" class="section level2">
<h2>Under Linux</h2>
<ol style="list-style-type: decimal">
<li><p>Open the shell prompt</p></li>
<li><p>Create a working directory, named “R”, using “mkdir” command:</p></li>
</ol>
<br/>
<div class="bash">
$ mkdir R
$ cd R
</div>
<p><br/></p>
<ol start="3" style="list-style-type: decimal">
<li>Start the R program with the command “R”:</li>
</ol>
<p><span class="bash">$ R</span></p>
<ol start="4" style="list-style-type: decimal">
<li>To quit R program, use this:</li>
</ol>
<p><span class="bash">$ q()</span></p>
</div>
</div>
<div id="use-r-inside-rstudio" class="section level1">
<h1>Use R inside RStudio</h1>
<p><span class="success">Using R inside RStudio is the recommended choice.</span></p>
<div id="launch-rstudio-under-windows-mac-osx-and-linux" class="section level2">
<h2>Launch RStudio under Windows, MAC OSX and Linux</h2>
<p>After <a href="https://www.sthda.com/english/english/wiki/installing-r-and-rstudio-easy-r-programming">installing R and RStudio</a>, launch RStudio from your computer “application folders”.</p>
<p><strong>RStudio screen</strong></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/rstudio.png" alt="RStudio" /></p>
<p>RStudio is a four pane work-space for 1) creating file containing R script, 2) typing R commands, 3) viewing command histories, 4) viewing plots and more.</p>
<ol style="list-style-type: decimal">
<li><p>Top-left panel: Code editor allowing you to create and open a file containing R script. The R script is where you keep a record of your work. R script can be created as follow: <strong>File –> New –> R Script</strong>.</p></li>
<li><p>Bottom-left panel: R console for typing R commands</p></li>
<li>Top-right panel:
<ul>
<li>Workspace tab: shows the list of R objects you created during your R session</li>
<li>History tab: shows the history of all previous commands</li>
</ul></li>
<li>Bottom-right panel:
<ul>
<li>Files tab: show files in your working directory</li>
<li>Plots tab: show the history of plots you created. From this tab, you can export a plot to a PDF or an image files</li>
<li>Packages tab: show external R packages available on your system. If checked, the package is loaded in R.</li>
</ul></li>
</ol>
<p><span class="success">For more about RStudio read the online <a href="https://support.rstudio.com/hc/en-us/categories/200035113-Documentation">RStudio documentation</a>.</span></p>
</div>
<div id="set-your-working-directory" class="section level2">
<h2>Set your working directory</h2>
<p><span class="success">Recall that, the working directory is a folder where R reads and saves files.</span></p>
<div id="change-your-working-directory" class="section level3">
<h3>Change your working directory</h3>
<p>You can change your working directory as follow:</p>
<br/>
<div class="block">
<ol style="list-style-type: decimal">
<li><p>Create a sub-directory named “R” in your “Documents” folder</p></li>
<li>From RStudio, use the menu to change your working directory under <strong>Session > Set Working Directory > Choose Directory</strong>.
</li>
<li>Choose the directory you’ve just created in step 1</li>
</ol>
</div>
<p><br/></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/rstudio-change-working-directory.png" alt="RStudio change working directory" /></p>
<p><span class="warning">It’s also possible to use the R function <strong>setwd()</strong>, which stands for “set working directory”.</span></p>
<pre class="r"><code>setwd("/path/to/my/directory")</code></pre>
<p>For Windows, the command might look like :</p>
<pre class="r"><code>setwd("c:/Documents/my/working/directory")</code></pre>
<p><span class="warning">Note that, if you want to know your current (or default) R working directory, type the command <strong>getwd()</strong>, which stands for “get working directory”. </span></p>
</div>
<div id="set-a-default-working-directory" class="section level3">
<h3>Set a default working directory</h3>
<p>A default working directory is a folder where RStudio goes, every time you open it. You can change the default working directory from RStudio menu under: <strong>Tools –> Global options –> click on “Browse” to select the default working directory you want.</strong></p>
</div>
</div>
</div>
<div id="close-your-rrstudio-session" class="section level1">
<h1>Close your R/RStudio session</h1>
<p>Each time you close R/RStudio, you will be asked whether you want to save the data from your R session. If you decide to save, the data will be available in future R sessions.</p>
</div>
<div id="related-articles" class="section level1">
<h1>Related articles</h1>
<ul>
<li>Previous chapters
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/what-is-r-and-why-learning-r-programming">What’is R and why learning R?</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/installing-r-and-rstudio-easy-r-programming">Installing R and RStudio</a></li>
</ul></li>
<li>Next chapters
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/easy-r-programming-basics">R programming basics</a></li>
</ul></li>
</ul>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using <strong>R software</strong> (ver. 3.2.3). </span></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->

</html>

<!-- END HTML -->]]></description>
			<pubDate>Tue, 05 Apr 2016 06:25:40 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Installing R and RStudio - Easy R Programming]]></title>
			<link>https://www.sthda.com/english/wiki/installing-r-and-rstudio-easy-r-programming</link>
			<guid>https://www.sthda.com/english/wiki/installing-r-and-rstudio-easy-r-programming</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">


<div id="TOC">
<ul>
<li><a href="#install-r-and-rstudio-on-windows">Install R and RStudio on windows</a><ul>
<li><a href="#install-r-for-windows">Install R for windows</a></li>
<li><a href="#install-rtools-for-windows">Install Rtools for Windows</a></li>
<li><a href="#install-rstudio-on-windows">Install RStudio on Windows</a></li>
</ul></li>
<li><a href="#install-r-and-rstudio-for-mac-osx">Install R and RStudio for MAC OSX</a></li>
<li><a href="#install-r-and-rstudio-on-linux">Install R and RStudio on Linux</a></li>
<li><a href="#further-ressources-for-installing-r-and-rstudio">Further ressources for installing R and RStudio</a></li>
<li><a href="#related-articles">Related articles</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<p><br/></p>
<p>In our previous article, we described <a href="https://www.sthda.com/english/english/wiki/what-is-r-and-why-learning-r-programming">what is R and why you should learn R</a>. In this article, we’ll describe briefly how to <strong>install R</strong> and <strong>RStudio</strong> on Windows, MAC OSX and Linux platforms. <strong>RStudio</strong> is an integrated development environment for R that makes using R easier. It includes a console, code editor and tools for plotting.</p>
<p><span class="error">To make things simple, we recommend to install first R and then RStudio.</span></p>
<br/>
<div class="block">
<ol style="list-style-type: decimal">
<li><p>R can be downloaded and installed on Windows, MAC OSX and Linux platforms from the <a href="http://cran.r-project.org/">Comprehensive R Archive Network</a> (CRAN) webpage (<a href="http://cran.r-project.org/" class="uri">http://cran.r-project.org/</a>).</p></li>
<li>After installing R software, install also the RStudio software available at: <a href="http://www.rstudio.com/products/RStudio/" class="uri">http://www.rstudio.com/products/RStudio/</a>.
</li>
</ol>
</div>
<p><br/></p>
<div id="install-r-and-rstudio-on-windows" class="section level1">
<h1>Install R and RStudio on windows</h1>
<div id="install-r-for-windows" class="section level2">
<h2>Install R for windows</h2>
<ol style="list-style-type: decimal">
<li>Download the latest version of R, for Windows, from CRAN at : <a href="https://cran.r-project.org/bin/windows/base/" class="uri">https://cran.r-project.org/bin/windows/base/</a></li>
</ol>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/windows-download-r.png" alt="Download R for Windows" /></p>
<ol start="2" style="list-style-type: decimal">
<li><p>Double-click on the file you just downloaded to install R</p></li>
<li><p>Cick ok –> Next –> Next –> Next …. (no need to change default installation parameters)</p></li>
</ol>
</div>
<div id="install-rtools-for-windows" class="section level2">
<h2>Install Rtools for Windows</h2>
<p>Rtools contains tools to build your own packages on Windows, or to build R itself.</p>
<ol style="list-style-type: decimal">
<li>Download Rtools version corresponding to your R version at: <a href="https://cran.r-project.org/bin/windows/Rtools/" class="uri">https://cran.r-project.org/bin/windows/Rtools/</a>. Use the latest release of Rtools with the latest release of R.</li>
</ol>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/windows-rtools.png" alt="Download Rtools for windows" /></p>
<ol start="2" style="list-style-type: decimal">
<li>Double-click on the file you just downloaded to install Rtools (no need to change default installation parameters)</li>
</ol>
</div>
<div id="install-rstudio-on-windows" class="section level2">
<h2>Install RStudio on Windows</h2>
<ul>
<li>Download RStudio at : <a href="https://www.rstudio.com/products/rstudio/download/" class="uri">https://www.rstudio.com/products/rstudio/download/</a></li>
</ul>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/windows-rstudio.png" alt="Download RStudio for Windows" /></p>
</div>
</div>
<div id="install-r-and-rstudio-for-mac-osx" class="section level1">
<h1>Install R and RStudio for MAC OSX</h1>
<ol style="list-style-type: decimal">
<li><p>Download the latest version of R, for MAC OSX, from CRAN at : <a href="https://cran.r-project.org/bin/macosx/" class="uri">https://cran.r-project.org/bin/macosx/</a></p></li>
<li><p>Double-click on the file you just downloaded to install R</p></li>
<li><p>Cick ok –> Next –> Next –> Next …. (no need to change default installation parameters)</p></li>
<li><p>Download and install the latest version of RStudio for MAC at: <a href="https://www.rstudio.com/products/rstudio/download/" class="uri">https://www.rstudio.com/products/rstudio/download/</a></p></li>
</ol>
</div>
<div id="install-r-and-rstudio-on-linux" class="section level1">
<h1>Install R and RStudio on Linux</h1>
<ol style="list-style-type: decimal">
<li>R can be installed on Ubuntu, using the following Bash script:</li>
</ol>
<p><span class="bash">
sudo apt-get install r-base </span></p>
<ol start="2" style="list-style-type: decimal">
<li>RStudio for Linux is available at <a href="https://www.rstudio.com/products/rstudio/download/" class="uri">https://www.rstudio.com/products/rstudio/download/</a></li>
</ol>
<p><span class="warning">To install the latest version of R for linux, read this: <a href="http://cran.r-project.org/bin/linux/ubuntu/README">Installing R on Ubuntu</a></span></p>
</div>
<div id="further-ressources-for-installing-r-and-rstudio" class="section level1">
<h1>Further ressources for installing R and RStudio</h1>
<p>It is relatively simple to install R, but if you need further help you can try the following resources:</p>
<ul>
<li><a href="https://github.com/genomicsclass/windows#installing-r">Installing R on Windows</a></li>
<li><a href="https://www.youtube.com/watch?v=Icawuhf0Yqo&amp;feature=youtu.be">Installing R on Mac</a></li>
<li><a href="http://cran.r-project.org/bin/linux/ubuntu/README">Installing R on Ubuntu</a></li>
</ul>
</div>
<div id="related-articles" class="section level1">
<h1>Related articles</h1>
<ul>
<li>Previous chapters
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/what-is-r-and-why-learning-r-programming">What’is R and why learning R?</a></li>
</ul></li>
<li>Next chapters
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming">Running RStudio and setting up your working directory</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/easy-r-programming-basics">R programming basics</a></li>
</ul></li>
</ul>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using <strong>R software</strong> (ver. 3.2.3). </span></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->



<!-- END HTML -->]]></description>
			<pubDate>Tue, 05 Apr 2016 06:22:30 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[What is R and Why Learning R Programming]]></title>
			<link>https://www.sthda.com/english/wiki/what-is-r-and-why-learning-r-programming</link>
			<guid>https://www.sthda.com/english/wiki/what-is-r-and-why-learning-r-programming</guid>
			<description><![CDATA[<!-- START HTML -->

            
  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#what-is-r">What is R?</a></li>
<li><a href="#why-learning-r">Why learning R?</a></li>
<li><a href="#related-articles">Related articles</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<p><br/></p>
<div id="what-is-r" class="section level1">
<h1>What is R?</h1>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-logo.png" alt="R logo" /></p>
<br/>
<div class="block">
<a href="http://www.r-project.org/"><strong>R</strong></a> is free and powerful <strong>programming language</strong> for <strong>statistical</strong> computing and <strong>data visualization</strong>.
</div>
<p><br/></p>
<ol style="list-style-type: decimal">
<li><p><strong>R</strong> can be used to compute a large variety of classical statistic tests including:</p>
<ul>
<li><strong>Student’s t-test</strong> comparing the means of two groups of samples</li>
<li><strong>Wilcoxon test</strong>, a non parametric alternative of <strong>t-test</strong></li>
<li><strong>Analysis of variance</strong> (ANOVA) comparing the means of more than two groups</li>
<li><strong>Chi-square test</strong> comparing proportions/distributions</li>
<li><strong>Correlation analysis</strong> for evaluating the relationship between two or more variables</li>
</ul></li>
<li><p>It’s also possible to use R for performing <strong>classification analysis</strong> such as:</p>
<ul>
<li><strong>Principal component analysis</strong></li>
<li><strong>clustering</strong></li>
</ul></li>
<li><p><strong>Many types of graphs</strong> can be drawn using R, including: box plot, histogram, density curve, scatter plot, line plot, bar plot, …</p></li>
</ol>
</div>
<div id="why-learning-r" class="section level1">
<h1>Why learning R?</h1>
<ul>
<li><p><strong>R</strong> is <strong>open source</strong>, so it’s free.</p></li>
<li><p><strong>R</strong> is <strong>cross-plateform</strong> compatible, so it can be installed on Windows, MAC OSX and Linux</p></li>
<li><p><strong>R</strong> provides a wide variety of <strong>statistical techniques</strong> and <strong>graphical capabilities</strong>.</p></li>
<li><p><strong>R</strong> provides the possibility to make a <strong>reproducible research</strong> by embedding script and results in a single file.</p></li>
<li><p><strong>R</strong> has a <strong>vast community</strong> both in academia and in business</p></li>
<li><p><strong>R</strong> is <strong>highly extensible</strong> and it has thousands of well-documented extensions (named R packages) for a very broad range of applications in the financial sector, health care,…</p></li>
<li><p>It’s <strong>easy to create R packages</strong> for solving particular problems</p></li>
</ul>
</div>
<div id="related-articles" class="section level1">
<h1>Related articles</h1>
<ul>
<li>Next chapters
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/installing-r-and-rstudio-easy-r-programming">Installing R and RStudio</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming">Running RStudio and setting up your working directory</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/easy-r-programming-basics">R programming basics</a></li>
</ul></li>
</ul>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using <strong>R software</strong> (ver. 3.2.3). </span></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->


<!-- END HTML -->]]></description>
			<pubDate>Sun, 03 Apr 2016 11:25:18 +0200</pubDate>
			
		</item>
		
	</channel>
</rss>
