{"id":16512,"date":"2020-07-10T16:24:08","date_gmt":"2020-07-10T10:54:08","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/r-tutorial\/"},"modified":"2024-12-16T09:48:44","modified_gmt":"2024-12-16T04:18:44","slug":"r-tutorial","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/r-tutorial\/","title":{"rendered":"R Tutorial"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"introduction-to-r\"><strong>Introduction to R<\/strong><\/h2>\n\n\n\n<p>R is a programming language developed by Ross Ihaka and Robert Gentleman in 1993. R possesses an extensive catalog of statistical and graphical methods. It includes machine learning algorithms, linear regression, time series, and statistical inference, to name a few. Most R libraries are written in R, but C, C++, and Fortran codes are preferred for heavy computational tasks.<br><\/p>\n\n\n\n<p>Data analysis with R is done in a series of steps; programming, transforming, discovering, modeling, and communicating the results<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Program<\/strong>: R is a clear and accessible programming tool<\/li>\n\n\n\n<li><strong>Transform<\/strong>: R is made up of a collection of libraries designed specifically for data science<\/li>\n\n\n\n<li><strong>Discover<\/strong>: Investigate the data, refine your hypothesis and analyze them<\/li>\n\n\n\n<li><strong>Model<\/strong>: R provides a wide array of tools to capture the right model for your data<\/li>\n\n\n\n<li><strong>Communicate<\/strong>: Integrate codes, graphs, and outputs to a report with R Markdown or build Shiny apps to share with the world.<\/li>\n<\/ul>\n\n\n\n<p>Check out the free <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/r-in-data-science\" target=\"_blank\" rel=\"noreferrer noopener\">R in Data Science<\/a> course to learn more about the significant concepts of R from scratch. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"what-is-r-used-for\"><strong>What is R used for?<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Statistical inference<\/li>\n\n\n\n<li>Data analysis<\/li>\n\n\n\n<li>Machine learning algorithm<\/li>\n<\/ul>\n\n\n\n<p>As conclusion, R is the world's most widely used statistics programming language. It's the 1st choice of data scientists and supported by a vibrant and talented community of contributors. R is taught in universities and deployed in mission-critical business applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"r-environment-setup\"><strong>R-environment setup<\/strong><\/h2>\n\n\n\n<p><strong>Windows Installation - <\/strong>We can download the Windows installer version of R from R-3.2.2 for windows (32\/64)<br><\/p>\n\n\n\n<p>As it is a Windows installer (.exe) with the name \"R-version-win.exe\". You can just double click and run the installer accepting the default settings. If your Windows is a 32-bit version, it installs the 32-bit version. But if your windows are 64-bit, then it installs both the 32-bit and 64-bit versions.<\/p>\n\n\n\n<p>After installation, you can locate the icon to run the program in a directory structure \"RR3.2.2bini386Rgui.exe\" under the Windows Program Files. Clicking this icon brings up the R-GUI which is the R console to do R Programming.&nbsp;<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"r-basic-syntax\"><strong>R basic Syntax<\/strong><\/h2>\n\n\n\n<p>R Programming is a popular programming language broadly used in data analysis. The way in which we define its code is quite simple. The \"Hello World!\" is the basic program for all the languages, and now we will understand the syntax of R programming with the \"Hello world\" program. We can write our code either in the command prompt, or we can use an R script file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"r-command-prompt\"><strong>R command prompt<\/strong><\/h3>\n\n\n\n<p>Once you have R environment setup, then it\u2019s easy to start your R command prompt by just typing the following command at your command prompt \u2212<br>$R<br>This will launch R interpreter and you will get a prompt &gt; where you can start typing your program as follows \u2212<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;myString &lt;- \"Hello, World\"\n&gt;print (myString)\n&#91;1] \"Hello, World!\"<\/code><\/pre>\n\n\n\n<div class=\"inherit-container-width wp-block-group is-layout-constrained wp-block-group-is-layout-constrained\"><\/div>\n\n\n\n<p>Here the first statement defines a string variable myString, where we assign a string \"Hello, World!\" and then the next statement print() is being used to print the value stored in myString variable. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"r-data-types\"><strong>R data-types<\/strong><\/h2>\n\n\n\n<p>While doing programming in any programming language, you need to use various variables to store various information. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.<\/p>\n\n\n\n<p>In contrast to other programming languages like C and java in R, the variables are not declared as some data type. The variables are assigned with R-Objects and the data type of the R-object becomes the variable's data type. There are many types of R-objects. The frequently used ones are \u2212<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Vectors<\/li>\n\n\n\n<li>Lists<\/li>\n\n\n\n<li>Matrices<\/li>\n\n\n\n<li>Arrays<\/li>\n\n\n\n<li>Factors<\/li>\n\n\n\n<li>Data Frames<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"vectors\"><strong>Vectors<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#create a vector and find the elements which are &gt;5\nv&lt;-c(1,2,3,4,5,6,5,8)\nv&#91;v&gt;5]\n\n#subset\nsubset(v,v&gt;5)\n\n#position in the vector created in which square of the numbers of v is &gt;10 holds good\nwhich(v*v&gt;10)\n\n#to know the values \nv&#91;v*v&gt;10]<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output: [1] 6 8\n\nOutput: [1] 6 8\n\nOutput: [1] 4 5 6 7 8\n\nOutput: [1] 4 5 6 5 8<\/pre>\n\n\n<figure class=\"wp-block-image size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/June-29-banner-for-GL-Academy-intro-to-R-1.png\"><a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/introduction-to-r\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1000\" height=\"242\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/June-29-banner-for-GL-Academy-intro-to-R-1.png\" alt=\"\" class=\"wp-image-19199\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/June-29-banner-for-GL-Academy-intro-to-R-1.png 1000w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/June-29-banner-for-GL-Academy-intro-to-R-1-300x73.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/June-29-banner-for-GL-Academy-intro-to-R-1-768x186.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/June-29-banner-for-GL-Academy-intro-to-R-1-696x168.png 696w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"matrices\"><strong>Matrices<\/strong><\/h3>\n\n\n\n<p>A matrix is a two-dimensional rectangular data set. It can be created using a vector input to the matrix function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#matrices: a vector with two dimensional attributes\nmat&lt;-matrix(c(1,2,3,4))\n \nmat1&lt;-matrix(c(1,2,3,4),nrow=2)\nmat1<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:     [,1] [,2]\n [1,]    1    3\n [2,]    2    4<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>mat2&lt;-matrix(c(1,2,3,4),ncol=2,byrow=T)\nmat2<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:       [,1] [,2]\n [1,]    1    2\n [2,]    3    4<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>mat3&lt;-matrix(c(1,2,3,4),byrow=T)\nmat3\n\n#transpose of matrix\nmattrans&lt;-t(mat)\nmattrans\n\n#create a character matrix called fruits with elements apple, orange, pear, grapes\nfruits&lt;-matrix(c(\"apple\",\"orange\",\"pear\",\"grapes\"),2)\n#create 3\u00d74 matrix of marks obtained in each quarterly exams for 4 different subjects \nX&lt;-matrix(c(50,70,40,90,60, 80,50, 90,100, 50,30, 70),nrow=3)\nX\n\n#give row names and column names\nrownames(X)&lt;-paste(prefix=\"Test.\",1:3)\nsubs&lt;-c(\"Maths\", \"English\", \"Science\", \"History\")\ncolnames(X)&lt;-subs\nX<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:       [,1]  [1,]    1  [2,]    2  [3,]    3  [4,]    4 Output:      [,1] [,2] [,3] [,4]  [1,]    1    2    3    4 Output:      [,1] [,2] [,3] [,4]  [1,]   50   90   50   50  [2,]   70   60   90   30  [3,]   40   80  100   70 Output:   Maths English Science History  Test. 1    50      90      50      50  Test. 2    70      60      90      30  Test. 3    40      80     100      70<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"arrays\"><strong>Arrays<\/strong><\/h3>\n\n\n\n<p>While matrices are confined to two dimensions, arrays can be of any number of dimensions. The array function takes a dim attribute, creating the required dimensions. In the below example, we create an array with two elements which are 3x3 matrices each.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Arrays\narr&lt;-array(1:24,dim=c(3,4,2))\narr\n\n#create an array using alphabets with dimensions 3 rows, 2 columns and 3 arrays\narr1&lt;-array(letters&#91;1:18],dim=c(3,2,3))\n\n#select only 1st two matrix of an array\narr1&#91;,,c(1:2)]\n\n#LIST\nX&lt;-list(u=2, n='abc')\nX\nX$u<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output: , , 1\n <code> [,1] [,2] [,3] [,4]<\/code>\n [1,]    1    4    7   10\n [2,]    2    5    8   11\n [3,]    3    6    9   12\n , , 2\n <code> [,1] [,2] [,3] [,4]<\/code>\n [1,]   13   16   19   22\n [2,]   14   17   20   23\n [3,]   15   18   21   24\n\nOutput: \n , , 1\n <code> [,1] [,2]<\/code>\n [1,] \"a\"  \"d\" \n [2,] \"b\"  \"e\" \n [3,] \"c\"  \"f\" \n , , 2\n <code> [,1] [,2]<\/code>\n [1,] \"g\"  \"j\" \n [2,] \"h\"  \"k\" \n [3,] \"i\"  \"l\" \n arr1[,,3]\n Output:       [,1] [,2]\n [1,] \"m\"  \"p\" \n [2,] \"n\"  \"q\" \n [3,] \"o\"  \"r\" \n\nOutput:\n  $u\n [1] 2\n $n\n [1] \"abc\"\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"dataframes\"><strong>Dataframes<\/strong><\/h3>\n\n\n\n<p>Data frames are tabular data objects. Unlike a matrix in a data frame, each column can contain different modes of data. The first column can be numeric while the second column can be character and the third column can be logical. It is a list of vectors of equal length.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Dataframes\nstudents&lt;-c(\"J\",\"L\",\"M\",\"K\",\"I\",\"F\",\"R\",\"S\")\nSubjects&lt;-rep(c(\"science\",\"maths\"),each=2)\nmarks&lt;-c(55,70,66,85,88,90,56,78)\ndata&lt;-data.frame(students,Subjects,marks)\n#Accessing dataframes\ndata&#91;&#91;1]]\n\ndata$Subjects\ndata&#91;,1]<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output: \n [1] J L M K I F R S\n Levels: F I J K L M R S\n\nOutput: \n   data$Subjects\n   [1] science science maths   maths   science science maths   maths  \n   Levels: maths science <\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"factors\"><strong>Factors<\/strong><\/h3>\n\n\n\n<p>Factors are the r-objects that are created using a vector. It stores the vector along with the distinct values of the elements in the vector as labels. The labels are always characters irrespective of whether it is numeric or character or Boolean etc. in the input vector. They are useful in statistical modeling.<\/p>\n\n\n\n<p>Factors are created using the factor() function. The nlevels function gives the count of levels.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Factors\nx&lt;-c(1,2,3)\nfactor(x)\n\n#apply function\ndata1&lt;-data.frame(age=c(55,34,42,66,77),bmi=c(26,25,21,30,22))\nd&lt;-apply(data1,2,mean)\nd\n\n#create two vectors age and gender and find mean age with respect to gender\nage&lt;-c(33,34,55,54)\ngender&lt;-factor(c(\"m\",\"f\",\"m\",\"f\"))\ntapply(age,gender,mean)\n\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output: [1] 1 2 3\n Levels: 1 2 3\n\nOutput:  age  bmi \n 54.8 24.8 \n\nOutput:  f  m \n         44 44\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"r-variables\"><strong>R Variables<\/strong><\/h2>\n\n\n\n<p>A variable provides us with named storage that our programs can manipulate. A variable in R can store an atomic vector, a group of atomic vectors, or a combination of many R objects. A valid variable name consists of letters, numbers, and the dot or underlined characters.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"rules-for-writing-identifiers-in-r\"><strong>Rules for writing Identifiers in R<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Identifiers can be a combination of letters, digits, period (.), and underscore (_).<\/li>\n\n\n\n<li>It must start with a letter or a period. If it starts with a period, it cannot be followed by a digit.<\/li>\n\n\n\n<li>Reserved words in R cannot be used as identifiers.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"valid-identifiers-in-r\"><strong>Valid identifiers in R<\/strong><\/h4>\n\n\n\n<p>total, sum, .fine.with.dot, this_is_acceptable, Number5<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"invalid-identifiers-in-r\"><strong>Invalid identifiers in R<\/strong><\/h4>\n\n\n\n<p>tot@l, 5um, _fine, TRUE, .0ne<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"best-practices\"><strong>Best Practices<\/strong><\/h3>\n\n\n\n<p>Earlier versions of R used underscore (_) as an assignment operator. So, the period (.) was used extensively in variable names having multiple words. Current versions of R support underscore as a valid identifier, but it is good practice to use a period as word separator.<br>For example, <em>a.variable.name<\/em> is preferred over <em>a_variable_name<\/em> or alternatively, we could use camel case as <em>aVariableName<\/em>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"constants-in-r\"><strong>Constants in R<\/strong><\/h3>\n\n\n\n<p>As the name suggests, constants are entities whose value cannot be altered. The basic types of constants are numeric constants and character constants.<\/p>\n\n\n\n<p><strong>Numeric Constants<\/strong><\/p>\n\n\n\n<p>All numbers fall under this category. They can be of type <em>integer, double or complex. <\/em>It can be checked with the <em>typeof() <\/em>function.<br>Numeric Constants followed by L are regarded as integers and those followed by <em>i<\/em> are regarded as complex.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; typeof(5)\n&gt; typeof(5L)\n&gt; typeof(5L)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">[1] \u201cdouble\u201d\n[1] \u201cdouble\u201d\n[[1] \u201cdouble\u201d<\/pre>\n\n\n\n<p><strong>Character Constants<\/strong><\/p>\n\n\n\n<p>Character constants can be represented using either single quotes (') or double quotes (\") as delimiters.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; 'example'\n&gt; typeof(\"5\")<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">[1] \"example\"\n[1] \"character\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"r-operators\"><strong>R Operators<\/strong><\/h2>\n\n\n\n<p>Operators \u2013 Arithmetic, Relational, Logical, Assignment, and some of the Miscellaneous Operators that R programming language provides.&nbsp;<\/p>\n\n\n\n<p>There are four main categories of Operators in the R programming language.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Arithmetic Operators<\/li>\n\n\n\n<li>Relational Operators<\/li>\n\n\n\n<li>Logical Operators<\/li>\n\n\n\n<li>Assignment Operators<\/li>\n\n\n\n<li>Mixed Operators<\/li>\n<\/ol>\n\n\n<figure class=\"wp-block-image size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/R-operators.png\"><img decoding=\"async\" width=\"500\" height=\"339\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/R-operators.png\" alt=\"\" class=\"wp-image-16596\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/R-operators.png 500w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/R-operators-300x203.png 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>x &lt;- 35\ny&lt;-10<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">   x+y       &gt; x-y     &gt; x*y       &gt; x\/y      &gt; x%\/%y     &gt; x%%y   &gt; x^y\n   [1] 45      [1] 25    [1] 350    [1] 3.5      [1] 3      [1] 5 [1]2.75e+15 <\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"logical-operators\"><strong>Logical Operators<\/strong><\/h3>\n\n\n\n<p> The below table shows the logical operators in R. Operators &amp; and | perform element-wise operation producing result having a length of the longer operand. But &amp;&amp; and || examines only the first element of the operands resulting in a single length logical vector. <\/p>\n\n\n<figure class=\"wp-block-image size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Logical-Operators.png\"><img decoding=\"async\" width=\"503\" height=\"261\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Logical-Operators.png\" alt=\"\" class=\"wp-image-16597\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Logical-Operators.png 503w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Logical-Operators-300x156.png 300w\" sizes=\"(max-width: 503px) 100vw, 503px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>a &lt;- c(TRUE,TRUE,FALSE,0,6,7)\nb &lt;- c(FALSE,TRUE,FALSE,TRUE,TRUE,TRUE)\na&amp;b \n&#91;1] FALSE TRUE FALSE FALSE TRUE TRUE\na&amp;&amp;b\n&#91;1] FALSE\n&gt; a|b\n&#91;1] TRUE TRUE FALSE TRUE TRUE TRUE\n&gt; a||b\n&#91;1] TRUE\n&gt; !a\n&#91;1] FALSE FALSE TRUE TRUE FALSE FALSE\n&gt; !b\n&#91;1] TRUE FALSE TRUE FALSE FALSE FALSE<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n<figure class=\"wp-block-image size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/June-29-banner-for-GL-Academy-intro-to-R-1.png\"><a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/introduction-to-r\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1000\" height=\"242\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/June-29-banner-for-GL-Academy-intro-to-R-1.png\" alt=\"\" class=\"wp-image-19199\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/June-29-banner-for-GL-Academy-intro-to-R-1.png 1000w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/June-29-banner-for-GL-Academy-intro-to-R-1-300x73.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/June-29-banner-for-GL-Academy-intro-to-R-1-768x186.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/June-29-banner-for-GL-Academy-intro-to-R-1-696x168.png 696w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"r-functions\"><strong>R functions<\/strong><\/h2>\n\n\n\n<p>Functions are defined using the function() directive and stored as R objects like anything else. In particular, they are R objects of class \u201cfunction\u201d. Here\u2019s a simple function that takes no arguments and simply prints \u2018Hi statistics\u2019.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define the function\nf &lt;- function() {\nprint(\"Hi statistics!!!\")\n}\n#Call the function\nf()<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n [1] \"Hi statistics!!!\"<\/pre>\n\n\n\n<p>Now let\u2019s define a function called standardize, and the function has a single argument x which is used in the body of a function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Define the function that will calculate standardized score.\nstandardize = function(x) {\nm = mean(x)\nsd = sd(x)\nresult = (x \u2013 m) \/ sd\nresult\n}\ninput&lt;- c(40:50) #Take input for what we want to calculate a standardized score.\nstandardize(input) #Call the function<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output: \n   standardize(input) #Call the function\n   [1] -1.5075567 -1.2060454 -0.9045340 -0.6030227 -0.3015113 0.0000000 0.3015113 0.6030227 0.9045340 1.2060454 1.5075567 <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"loop-functions\"><strong>Loop Functions<\/strong><\/h2>\n\n\n\n<p>R has some very useful functions which implement looping in a compact form to make life easier. The very rich and powerful family of applied functions is made of intrinsically vectorized functions. These functions in R allow you to apply some function to a series of objects (eg. vectors, matrices, data frames, or files). They include:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>lapply(): Loop over a list and evaluate a function on each element<\/li>\n\n\n\n<li>sapply(): Same as lapply() but try to simplify the result<\/li>\n\n\n\n<li>apply(): Apply a function over the margins of an array<\/li>\n\n\n\n<li>tapply(): Apply a function over subsets of a vector<\/li>\n\n\n\n<li>mapply(): Multivariate version of lapply<\/li>\n<\/ol>\n\n\n\n<p>There is another function called split(), which is also useful, particularly in conjunction with lapply.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"r-vectors\"><strong>R Vectors<\/strong><\/h2>\n\n\n\n<p>A vector is a sequence of data elements of the same basic type. Members in a vector are officially called components. Vectors are the most basic R data objects, and there are six types of atomic vectors. They are logical, integer, double, complex, character, and raw.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The c() function can be used to create vectors of objects by concatenating things together. \nx &lt;- c(1,2,3,4,5) #double\nx #If you use only x auto-printing occurs\nl &lt;- c(TRUE, FALSE) #logical\nl &lt;- c(T, F) ## logical\nc &lt;- c(\"a\", \"b\", \"c\", \"d\") ## character\ni &lt;- 1:20 ## integer\ncm &lt;- c(2+2i, 3+3i) ## complex\nprint(l)\nprint(c)\nprint(i)\nprint(cm)\n\nYou can see the type of each vector using typeof() function in R.\ntypeof(x)\ntypeof(l)\ntypeof(c)\ntypeof(i)\ntypeof(cm)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n print(l)\n [1] TRUE FALSE\n   print(c)\n   [1] \"a\" \"b\" \"c\" \"d\"\n   print(i)\n   [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20\n   print(cm)\n   [1] 2+2i 3+3i \n\nOutput: \n typeof(x)\n [1] \"double\"\n   typeof(l)\n   [1] \"logical\"\n   typeof(c)\n   [1] \"character\"\n   typeof(i)\n   [1] \"integer\"\n   typeof(cm)\n   [1] \"complex\" <\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"creating-a-vector-using-seq-function\"><strong>Creating a vector using seq() function:<\/strong><\/h3>\n\n\n\n<p>We can use the seq() function to create a vector within an interval by specifying step size or specifying the length of the vector.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>seq(1:10) #By default it will be incremented by 1\nseq(1, 20, length.out=5) # specify length of the vector\nseq(1, 20, by=2) # specify step size<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n &gt; seq(1:10) #By default it will be incremented by 1\n [1] 1 2 3 4 5 6 7 8 9 10\n &gt; seq(1, 20, length.out=5) # specify length of the vector\n [1] 1.00 5.75 10.50 15.25 20.00\n &gt; seq(1, 20, by=2) # specify step size\n [1] 1 3 5 7 9 11 13 15 17 19<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"extract-elements-from-a-vector\"><strong>Extract Elements from a Vector:<\/strong><\/h3>\n\n\n\n<p>Elements of a vector can be accessed using indexing. The vector indexing can be logical, integer, or character. The [ ] brackets are used for indexing. Indexing starts with position 1, unlike most programming languages, where indexing starts from 0.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"extract-using-integer-as-index\"><strong>Extract Using Integer as Index:<\/strong><\/h3>\n\n\n\n<p>We can use integers as an index to access specific elements. We can also use negative integers to return all elements except that specific element.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x&lt;- 101:110\nx&#91;1]   #access the first element\nx&#91;c(2,3,4,5)] #Extract 2nd, 3rd, 4th, and 5th elements\nx&#91;5:10]        #Extract all elements from 5th to 10th\nx&#91;c(-5,-10)] #Extract all elements except 5th and 10th\nx&#91;-c(5:10)] #Extract all elements except from 5th to 10th \n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n   x[1] #Extract the first element\n   [1] 101\n   x[c(2,3,4,5)] #Extract 2nd, 3rd, 4th, and 5th elements\n   [1] 102 103 104 105\n   x[5:10] #Extract all elements from 5th to 10th\n   [1] 105 106 107 108 109 110\n   x[c(-5,-10)] #Extract all elements except 5th and 10th\n   [1] 101 102 103 104 106 107 108 109\n   x[-c(5:10)] #Extract all elements except from 5th to 10th\n   [1] 101 102 103 104 <\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"extract-using-logical-vector-as-index\"><strong>Extract Using Logical Vector as Index:<\/strong><\/h3>\n\n\n\n<p> If you use a logical vector for indexing, the position where the logical vector is TRUE will be returned.  <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x&#91;x &lt; 105]\nx&#91;x&gt;=104]<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output: \n   x[x &lt; 105] [1] 101 102 103 104 \nx[x&gt;=104]\n   [1] 104 105 106 107 108 109 110 <\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"modify-a-vector-in-r\"><strong>Modify a Vector in R:<\/strong><\/h3>\n\n\n\n<p>We can modify a vector and assign a new value to it. You can truncate a vector by using reassignments. Check the below example.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x&lt;- 10:12\nx&#91;1]&lt;- 101 #Modify the first element\nx\nx&#91;2]&lt;-102 #Modify the 2nd element\nx\nx&lt;- x&#91;1:2] #Truncate the last element\nx \n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output: \n   x\n   [1] 101 11 12\n   x[2]&lt;-102 #Modify the 2nd element\n   x\n   [1] 101 102 12\n   x&lt;- x[1:2] #Truncate the last element\n   x\n   [1] 101 102 <\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"arithmetic-operations-on-vectors\"><strong>Arithmetic Operations on Vectors:<\/strong><\/h3>\n\n\n\n<p>We can use arithmetic operations on two vectors of the same length. They can be added, subtracted, multiplied, or divided. Check the output of the below code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Create two vectors.\nv1 &lt;- c(1:10)\nv2 &lt;- c(101:110)\n\n# Vector addition.\nadd.result &lt;- v1+v2\nprint(add.result)\n# Vector subtraction.\nsub.result &lt;- v2-v1\nprint(sub.result)\n# Vector multiplication.\nmulti.result &lt;- v1*v2\nprint(multi.result)\n# Vector division.\ndivi.result &lt;- v2\/v1\nprint(divi.result)\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n   print(add.result)\n   [1] 102 104 106 108 110 112 114 116 118 120\n   print(sub.result)\n   [1] 100 100 100 100 100 100 100 100 100 100\n   print(multi.result)\n   [1] 101 204 309 416 525 636 749 864 981 1100\n   print(divi.result)\n   [1] 101.00000 51.00000 34.33333 26.00000 21.00000 17.66667 15.28571 13.50000 12.11111 11.00000 <\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"find-minimum-and-maximum-in-a-vector\"><strong>Find Minimum and Maximum in a Vector:<\/strong><\/h3>\n\n\n\n<p>A vector's minimum and maximum can be found using the min() or the max() function. range() is also available, which returns the minimum and maximum in a vector.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x&lt;- 1001:1010\nmax(x) # Find the maximum\nmin(x) # Find the minimum\nrange(x) #Find the range<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output: \n   max(x) # Find the maximum\n   [1] 1010\n   min(x) # Find the minimum\n   [1] 1001\n   range(x) #Find the range\n   [1] 1001 1010 <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"r-lists\"><strong>R Lists<\/strong><\/h2>\n\n\n\n<p>The list is a data structure having elements of mixed data types. A vector having all elements of the same type is called an atomic vector but a vector having elements of a different type is called list.<br>We can check the type with typeof() or class() function and find the length using length()function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x &lt;- list(\"stat\",5.1, TRUE, 1 + 4i)\nx\nclass(x)\ntypeof(x)\nlength(x)\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n   x\n   [[1]]\n   [1] \"stat\"\n   [[2]]\n   [1] 5.1\n   [[3]]\n   [1] TRUE\n   [[4]]\n   [1] 1+4i\n   class(x)\n   [1] \u201clist\u201d\n   typeof(x)\n   [1] \u201clist\u201d\n   length(x)\n   [1] 4 <\/pre>\n\n\n\n<p> You can create an empty list of a prespecified length with the vector() function. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x &lt;- vector(\"list\", length = 10)\nx<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n   x\n   [[1]]\n   NULL\n   [[2]]\n   NULL\n   [[3]]\n   NULL\n   [[4]]\n   NULL\n   [[5]]\n   NULL\n   [[6]]\n   NULL\n   [[7]]\n   NULL\n   [[8]]\n   NULL\n   [[9]]\n   NULL\n   [[10]]\n   NULL <\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"how-to-extract-elements-from-a-list\"><strong>How to extract elements from a list?<\/strong><\/h3>\n\n\n\n<p>Lists can be subsets using two syntaxes, the $ operator and square brackets []. The $ operator returns a named element of a list. The [] syntax returns a list, while the [[]] returns an element of a list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># subsetting\nl$e\nl&#91;\"e\"]\nl&#91;1:2]\nl&#91;c(1:2)] #index using integer vector\nl&#91;-c(3:length(l))] #negative index to exclude elements from 3rd up to last.\nl&#91;c(T,F,F,F,F)] # logical index to access elements<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n &gt; l$e\n [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n [1,] 1 0 0 0 0 0 0 0 0 0\n [2,] 0 1 0 0 0 0 0 0 0 0\n [3,] 0 0 1 0 0 0 0 0 0 0\n [4,] 0 0 0 1 0 0 0 0 0 0\n [5,] 0 0 0 0 1 0 0 0 0 0\n [6,] 0 0 0 0 0 1 0 0 0 0\n [7,] 0 0 0 0 0 0 1 0 0 0\n [8,] 0 0 0 0 0 0 0 1 0 0\n [9,] 0 0 0 0 0 0 0 0 1 0\n [10,] 0 0 0 0 0 0 0 0 0 1\n &gt; l[\"e\"]\n $e\n [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n [1,] 1 0 0 0 0 0 0 0 0 0\n [2,] 0 1 0 0 0 0 0 0 0 0\n [3,] 0 0 1 0 0 0 0 0 0 0\n [4,] 0 0 0 1 0 0 0 0 0 0\n [5,] 0 0 0 0 1 0 0 0 0 0\n [6,] 0 0 0 0 0 1 0 0 0 0\n [7,] 0 0 0 0 0 0 1 0 0 0\n [8,] 0 0 0 0 0 0 0 1 0 0\n [9,] 0 0 0 0 0 0 0 0 1 0\n [10,] 0 0 0 0 0 0 0 0 0 1\n &gt; l[1:2]\n [[1]]\n [1] 1 2 3 4\n [[2]]\n [1] FALSE\n &gt; l[c(1:2)] #index using integer vector\n [[1]]\n [1] 1 2 3 4\n [[2]]\n [1] FALSE\n &gt; l[-c(3:length(l))] #negative index to exclude elements from 3rd up to last.\n [[1]]\n [1] 1 2 3 4\n [[2]]\n [1] FALSE\n l[c(T,F,F,F,F)]\n [[1]]\n [1] 1 2 3 4<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"modifying-a-list-in-r\"><strong>Modifying a List in R:<\/strong><\/h3>\n\n\n\n<p>We can change components of a list through reassignment.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>l&#91;&#91;\"name\"]] &lt;- \"Kalyan Nandi\"\nl<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n [[1]]\n [1] 1 2 3 4\n [[2]]\n [1] FALSE\n [[3]]\n [1] \u201cHello Statistics!\u201d\n $d\n function (arg = 42)\n {\n print(\u201cHello World!\u201d)\n }\n $name\n [1] \u201cKalyan Nandi\u201d<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"r-matrices\"><strong>R Matrices<\/strong><\/h2>\n\n\n\n<p>In R Programming Matrix is a two-dimensional data structure. They contain elements of the same atomic types. A Matrix can be created using the matrix() function. R can also be used for matrix calculations. Matrices have rows and columns containing a single data type. In a matrix, the order of rows and columns is important. Dimension can be checked directly with the dim() function, and all attributes of an object can be checked with the attributes() function. Check the below example.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Creating a matrix in R\n\nm &lt;- matrix(nrow = 2, ncol = 3)\ndim(m)\nattributes(m)\nm &lt;- matrix(1:20, nrow = 4, ncol = 5)\nm<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n   dim(m)\n   [1] 2 3\n   attributes(m)\n   $dim\n   [1] 2 3\n   m &lt;- matrix(1:20, nrow = 4, ncol = 5)\n   m\n   [,1] [,2] [,3] [,4] [,5]\n   [1,] 1 5 9 13 17\n   [2,] 2 6 10 14 18\n   [3,] 3 7 11 15 19\n   [4,] 4 8 12 16 20 <\/pre>\n\n\n\n<p>Matrices can be created by column-binding or row-binding with the cbind() and rbind() functions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x&lt;-1:3\ny&lt;-10:12\nz&lt;-30:32\ncbind(x,y,z)\nrbind(x,y,z)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n   cbind(x,y,z)\n   x y z\n   [1,] 1 10 30\n   [2,] 2 11 31\n   [3,] 3 12 32\n   rbind(x,y,z)\n   [,1] [,2] [,3]\n   x 1 2 3\n   y 10 11 12\n   z 30 31 32 <\/pre>\n\n\n\n<p>By default, the matrix function reorders a vector into columns, but we can also tell R to use rows instead.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x &lt;-1:9\nmatrix(x, nrow = 3, ncol = 3)\nmatrix(x, nrow = 3, ncol = 3, byrow = TRUE)\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output\n   cbind(x,y,z)\n   x y z\n   [1,] 1 10 30\n   [2,] 2 11 31\n   [3,] 3 12 32\n   rbind(x,y,z)\n   [,1] [,2] [,3]\n   x 1 2 3\n   y 10 11 12\n   z 30 31 32 <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"r-arrays\"><strong>R Arrays<\/strong><\/h2>\n\n\n\n<p>In R, Arrays are the data types that can store data in more than two dimensions. An array can be created using the array() function. It takes vectors as input and uses the values in the dim parameter to create an array. If you create an array of dimensions (2, 3, 4) then it creates 4 rectangular matrices each with 2 rows and 3 columns. Arrays can store only data types.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"give-a-name-to-columns-and-rows\"><strong>Give a Name to Columns and Rows:<\/strong><\/h3>\n\n\n\n<p>We can give names to the array's rows, columns, and matrices by setting the dimnames parameter.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>v1 &lt;- c(1,2,3)\nv2 &lt;- 100:110\ncol.names &lt;- c(\"Col1\",\"Col2\",\"Col3\",\"Col4\",\"Col5\",\"Col6\",\"Col7\")\nrow.names &lt;- c(\"Row1\",\"Row2\")\nmatrix.names &lt;- c(\"Matrix1\",\"Matrix2\")\narr4 &lt;- array(c(v1,v2), dim=c(2,7,2), dimnames = list(row.names,col.names, matrix.names))\narr4\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n , , Matrix1\n Col1 Col2 Col3 Col4 Col5 Col6 Col7\n Row1 1 3 101 103 105 107 109\n Row2 2 100 102 104 106 108 110\n , , Matrix2\n Col1 Col2 Col3 Col4 Col5 Col6 Col7\n Row1 1 3 101 103 105 107 109\n Row2 2 100 102 104 106 108 110<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"accessing-extracting-array-elements\"><strong>Accessing\/Extracting Array Elements:<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># Print the 2nd row of the 1st matrix of the array.\nprint(arr4&#91;2,,1])\n# Print the element in the 2nd row and 4th column of the 2nd matrix.\nprint(arr4&#91;2,4,2])\n# Print the 2nd Matrix.\nprint(arr4&#91;,,2])<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n &gt; print(arr4[2,,1])\n Col1 Col2 Col3 Col4 Col5 Col6 Col7\n 2 100 102 104 106 108 110\n &gt;\n &gt; # Print the element in the 2nd row and 4th column of the 2nd matrix.\n &gt; print(arr4[2,4,2])\n [1] 104\n &gt;\n &gt; # Print the 2nd Matrix.\n &gt; print(arr4[,,2])\n Col1 Col2 Col3 Col4 Col5 Col6 Col7\n Row1 1 3 101 103 105 107 109\n Row2 2 100 102 104 106 108 110<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"r-factors\"><strong>R Factors<\/strong><\/h2>\n\n\n\n<p>Factors are used to represent categorical data and can be unordered or ordered. An example might be \u201cMale\u201d and \u201cFemale\u201d if we consider gender. Factor objects can be created with the factor() function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x &lt;- factor(c(\"male\", \"female\", \"male\", \"male\", \"female\"))\nx\ntable(x)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n   x\n   [1] male female male male female\n   Levels: female male\n   table(x)\n   x\n   female male\n     2      3 <\/pre>\n\n\n\n<p> By default, Levels are put in alphabetical order. If you print the above code, you will get levels as female and male. But if you want to get your levels in a particular order, then set levels parameters like this. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x &lt;- factor(c(\"male\", \"female\", \"male\", \"male\", \"female\"), levels=c(\"male\", \"female\"))\nx\ntable(x)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n   x\n   [1] male female male male female\n   Levels: male female\n   table(x)\n   x\n   male female\n    3      2 <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"r-dataframes\"><strong>R Dataframes<\/strong><\/h2>\n\n\n\n<p>Data frames are used to store tabular data in R. They are an important type of object in R and are used in a variety of statistical modeling applications. Data frames are represented as a special list type where every list element has to have the same length. Each element of the list can be thought of as a column; the length of each element is the number of rows. Unlike matrices, data frames can store different classes of objects in each column. Matrices must have every element be the same class (e.g., all integers or all numeric).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"creating-a-data-frame\"><strong>Creating a Data Frame:<\/strong><\/h3>\n\n\n\n<p>Data frames can be created explicitly with the data.frame() function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>employee &lt;- c('Ram','Sham','Jadu')\nsalary &lt;- c(21000, 23400, 26800)\nstartdate &lt;- as.Date(c('2016-11-1','2015-3-25','2017-3-14'))\nemploy_data &lt;- data.frame(employee, salary, startdate)\nemploy_data\nView(employ_data)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n employ_data\n employee salary startdate\n 1 Ram 21000 2016-11-01\n 2 Sham 23400 2015-03-25\n 3 Jadu 26800 2017-03-14\n   View(employ_data) <\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"get-the-structure-of-the-data-frame\"><strong>Get the Structure of the Data Frame:<\/strong><\/h3>\n\n\n\n<p>If you look at the structure of the data frame now, you see that the variable employee is a character vector, as shown in the following output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>str(employ_data)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n &gt; str(employ_data)\n 'data.frame': 3 obs. of 3 variables:\n $ employee : Factor w\/ 3 levels \"Jadu\",\"Ram\",\"Sham\": 2 3 1\n $ salary : num 21000 23400 26800\n $ startdate: Date, format: \"2016-11-01\" \"2015-03-25\" \"2017-03-14\"<\/pre>\n\n\n\n<p>Note that the first column, employee, is of type factor, instead of a character vector. By default, data.frame() function converts character vector into factor. To suppress this behavior, we can pass the argument stringsAsFactors=FALSE.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>employ_data &lt;- data.frame(employee, salary, startdate, stringsAsFactors = FALSE)\nstr(employ_data)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n 'data.frame': 3 obs. of 3 variables:\n $ employee : chr \"Ram\" \"Sham\" \"Jadu\"\n $ salary : num 21000 23400 26800\n $ startdate: Date, format: \"2016-11-01\" \"2015-03-25\" \"2017-03-14\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"r-packages\"><strong>R Packages<\/strong><\/h2>\n\n\n\n<p>The primary location for obtaining R packages is CRAN.<\/p>\n\n\n\n<p>You can obtain information about the available packages on CRAN with the <em>available.packages()<\/em> function.<br>a &lt;- available.packages()<\/p>\n\n\n\n<p>head(rownames(a), 30) # Show the names of the first 30 packages<br>Packages can be installed with the <em>install.packages()<\/em> function in R.&nbsp; To install a single package, pass the name of the lecture to the <em>install.packages()<\/em> function as the first argument.<br>The following code installs the <strong>ggplot2<\/strong> package from CRAN.<br>install.packages(\"ggplot2\")<br>You can install multiple R packages at once with a single call to <em>install.packages(). <\/em>Place the names of the R packages in a character vector.<br>install.packages(c(\"caret\", \"ggplot2\", \"dplyr\"))<br><\/p>\n\n\n\n<p><strong>Loading packages<\/strong><br>Installing a package does not make it immediately available to you in R; you must load the package. The library() function is used to load packages into R. The following code is used to load the ggplot2 package into R. Do not put the package name in quotes.<br>library(ggplot2)<br>If you have Installed your packages without root access using the command <em>install.packages(\u201cggplot2\u2033, lib=\u201d\/data\/Rpackages\/\u201d)<\/em>. Then to load use the below command.<br>library(ggplot2, lib.loc=\"\/data\/Rpackages\/\")<br>After loading a package, the functions exported by that package will be attached to the top of the search() list (after the workspace).<br>library(ggplot2)<\/p>\n\n\n\n<p>search()<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"r-csv-files\"><strong>R - CSV() files <\/strong><\/h2>\n\n\n\n<p>In R, we can read data from files stored outside the R environment. We can also write data into files that will be stored and accessed by the operating system. R can read and write into various file formats like CSV, Excel, XML, etc. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"getting-and-setting-the-working-directory\"><strong>Getting and Setting the Working Directory<\/strong><\/h3>\n\n\n\n<p>We can check which directory the R workspace is pointing to using the&nbsp;<strong>getwd()<\/strong>&nbsp;function. You can also set a new working directory using&nbsp;<strong>setwd()<\/strong>function. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Get and print current working directory.\nprint(getwd())\n\n# Set current working directory.\nsetwd(\"\/web\/com\")\n\n# Get and print current working directory.\nprint(getwd())<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n [1] \"\/web\/com\/1441086124_2016\"\n [1] \"\/web\/com\"<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"input-as-csv-file\"><strong>Input as CSV File<\/strong><\/h3>\n\n\n\n<p>The CSV file is a text file in which the values in the columns are separated by a comma. Let's consider the following data present in the file named&nbsp;<strong>input.csv<\/strong>.<\/p>\n\n\n\n<p>You can create this file using windows notepad by copying and pasting this data. Save the file as&nbsp;<strong>input.csv<\/strong>&nbsp;using the save As All files(*.*) option in notepad.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"reading-a-csv-file\"><strong>Reading a CSV File<\/strong><\/h3>\n\n\n\n<p>Following is a simple example of&nbsp;<strong>read.csv()<\/strong>&nbsp;function to read a CSV file available in your current working directory \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data &lt;- read.csv(\"input.csv\")\nprint(data)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n <code>  id,   name,    salary,   start_date,     dept<\/code>\n 1      1    Rick     623.30    2012-01-01      IT\n 2      2    Dan      515.20    2013-09-23      Operations\n 3      3    Michelle 611.00    2014-11-15      IT\n 4      4    Ryan     729.00    2014-05-11      HR\n 5     NA    Gary     843.25    2015-03-27      Finance\n 6      6    Nina     578.00    2013-05-21      IT\n 7      7    Simon    632.80    2013-07-30      Operations\n 8      8    Guru     722.50    2014-06-17      Finance<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"r-charts-and-graphs\"><strong>R- Charts and Graphs<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"r-pie-charts\"><strong>R- Pie Charts<\/strong><\/h3>\n\n\n\n<p>Pie charts are created with the function&nbsp;<strong>pie(<\/strong><em>x<\/em><strong>, labels=)<\/strong>&nbsp;where&nbsp;<em>x<\/em>&nbsp;is a non-negative numeric vector indicating the area of each slice and labels= notes a character vector of names for the slices. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"syntax\"><strong>Syntax<\/strong><\/h4>\n\n\n\n<p>The basic syntax for creating a pie-chart using the R is \u2212<\/p>\n\n\n\n<p>pie(x, labels, radius, main, col, clockwise)<\/p>\n\n\n\n<p>Following is the description of the parameters used \u2212<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>x&nbsp;is a vector containing the numeric values used in the pie chart.<\/li>\n\n\n\n<li>labels&nbsp;are used to give a description of the slices.<\/li>\n\n\n\n<li>radius&nbsp;indicates the radius of the circle of the pie chart. (value between \u22121 and +1).<\/li>\n\n\n\n<li>main&nbsp;indicates the title of the chart.<\/li>\n\n\n\n<li>col&nbsp;indicates the color palette.<\/li>\n\n\n\n<li>clockwise&nbsp;is a logical value indicating if the slices are drawn clockwise or anti-clockwise.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"simple-pie-chart\"><strong>Simple Pie chart<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code># Simple Pie Chart\nslices &lt;- c(10, 12,4, 16, 8)\nlbls &lt;- c(\"US\", \"UK\", \"Australia\", \"Germany\", \"France\")\npie(slices, labels = lbls, main=\"Pie Chart of Countries\")<\/code><\/pre>\n\n\n\n<p><br><\/p>\n\n\n\n<p>3-D pie chart<\/p>\n\n\n\n<p> The&nbsp;<strong>pie3D( )<\/strong>&nbsp;function in the&nbsp;plotrix package provides 3D exploded pie charts. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># 3D Exploded Pie Chart\nlibrary(plotrix)\nslices &lt;- c(10, 12, 4, 16, 8)\nlbls &lt;- c(\"US\", \"UK\", \"Australia\", \"Germany\", \"France\")\npie3D(slices,labels=lbls,explode=0.1,\n   main=\"Pie Chart of Countries \")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"r-bar-charts\"><strong>R -Bar Charts<\/strong><\/h3>\n\n\n\n<p> A&nbsp;bar chart&nbsp;represents data in rectangular&nbsp;bars&nbsp;with a length of the&nbsp;bar&nbsp;proportional to the value of the variable.&nbsp;R&nbsp;uses the function barplot() to create&nbsp;bar charts.&nbsp;R&nbsp;can draw both vertical and Horizontal&nbsp;bars&nbsp;in the&nbsp;bar chart. Each of the bars can be given different colors in the bar chart.<\/p>\n\n\n\n<p> Let us suppose, we have a vector of maximum temperatures (in degree Celsius) for seven days as follows. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>max.temp &lt;- c(22, 27, 26, 24, 23, 26, 28)\nbarplot(max.temp)<\/code><\/pre>\n\n\n\n<p>Some of the frequently used ones are, \"main\" to give the title, \"xlab\" and \"ylab\" to provide labels for the axes, names.arg for naming each bar, \"col\" to define color, etc.<\/p>\n\n\n\n<p>We can also plot bars horizontally by providing the argument horiz=TRUE.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># barchart with added parameters\nbarplot(max.temp,\nmain = \"Maximum Temperatures in a Week\",\nxlab = \"Degree Celsius\",\nylab = \"Day\",\nnames.arg = c(\"Sun\", \"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\"),\ncol = \"darkred\",\nhoriz = TRUE)<\/code><\/pre>\n\n\n\n<p> Simply doing barplot(age)  will not give us the required plot. It will plot 10 bars with height equal to the student\u2019s age. But we want to know the number of students in each age category. <\/p>\n\n\n\n<p> This count can be quickly found using the table() function, as shown below. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; table(age)\nage\n16 17 18 19 \n1  2  6  1<\/code><\/pre>\n\n\n\n<p> Now plotting this data will give our required bar plot. Note below, that we define the argument \"density\" to shade the bars.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>barplot(table(age),\nmain=\"Age Count of 10 Students\",\nxlab=\"Age\",\nylab=\"Count\",\nborder=\"red\",\ncol=\"blue\",\ndensity=10\n)<\/code><\/pre>\n\n\n\n<p><br><\/p>\n\n\n\n<p>A histogram represents the frequencies of values of a variable bucketed into ranges. Histogram is similar to bar chat, but it groups the values into continuous ranges. Each bar in the histogram represents the height of the number of values present in that range.<\/p>\n\n\n\n<p>R creates a histogram using&nbsp;<strong>hist()<\/strong>&nbsp;function. This function takes a vector as an input and uses some more parameters to plot histograms.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"syntax\"><strong>Syntax<\/strong><\/h4>\n\n\n\n<p>The basic syntax for creating a histogram using R is \u2212<\/p>\n\n\n\n<p>hist(v,main,xlab,xlim,ylim,breaks,col,border)<\/p>\n\n\n\n<p>Following is the description of the parameters used \u2212<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>v<\/strong>&nbsp;is a vector containing numeric values used in the histogram.<\/li>\n\n\n\n<li><strong>main<\/strong>&nbsp;indicates the title of the chart.<\/li>\n\n\n\n<li><strong>col<\/strong>&nbsp;is used to set the color of the bars.<\/li>\n\n\n\n<li><strong>border<\/strong>&nbsp;is used to set the border color of each bar.<\/li>\n\n\n\n<li><strong>xlab<\/strong>&nbsp;is used to give a description of the x-axis.<\/li>\n\n\n\n<li><strong>xlim<\/strong>&nbsp;is used to specify the range of values on the x-axis.<\/li>\n\n\n\n<li><strong>ylim<\/strong>&nbsp;is used to specify the range of values on the y-axis.<\/li>\n\n\n\n<li><strong>breaks<\/strong>&nbsp;are used to mention the width of each bar.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\"><strong>Example<\/strong><\/h3>\n\n\n\n<p>A simple histogram is created using input vector, label, col, and border parameters.<\/p>\n\n\n\n<p>The script given below will create and save the histogram in the current R working directory.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Create data for the graph.\nv &lt;-  c(9,13,21,8,36,22,12,41,31,33,19)\n\n# Give the chart file a name.\npng(file = \"histogram.png\")\n\n# Create the histogram.\nhist(v,xlab = \"Weight\",col = \"yellow\",border = \"blue\")\n\n# Save the file.\ndev.off()<\/code><\/pre>\n\n\n\n<p><br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"range-of-x-and-y-values\"><strong>Range of X and Y values<\/strong><\/h3>\n\n\n\n<p>To specify the range of values allowed in X axis and Y axis, we can use the xlim and ylim parameters.<\/p>\n\n\n\n<p>The width of each bar can be decided by using breaks.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Create data for the graph.\nv &lt;- c(9,13,21,8,36,22,12,41,31,33,19)\n\n# Give the chart file a name.\npng(file = \"histogram_lim_breaks.png\")\n\n# Create the histogram.\nhist(v,xlab = \"Weight\",col = \"green\",border = \"red\", xlim = c(0,40), ylim = c(0,5),\n   breaks = 5)\n\n# Save the file.\ndev.off()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"r-vs-sas-which-tool-is-better\"><strong>R vs SAS \u2013 Which Tool is Better?<\/strong><\/h2>\n\n\n\n<p>The debate around data analytics tools has been going on forever. Each time a new one comes out, comparisons transpire. Although many aspects of the tool remain subjective, beginners want to know which tool is better to start with.<br>The most popular and widely used tools for data analytics are R and SAS. Both of them have been around for a long time and are often pitted against each other. So, let's compare them based on the most relevant factors.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Availability and Cost:<\/strong> SAS is widely used in most private organizations as it is a commercial software. It is more expensive than any other data analytics tool available. It might thus be a bit difficult to buy the software if you are an individual professional or a student starting out.&nbsp;On the other hand, R is an open-source software and is completely free to use. Anyone can begin using it right away without having to spend a penny.&nbsp;So, regarding availability and cost, R is hands down the better tool.<\/li>\n\n\n\n<li><strong>Ease of<\/strong> <strong>learning:<\/strong> Since SAS is a commercial software, it has many online resources available. Also, those who already know SQL might find it easier to adapt to SAS as it comes with PROC SQL option.&nbsp;The tool has a user-friendly GUI. It comes with extensive documentation and a tutorial base which can help early learners get started seamlessly.&nbsp;Whereas the learning curve for R is quite steep. You need to learn to code at the root level, and carrying out simple tasks demand a lot of time and effort with R. However, several forums and online communities post religiously about its usage.<\/li>\n\n\n\n<li><strong>Data Handling Capabilities:<\/strong> When it comes to data handling, both SAS and R perform well, but there are some caveats for the latter.&nbsp;While SAS can even churn through terabytes of data easily, R might be constrained as it uses the available RAM in the machine. This can be a hassle for 32-bit systems with low RAM capacity. Due to this, R can, at times, become unresponsive or give an 'out of memory' error. Both of them can run parallel computations, and support integrations for Hadoop, Spark, Cloudera, and Apache Pig, among others. Also, the availability of devices with better RAM capacity might negate the disadvantages of R.<\/li>\n\n\n\n<li><strong>Graphical Capabilities:<\/strong> Graphical capabilities or data visualization is the strongest forte of R. This is where SAS lacks behind in a major way. R has access to packages like GGPlot, RGIS, Lattice, and GGVIS, which provide superior graphical competency.&nbsp;In comparison, Base SAS is struggling hard to catch up with the advancements in graphics and visualization in data analytics. Even the graphics packages available in SAS are poorly documented, making them difficult to use.<\/li>\n\n\n\n<li><strong>Advancements in Tool:<\/strong> Advancements in the industry give way to advancements in tools, and both SAS and R hold up pretty well in this regard. As a corporate software, SAS rolls out new features and technologies frequently with new versions of its software.&nbsp;However, the updates are not as fast as R since it is open-source software with many contributors worldwide. Alternatively, the latest updates in SAS are pushed out after thorough testing, making them much more stable and reliable than R.&nbsp;Both tools have a fair share of pros &amp; cons.<\/li>\n\n\n\n<li><strong>Job Scenario:<\/strong>&nbsp;Currently, large corporations insist on using SAS, but SMEs and start-ups are increasingly opting for R, given that it's free. The current job trend seems to show that while SAS is losing its momentum, R is gaining potential. The job scenario is on the cusp of change, and both the tools seem strong, but since R is on an uphill path, it can probably witness more jobs in the future, albeit not in huge corporates.<\/li>\n\n\n\n<li><strong>Deep Learning Support:<\/strong>&nbsp;While SAS has just begun work on adding deep learning support, R has added support for a few packages which enable deep learning capabilities in the tool.&nbsp;You can use KerasR and keras package in R, which are mere interfaces for the original Keras package built on Python. Although none of the tools are excellent facilitators of deep learning, R has seen some recent active developments on this front.<\/li>\n\n\n\n<li><strong>Customer Service Support and Community:<\/strong> As one would expect from full-fledged commercial software, SAS offers excellent customer service support and the backing of a helpful community.&nbsp;Since R is free, open-source software, expecting customer support will be hard to justify. However, it has a vast online community that can help you with almost everything. On the other hand, no matter what problem you face with SAS, you can immediately reach out to their customer support and solve it without any hassles.<\/li>\n<\/ol>\n\n\n\n<p><strong>Final Verdict<\/strong><br>As per estimations by the Economic Times, the analytics industry will grow to $16 billion till 2025 in India.&nbsp;If you wish to venture into this domain, there can't be a better time. Just start learning the tool you think is better based on the comparison points above.<\/p>\n\n\n\n<p><br><a href=\"https:\/\/www.mygreatlearning.com\/academy\">Free online courses<\/a> on R programming offer a wealth of resources, expert instruction, and practical exercises to help individuals delve into the intricacies of R. These courses cover various aspects, including data manipulation, <a href=\"https:\/\/www.mygreatlearning.com\/blog\/what-is-statistical-analysis\/\">statistical analysis<\/a>, visualization, and machine learning using R, among others. Whether you are a beginner or an experienced programmer, these courses provide a flexible and accessible way to deepen your understanding and proficiency in R programming.<br><\/p>\n\n\n\n<p><br><br><br><\/p>\n\n\n\n<p><br><\/p>\n\n\n\n<p><br><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to R R is a programming language developed by Ross Ihaka and Robert Gentleman in 1993. R possesses an extensive catalog of statistical and graphical methods. It includes machine learning algorithms, linear regression, time series, and statistical inference, to name a few. Most R libraries are written in R, but C, C++, and Fortran [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":16807,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36847],"content_type":[],"class_list":["post-16512","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-r-programming"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>R Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/r-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"R Tutorial\" \/>\n<meta property=\"og:description\" content=\"Introduction to R R is a programming language developed by Ross Ihaka and Robert Gentleman in 1993. R possesses an extensive catalog of statistical and graphical methods. It includes machine learning algorithms, linear regression, time series, and statistical inference, to name a few. Most R libraries are written in R, but C, C++, and Fortran [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/r-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-10T10:54:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-12-16T04:18:44+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/shutterstock_119026663.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"700\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"19 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/r-tutorial\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/r-tutorial\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"R Tutorial\",\"datePublished\":\"2020-07-10T10:54:08+00:00\",\"dateModified\":\"2024-12-16T04:18:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/r-tutorial\\\/\"},\"wordCount\":4132,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/r-tutorial\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/shutterstock_119026663.jpg\",\"keywords\":[\"R Programming\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/r-tutorial\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/r-tutorial\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/r-tutorial\\\/\",\"name\":\"R Tutorial\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/r-tutorial\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/r-tutorial\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/shutterstock_119026663.jpg\",\"datePublished\":\"2020-07-10T10:54:08+00:00\",\"dateModified\":\"2024-12-16T04:18:44+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/r-tutorial\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/r-tutorial\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/r-tutorial\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/shutterstock_119026663.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/shutterstock_119026663.jpg\",\"width\":1000,\"height\":700},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/r-tutorial\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"R Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"R Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mygreatlearning.com\/blog\/r-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"R Tutorial","og_description":"Introduction to R R is a programming language developed by Ross Ihaka and Robert Gentleman in 1993. R possesses an extensive catalog of statistical and graphical methods. It includes machine learning algorithms, linear regression, time series, and statistical inference, to name a few. Most R libraries are written in R, but C, C++, and Fortran [&hellip;]","og_url":"https:\/\/www.mygreatlearning.com\/blog\/r-tutorial\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2020-07-10T10:54:08+00:00","article_modified_time":"2024-12-16T04:18:44+00:00","og_image":[{"width":1000,"height":700,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/shutterstock_119026663.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"19 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/r-tutorial\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/r-tutorial\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"R Tutorial","datePublished":"2020-07-10T10:54:08+00:00","dateModified":"2024-12-16T04:18:44+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/r-tutorial\/"},"wordCount":4132,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/r-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/shutterstock_119026663.jpg","keywords":["R Programming"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/r-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/r-tutorial\/","url":"https:\/\/www.mygreatlearning.com\/blog\/r-tutorial\/","name":"R Tutorial","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/r-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/r-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/shutterstock_119026663.jpg","datePublished":"2020-07-10T10:54:08+00:00","dateModified":"2024-12-16T04:18:44+00:00","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/r-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/r-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/r-tutorial\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/shutterstock_119026663.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/shutterstock_119026663.jpg","width":1000,"height":700},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/r-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"R Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/shutterstock_119026663.jpg",1000,700,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/shutterstock_119026663-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/shutterstock_119026663-300x210.jpg",300,210,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/shutterstock_119026663-768x538.jpg",768,538,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/shutterstock_119026663.jpg",1000,700,false],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/shutterstock_119026663.jpg",1000,700,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/shutterstock_119026663.jpg",1000,700,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/shutterstock_119026663.jpg",640,448,false],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/shutterstock_119026663.jpg",96,67,false],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/shutterstock_119026663.jpg",150,105,false]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":1,"uagb_excerpt":"Introduction to R R is a programming language developed by Ross Ihaka and Robert Gentleman in 1993. R possesses an extensive catalog of statistical and graphical methods. It includes machine learning algorithms, linear regression, time series, and statistical inference, to name a few. Most R libraries are written in R, but C, C++, and Fortran&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/16512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=16512"}],"version-history":[{"count":62,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/16512\/revisions"}],"predecessor-version":[{"id":104602,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/16512\/revisions\/104602"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/16807"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=16512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=16512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=16512"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=16512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}