{"id":904,"date":"2018-10-19T00:35:57","date_gmt":"2018-10-19T00:35:57","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=904"},"modified":"2018-10-22T22:47:19","modified_gmt":"2018-10-22T22:47:19","slug":"installing-r-programming-on-ubuntu-18-04","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/19\/installing-r-programming-on-ubuntu-18-04\/","title":{"rendered":"Installing R programming on Ubuntu 18.04"},"content":{"rendered":"<p>In this lesson, we will take a look at how we can install and start using the R programming language on Ubuntu 18.04. R is an excellent open-source graphical and statistical computing programming language and is one of the most used programming languages after\u00a0<a href=\"https:\/\/linuxhint.com\/python\/\" target=\"_blank\" rel=\"noopener\">Python\u00a0<\/a>for Data Science and Machine Learning, used with one of the best tools,\u00a0<a href=\"https:\/\/linuxhint.com\/install-jupyter-notebook-ubuntu\/\" target=\"_blank\" rel=\"noopener\">Jupyter Notebooks.<\/a><\/p>\n<p>We will start by installing the R programming language on Ubuntu 18.04 and continue with a very simple program in this language. Let\u2019s get started.<\/p>\n<h2>Add GPG Keys<\/h2>\n<p>We first need to add the relevant GPG keys:<\/p>\n<p>sudo apt-key adv &#8211;keyserver keyserver.ubuntu.com &#8211;recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9<\/p>\n<p>Here is what we get back with this command:<\/p>\n<p><a href=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/add-gpg-key.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/add-gpg-key.png\" alt=\"Add GPG Keys\" width=\"882\" height=\"152\" \/><\/a><\/p>\n<p>Add GPG Keys<\/p>\n<h3>Add R Repositories<\/h3>\n<p>We can now add R repositories for the R programming language release:<\/p>\n<p>sudo add-apt-repository &#8216;deb https:\/\/cloud.r-project.org\/bin\/linux\/ubuntu bionic-cran35\/&#8217;<\/p>\n<p>Here is what we get back with this command:<\/p>\n<p><a href=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/add-r-repositories.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/add-r-repositories.png\" alt=\"Add R repositories\" width=\"962\" height=\"204\" \/><\/a><\/p>\n<p>Add R repositories<\/p>\n<h3>Update Package List<\/h3>\n<p>Let\u2019s update Ubuntu Package list:<\/p>\n<h3>Install R<\/h3>\n<p>We can finally install R programming language now:<\/p>\n<h3>Verify Installation<\/h3>\n<p>Run the following command to verify your install:<\/p>\n<p>We will see the R console once we write above command:<\/p>\n<p><a href=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/verify-r-installation.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/verify-r-installation.png\" alt=\"Verify R Installation\" width=\"658\" height=\"381\" \/><\/a><\/p>\n<p>Verify R Installation<\/p>\n<h2>Start using R programming with Hello World<\/h2>\n<p>Once we have an active installation for the R programming language on Ubuntu 18.04, we will start using it in a very simple and traditional \u201cHello World\u201d program here. To execute a simple program, we can simply open a terminal, type the following command to open R console:<\/p>\n<p>We can now start writing simple statements in the console now:<\/p>\n<p>&gt; helloLinuxHint &lt;- &#8220;Hello World&#8221;<br \/>\n&gt; print (helloLinuxHint)<\/p>\n<p>Here is what we get back with this command:<\/p>\n<p><a href=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/r-hello-world.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/r-hello-world.png\" alt=\"R Hello World\" width=\"345\" height=\"80\" \/><\/a><\/p>\n<p>R Hello World<\/p>\n<h2>Running R-based scripts<\/h2>\n<p>It is also possible to run R-based scripts using the R command line tool. To do this, make a new file \u2018linuxhint.R\u2019 with the following content:<\/p>\n<p>helloLinuxHint &lt;- &#8220;Hello from the script, World!&#8221;<br \/>\nprint(helloLinuxHint)<\/p>\n<p>Here is what we get back with a command which will run this script:<\/p>\n<p><a href=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/rscript-ubuntu.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/rscript-ubuntu.png\" alt=\"Running R program from Rscript\" width=\"373\" height=\"100\" \/><\/a><\/p>\n<p>Running R program from Rscript<\/p>\n<p>Here is the command we used:<\/p>\n<p>At last, we will demonstrate another simple program to calculate factorial of a number with R. Here is a sample program which shows how to do this:<\/p>\n<p>num = 5<\/p>\n<p>factorial = 1<\/p>\n<p># check if the number is negative, positive or zeroif(num &lt; 0) {<\/p>\n<p>print (&#8220;Sorry, number cannot be negative.&#8221;)<br \/>\n} else if( num == 0) { print (&#8220;The factorial of 0 is 1.&#8221;)<br \/>\n} else { for( i in 1 :number) { factorial = factorial*i}<br \/>\nprint(paste(&#8220;The factorial of&#8221;, num ,&#8221;is:&#8221;,factorial))}<\/p>\n<p>We can run the above script with the following command:<\/p>\n<p>Once we run the provided script, we can see the factorial calculated for a given number:<\/p>\n<p><a href=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/r-factorial.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/r-factorial.png\" alt=\"Calculating factorial of a number\" width=\"341\" height=\"53\" \/><\/a><\/p>\n<p>Calculating factorial of a number<\/p>\n<p>Now, you&#8217;re ready to write your own R programs.<\/p>\n<h2>Python vs R for Data Science<\/h2>\n<p>If you are a beginner, it is difficult to pick <a href=\"https:\/\/linuxhint.com\/python\/\" target=\"_blank\" rel=\"noopener\">Python<\/a> or R over each other for data analysis and visualisation. Both of these languages have a lot of common libraries than you can imagine. Almost each and every task can be done in both of these languages, may it be related to data wrangling, engineering, feature selection web scrapping, app and so on. Some points we can consider for Python include:<\/p>\n<ul>\n<li>Python is a language to deploy and implement machine learning at a large-scale<\/li>\n<li>The code in Python is much more scalable and maintainable<\/li>\n<li>Most of the data science job can be done with five Python libraries: Numpy, Pandas, Scipy, Scikit-learn and Seaborn and they have developed majorly over past few hours and is catching up with the R programming language<\/li>\n<\/ul>\n<p>Some things which make R more useful is the availability of many statistical products which creates excellent output for business use-cases which we will discover in coming posts.<\/p>\n<h2>Conclusion: Installing R on Ubuntu 18.04<\/h2>\n<p>In this lesson, we studied how we can install and start using the R programming language on Ubuntu 18.04 with very simple programs in the language. This is just a very simple introduction of many lessons to come with the R programming language. Share your feedback for the lesson with <a href=\"https:\/\/twitter.com\/sbmaggarwal\" target=\"_blank\" rel=\"noopener\">me<\/a> or to <a href=\"https:\/\/twitter.com\/linuxhint\" target=\"_blank\" rel=\"noopener\">LinuxHint Twitter handle<\/a>.<\/p>\n<p><a href=\"https:\/\/linuxhint.com\/installing-r-ubuntu-1804\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, we will take a look at how we can install and start using the R programming language on Ubuntu 18.04. R is an excellent open-source graphical and statistical computing programming language and is one of the most used programming languages after\u00a0Python\u00a0for Data Science and Machine Learning, used with one of the best &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/19\/installing-r-programming-on-ubuntu-18-04\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Installing R programming on Ubuntu 18.04&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-904","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/904","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=904"}],"version-history":[{"count":2,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/904\/revisions"}],"predecessor-version":[{"id":1189,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/904\/revisions\/1189"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}