In previous section, we studied about R Introduction, now we will be studying about R environment.
R studio has three main windows : Console, Workspace, and the output.
It is in the Console where we write the code most of the times and the output will be displayed there itself. If we write ‘here’, it will say, error: object ‘here’ not found, as this object is not there anywhere. But if the dataset is already there, and if we enter the object name of the dataset, then it will print the dataset. In the console, we give the commands and if we press Enter, then we can see the output right over here.
Then the Workspace is what you see on the top right of the R studio. We can see the data sets and the objects created in this window. Let us consider the dataset ‘AirPassengers’. This is monthly airline passenger data. Let us store it into an object ‘new_data’. Now ‘new_data’ is created and thus you can see that in the workspace. Anything you have created you can see it in the workspace. In R, to see an object we need to type a command ‘ls()’, whereas in R studio we can see it in the workspace.
Then the third one is the ‘Output’ window. In this, we can see the files, plots, packages, etc.There is one editor in R studio i.e., the R script. We can open it in file>new>R script. This is the actual editor of R studio. Let us write the command Air passengers, which are a default available dataset.
To run the command in R Script, press Ctrl+Enter. But the R script will not show any syntactical errors. It’s almost like notepad i.e., you need to write the code and save it and then afterwards while running the code itself, we see all the errors. The below image shows the console and workspace windows.
Let us do a quick warm-up. Open R studio and type in the following commands.
>68+28
[1] 96
>134*456
[1] 61104
>sqrt(119)
[1] 10.90871
>log(10)
[1] 2.302585
>exp(5)
[1] 148.4132
As soon as we hit enter, then the results will appear there itself, so we can say that command line itself is a calculator.Here it is dynamic variable declaration i.e., we do not need to declare everything at the beginning.
Workspace Demo
To see objects in workspace we can also write ‘ls()’. Using this command, all the objects created will be listed.
R is a case sensitive language, i.e., ls() cannot be written as LS(), and the same goes with all variables.Whenever we get an error, most of the times it has to do with this uppercase and lowercase.
Assignment Operator in R
The First operator that we need to learn is Assign operator.
> x<-7 > x [1] 7
We can also use “=”, but it’s always a good coding practice to use “<-” mark. It is read from right to the left i.e., 7 is assigned to x. When we type x<-7 and press enter, and then object x gets created which is displayed in the workspace. Now if we type x, it will show the value assigned to it, i.e., [1] 7. Now [1] means 7 is the first number assigned to x, as x can have multiple elements assigned to it. [1] Is just an index for x, if there are multiple values, the index can be [1],[2],[3] and so on. So [1] is just the index & 7 is the answer. To make this clear, let’s consider an example where more than one element is assigned to x. x<-1:35 that means x is 1 to 35.
> x<-1:35 > x [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 [31] 31 32 33 34 35
The index, based output makes our reading very easy. What R does is, it starts with index [1] to 30 and then prints the index [31], when 31 came in the next line. This is being done as we don’t get confused. Like if these could have been names like state names, and if the index is [31] then it’s the 31st state name and so on. This makes the reading easier.
Let us consider some more example
> x<-7 > y<-68+28 > z<-134*456 > k<-sqrt(119)
For printing it, just type: y z k and the results will be displayed. So what we are doing here is just assigning 68+28 in y, assigning 134*456 in z & assigning sqrt(119) in k. Here are some mathematical calculations which have been assigned to the objects. Assignment can be done using this “<-” assign operator.
>x [1] 7
>y [1] 96
>z [1] 61104
>k [1] 10.90871
> z/x [1] 8729.143
Naming Conventions
The names must start with an upper or lower case letter (A-Z or a-z) thus R is case-sensitive. They can contain digits (0-9), periods, underscore, etc.
The object newdata is not equal to NewData.
Working With R
Now let us explore some more concepts in R.
> x <- rnorm (1000, mean=20, sd=5)
So here rnorm is a function which will create a normal distribution for 1000 elements with mean 20 and standard deviation 5. Here x will have 1000 elements. By typing x in the console, we can get all the 1000 elements.
> x <- rnorm(1000,mean=20,sd=5)
> x
This will print output as shown :
## [1] 29.727512 22.027772 14.173818 11.567661 11.290773 28.545150
## [7] 23.336270 10.623532 25.238777 28.339100 19.317402 22.607638
## [13] 16.696072 20.682688 18.952773 21.853967 24.366763 19.695094
## [19] 17.400101 21.136721 29.736818 23.917742 25.168942 25.422684
## [25] 11.653086 22.272998 27.392081 15.312394 20.621272 20.774217
## [31] 26.400664 20.912214 21.110055 24.317066 18.775163 19.759073
## [37] 17.423549 25.615045 20.155949 20.382279 24.729653 17.432053
## [43] 17.433073 16.289813 22.945870 24.711526 17.996536 17.654353
## [49] 33.622504 17.235110 24.032638 18.196793 14.204424 15.881424
## [55] 20.831512 29.730846 19.276928 18.181224 25.362097 14.644336
## [61] 22.779562 11.467166 17.549835 22.901572 30.653036 13.740296
## [67] 15.412779 21.977330 26.986697 23.066531 20.802516 27.602665
## [73] 21.174894 19.420062 29.804671 25.722596 22.245351 20.137828
## [79] 8.641546 18.413145 16.674934 17.744648 14.345363 26.419367
## [85] 25.286868 22.819012 19.278091 24.047195 20.367724 7.406066
## [91] 25.085621 24.219120 19.048239 22.182127 10.387633 26.160024
## [97] 21.120116 20.110008 26.169600 21.986982 23.216666 28.166575
## [103] 17.877387 12.994729 13.082539 19.386388 21.613470 26.454635
## [109] 21.686849 27.967669 14.250426 19.472040 17.442288 15.332908
## [115] 27.594943 28.256114 32.458636 16.783834 14.985155 25.150545
## [121] 12.632237 16.698488 11.660866 21.716199 20.933646 22.702097
## [127] 15.476770 20.205206 26.343377 10.139146 15.813740 15.634903
## [133] 16.514700 24.291489 15.913789 23.240543 20.506762 16.580606
## [139] 22.207518 9.691391 16.676488 19.984987 21.002244 17.029171
## [145] 20.806644 11.498186 19.899529 22.933672 14.163869 21.947553
## [151] 25.157130 18.244896 16.461983 8.412688 25.306571 23.125001
## [157] 17.606258 30.266219 22.116809 16.431025 17.083604 15.466805
## [163] 24.401583 19.544583 19.942909 27.119469 18.213193 14.463331
## [169] 22.176626 19.306320 17.776627 22.418411 17.234031 18.643855
## [175] 14.138607 24.799317 13.090200 21.463304 34.173550 16.199937
## [181] 17.352828 28.606000 15.466058 18.753820 25.505661 22.614690
## [187] 27.898874 24.758957 18.805842 20.594374 22.588168 23.769131
## [193] 19.655519 9.011509 25.684415 20.046036 16.952598 29.516783
## [199] 20.608292 15.339981 25.789671 21.119468 15.386253 17.075294
## [205] 20.530112 17.378699 22.577657 23.077779 16.267017 34.587203
## [211] 17.038457 22.662198 18.369485 20.938257 17.675383 16.516821
## [217] 19.366059 12.921027 21.492170 19.151660 28.779073 15.795389
## [223] 22.036802 26.010214 18.312752 16.922768 19.836704 23.957549
## [229] 8.068545 20.327931 15.564294 31.962241 12.362306 24.442804
## [235] 22.079282 22.009864 30.348366 16.951518 17.823881 13.821275
## [241] 23.030961 23.299950 17.091296 13.137487 17.041698 19.996918
## [247] 20.373975 32.081578 23.157645 17.717973 19.656141 19.713430
## [253] 16.304237 15.845531 21.181814 18.263276 31.491360 13.226387
## [259] 20.753108 33.975560 21.556913 25.103936 30.089965 10.538623
## [265] 30.962363 23.459218 13.792790 19.196656 20.129247 24.564201
## [271] 24.577835 21.519863 29.632636 13.814721 25.846305 29.057134
## [277] 20.171712 18.640709 26.183392 16.849311 13.704763 20.029619
## [283] 16.997223 25.276494 25.027250 15.552910 27.133327 23.615325
## [289] 25.210040 18.196115 16.591711 16.425250 15.852650 16.721285
## [295] 23.976945 23.120209 16.552743 22.714420 22.319958 31.019111
## [301] 23.850647 17.985637 21.325981 23.062039 17.563887 13.274493
## [307] 13.466433 19.765545 11.638812 21.977456 31.443411 19.689766
## [313] 19.683234 22.671269 27.047825 21.140776 14.350831 23.163323
## [319] 24.993487 13.308481 17.804535 17.430428 22.258972 16.858152
## [325] 24.146135 28.916969 20.784642 22.103654 23.235227 18.363599
## [331] 17.491072 22.285308 27.187974 24.384006 25.319820 25.645659
## [337] 18.077088 15.741573 17.156785 17.726549 15.399142 22.906717
## [343] 26.668604 20.498866 30.550796 22.255092 21.430002 17.090993
## [349] 21.083509 26.410319 25.184856 7.431343 22.397728 18.231511
## [355] 23.770490 22.288674 21.518100 17.622687 20.853831 16.578927
## [361] 23.623828 15.994951 30.322290 20.969471 18.448244 20.520756
## [367] 19.460768 21.296014 18.290777 15.212567 26.234917 17.666005
## [373] 23.687114 26.716202 13.969268 24.962188 23.602565 14.667628
## [379] 23.822215 18.320073 19.108169 17.859525 17.757073 25.693470
## [385] 21.641692 17.674582 13.870166 12.694512 19.477806 22.126312
## [391] 21.341572 15.989978 28.426667 16.442703 26.313395 11.106016
## [397] 21.517167 19.037988 32.208319 21.036600 10.401488 31.509901
## [403] 16.909775 18.415838 9.804727 22.178392 13.978888 24.746192
## [409] 14.801578 17.466264 18.272845 19.615796 21.026918 26.233214
## [415] 20.083827 22.525222 9.372677 5.471432 16.726941 19.586206
## [421] 22.416402 15.593327 23.508801 22.653192 21.768656 21.190214
## [427] 29.530999 7.345017 20.875496 12.102681 22.081654 25.687963
## [433] 10.674088 21.643438 21.733889 21.527027 29.696669 19.753639
## [439] 22.414899 17.257994 13.960491 30.577455 13.590040 16.520321
## [445] 20.385122 23.055598 13.264064 13.384380 26.204026 15.962176
## [451] 30.001610 24.975408 22.229407 25.832827 20.740921 17.902771
## [457] 16.818382 20.727127 18.295186 13.015456 17.720566 23.635101
## [463] 24.290884 19.195902 14.333086 16.042536 21.576213 22.272675
## [469] 17.318944 30.251992 18.554159 29.921826 23.126921 22.335424
## [475] 11.337965 9.712197 8.717392 15.707144 29.278814 19.281417
## [481] 18.233900 12.032921 18.732276 16.491830 15.407045 24.378203
## [487] 17.095985 21.029514 16.466080 19.070435 15.672621 18.779453
## [493] 22.927014 26.485272 22.902771 21.741249 24.942199 22.276011
## [499] 19.850424 15.324483 19.433889 18.308885 26.614344 21.896915
## [505] 11.770512 25.121654 18.051333 25.562428 28.974449 21.074008
## [511] 24.134702 8.458984 20.923043 25.735673 20.900147 14.571439
## [517] 23.795236 11.557096 19.032457 29.499164 19.420786 21.199031
## [523] 25.932573 8.876581 12.821955 18.202377 18.554885 23.426905
## [529] 26.536813 21.380011 21.846392 16.335892 24.277272 28.138421
## [535] 26.500791 23.818051 11.960563 23.589115 19.475901 18.853133
## [541] 21.214499 28.666910 24.947389 17.659138 12.469256 27.073906
## [547] 26.686557 23.085566 14.240169 14.808822 16.688735 13.946308
## [553] 27.804613 27.700073 15.900484 18.888252 23.150262 20.993335
## [559] 20.078851 23.775798 26.474347 14.854370 20.240155 24.757675
## [565] 22.219904 16.192665 13.060905 26.078641 17.518245 25.785360
## [571] 28.437244 16.988412 20.572044 16.155074 24.252410 20.858714
## [577] 22.430396 26.036905 11.690045 12.464086 30.407987 31.106195
## [583] 15.477675 19.804692 19.451855 22.531757 22.320660 17.879739
## [589] 34.887069 22.801761 21.179131 14.492035 28.361927 29.077376
## [595] 20.534770 26.247919 20.395065 21.099368 9.295359 21.247362
## [601] 10.408193 18.622757 25.963462 27.524196 22.760837 17.005384
## [607] 22.010480 25.358175 21.348835 20.721979 14.261099 26.914990
## [613] 21.060421 17.496182 20.226238 27.610966 22.209560 20.015765
## [619] 16.287719 12.893625 26.409264 14.567679 29.352838 19.716488
## [625] 22.001347 17.045938 12.300877 19.785634 19.221634 16.768579
## [631] 13.309049 22.909324 24.992536 15.502990 20.884396 22.417801
## [637] 15.081900 21.522051 24.911567 24.561802 19.743491 17.364330
## [643] 11.448953 24.816523 15.493115 21.037278 22.436472 14.611121
## [649] 24.101681 16.471575 23.441284 20.519828 14.437120 21.932926
## [655] 25.019366 21.190071 21.717150 23.081726 20.684399 19.836137
## [661] 15.419452 21.184117 26.328972 20.669426 16.886217 16.478219
## [667] 23.762737 29.938412 15.331936 10.079235 18.091092 12.324329
## [673] 19.437112 19.931701 25.508238 21.073580 23.088567 18.154038
## [679] 17.463793 25.977010 29.278583 23.100171 25.379744 21.772949
## [685] 16.971225 20.761156 12.820146 30.591575 20.839551 31.781976
## [691] 15.987095 17.504043 28.677303 16.711454 15.792376 18.828107
## [697] 22.164995 17.282692 19.343596 22.531538 20.263012 19.678264
## [703] 18.709399 20.405853 20.782477 19.767363 16.053323 27.061384
## [709] 19.737181 26.483659 19.398232 22.890951 12.145002 26.302212
## [715] 20.752499 12.414235 23.207896 30.764119 27.088660 13.567575
## [721] 13.596255 25.377880 24.870826 17.340803 24.755759 25.542423
## [727] 19.115129 21.414287 23.440763 27.871175 22.037819 23.180335
## [733] 22.511037 19.221952 15.939205 25.054925 30.220672 13.119708
## [739] 15.706674 25.004249 19.257284 16.632356 19.541559 16.974871
## [745] 16.852292 24.538191 23.413720 22.405756 17.476713 18.778854
## [751] 20.829434 19.111266 24.626245 24.301301 21.936837 21.610876
## [757] 23.844085 30.350177 21.518825 19.280792 23.284695 26.628839
## [763] 24.106712 19.448038 14.513543 20.719627 26.996327 14.628779
## [769] 22.572400 10.394729 17.961564 23.135872 15.984582 24.298295
## [775] 19.188606 21.169912 17.830440 25.480069 14.568884 15.680531
## [781] 19.830539 18.630551 20.866931 27.819166 15.121630 13.843840
## [787] 16.388020 17.040887 19.785344 16.278083 22.651521 24.395421
## [793] 25.486104 25.549499 22.980868 19.648534 14.658246 12.985290
## [799] 27.238426 22.086507 15.030376 26.842067 18.007588 22.657393
## [805] 28.744461 24.935354 17.174687 18.403790 23.801563 27.410771
## [811] 14.474528 23.857910 18.500923 20.949005 16.417592 23.272319
## [817] 13.074286 12.346769 23.483140 20.184164 18.510095 22.052894
## [823] 20.589209 25.118588 26.846245 22.489599 25.267335 12.491648
## [829] 19.373453 16.047960 18.556984 16.257403 21.905341 20.213579
## [835] 16.548013 12.471732 25.618366 28.437215 17.456180 18.373999
## [841] 17.881439 17.545000 25.063343 24.855744 12.930833 23.838372
## [847] 17.539595 15.891848 21.157843 20.735751 18.970610 16.890390
## [853] 15.906976 23.509515 20.399574 18.058508 21.689823 22.778539
## [859] 22.723346 21.015544 21.295720 16.465603 13.611244 16.586355
## [865] 20.908675 21.148464 26.357688 18.260495 16.272430 16.755129
## [871] 25.984127 31.489427 17.261744 21.772685 22.007434 24.851916
## [877] 19.733491 20.589950 14.416504 21.628835 23.928860 10.147253
## [883] 16.553029 14.502338 21.434648 21.507492 15.579007 22.270235
## [889] 19.189912 22.075889 23.655799 17.971290 25.127437 7.930384
## [895] 19.048262 17.705362 14.745877 19.528426 30.919585 19.000064
## [901] 19.304166 19.775426 17.546814 19.157778 13.854038 16.572636
## [907] 16.745860 20.939633 12.698834 17.521182 18.247028 15.698520
## [913] 16.377031 21.862080 22.000891 20.879660 13.626218 23.695772
## [919] 9.929162 25.062728 14.243576 13.266657 10.237104 17.133295
## [925] 14.053217 15.329687 12.266174 22.342217 13.786468 20.427231
## [931] 20.518508 15.643824 16.700909 23.960381 17.542203 11.539881
## [937] 14.208695 22.459240 22.399263 11.164244 33.942188 24.081148
## [943] 25.887615 23.564897 20.836841 27.789659 23.848107 19.531269
## [949] 20.795473 16.967840 20.019288 17.733159 13.076931 14.758359
## [955] 15.186519 13.594224 17.271994 14.996255 18.831467 31.342821
## [961] 23.492658 22.039948 21.045584 23.673465 16.381956 9.372736
## [967] 20.180343 20.821498 19.630436 20.919515 19.234222 27.355534
## [973] 21.447976 21.647215 19.309875 26.374084 25.485520 17.149242
## [979] 15.161896 15.295486 11.753244 21.928955 18.138346 21.055981
## [985] 19.484901 16.884938 27.356565 27.056626 13.589372 19.441235
## [991] 25.480919 14.468213 35.919713 19.517106 11.803202 10.565383
## [997] 17.919860 23.029789 20.178839 24.005591
Now if we need only the 1st 100 elements then we can get it by typing the command x[1:100] which will print the first 100 elements.
>mean_x<-mean(x)
Here we are creating a new object, mean_x. Now, x is a normal distribution with mean 20 as we have already defined it. When we enter mean_x, we get the value 20 or a value which is almost equal to 20.
>mean_x [1] 20.0092
Now let’s calculate the standard deviation.
>s<-sd(x)
sd(x) is an object for the standard deviation. Thus we get the value as 4.91 i.e., almost 5, which we had defined already. We have created random variables hence the values will not be exactly the same. Thus, as the number of elements will increase, the mean will also change. As we increase the number, the overall distribution will move towards the actual value. If the standard deviation is reduced, then the mean(x) will be almost 20.
In the next post you will learn about R Packages.
In next section, we will be studying about R Packages.