R Programming Language For Beginners Learn R Programming Language Tutorial Shila instant download
R Programming Language For Beginners Learn R Programming Language Tutorial Shila instant download
https://ebookbell.com/product/r-programming-language-for-
beginners-learn-r-programming-language-tutorial-shila-58536520
https://ebookbell.com/product/expert-advisor-programming-for-
metatrader-5-creating-automated-trading-systems-in-the-mql5-language-
andrew-r-young-11812946
https://ebookbell.com/product/practical-foundations-for-programming-
languages-2ed-book-draft-harper-r-6739732
https://ebookbell.com/product/practical-foundations-for-programming-
languages-harper-r-1528004
https://ebookbell.com/product/introduction-to-r-programming-language-
mohsen-nady-50975710
Instant R Starter Jump Into The R Programming Language And Go Beyond
Donato Teutonico
https://ebookbell.com/product/instant-r-starter-jump-into-the-r-
programming-language-and-go-beyond-donato-teutonico-5471826
https://ebookbell.com/product/the-hitchhikers-guide-to-linear-models-
based-on-the-famous-r-programming-language-mauricio-vargas-
seplveda-57662768
https://ebookbell.com/product/the-hitchhikers-guide-to-econometrics-
based-on-the-famous-r-programming-language-1-mauricio-vargas-
seplveda-51428730
https://ebookbell.com/product/the-scheme-programming-language-r-kent-
dybvig-56576608
https://ebookbell.com/product/beginning-r-the-statistical-programming-
language-1st-edition-gardener-55423056
R programming language For
Beginners : Learn R Programming
Language Tutorial
(Language of Book : English)
Author
Kavi & Shila
Publisher
Kavi & Shila Book Publication House
Ukwa Balaghat 481105 (Madhya Pradesh)
Copyright © 2024 kdp copyright All rights reserved.
No part of this publication may be reproduced, distributed, or transmitted in
any form or by any means, including photocopying, recording, or other
electronic or mechanical methods, without the prior written permission of
the publisher, except in the case of brief quotations embodied in reviews
and certain other non-commercial uses permitted by copyright law.
Table Of Contents
Chapter - 1 : Overview
Evolution of R
Features of R
Chapter - 2 : Environment Setup
Local Environment Setup
Windows Installation
Linux Installation
Chapter - 3 : Basic Syntax
R Command Prompt
R Script File
Comments
Chapter - 4 : Data Types
Vectors
Lists
Matrices
Arrays
Factors
Data Frames
Chapter - 5 : Variables
Variable Assignment
Data Type of a Variable
Finding Variables
Deleting Variables
Chapter - 6 : Operators
Types of Operators
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Miscellaneous Operators
Chapter - 7 : Decision making
Chapter - 8 : Loops
Loop Control Statements
Chapter - 9 : Functions
Function Definition
Function Components
Built-in Function
User-defined Function
Calling a Function
Calling a Function without an Argument
Calling a Function with Argument Values (by position and by name)
Calling a Function with Default Argument
Lazy Evaluation of Function
Chapter - 10 : Strings
Rules Applied in String Construction
Examples of Valid Strings
Examples of Invalid Strings
String Manipulation
Concatenating Strings - paste() function
Syntax
Example
Formatting numbers & strings - format() function
Syntax
Example
Counting number of characters in a string - nchar() function
Syntax
Example
Changing the case - toupper() & tolower() functions
Syntax
Example
Extracting parts of a string - substring() function
Syntax
Example
Chapter - 11 : Vectors
Vector Creation
Single Element Vector
Multiple Elements Vector
Accessing Vector Elements
Vector Manipulation
Vector arithmetic
Vector Element Recycling
Vector Element Sorting
Chapter - 12 : Lists
Creating a List
Naming List Elements
Accessing List Elements
Manipulating List Elements
Merging Lists
Converting List to Vector
Chapter - 13 : Matrices
Syntax
Example
Accessing Elements of a Matrix
Matrix Computations
Matrix Addition & Subtraction
Matrix Multiplication & Division
Chapter - 14 : Arrays
Example
Naming Columns and Rows
Accessing Array Elements
Manipulating Array Elements
Calculations Across Array Elements
Syntax
Example
Chapter - 15 : Factors
Example
Factors in Data Frame
Changing the Order of Levels
Generating Factor Levels
Syntax
Example
Chapter - 16 : Data Frames
Create Data Frame
Get the Structure of the Data Frame
Summary of Data in Data Frame
Extract Data from Data Frame
Expand Data Frame
Add Column
Add Row
Chapter - 17 : Packages
Check Available R Packages
Get the list of all the packages installed
Install a New Package
Install directly from CRAN
Install package manually
Load Package to Library
Chapter - 18 : Data Reshaping
Joining Columns and Rows in a Data Frame
Merging Data Frames
Melting and Casting
Melt the Data
Cast the Molten Data
Chapter - 19 : CSV Files
Getting and Setting the Working Directory
Input as CSV File
Reading a CSV File
Analysing the CSV File
Get the maximum salary
Get the details of the person with max salary
Get all the people working in IT department
Get the persons in IT department whose salary is greater than 600
Get the people who joined on or after 2014
Writing into a CSV File
Chapter - 20 : Excel File
Install xlsx Package
Verify and Load the "xlsx" Package
Input as xlsx File
Reading the Excel File
Chapter - 21 : Binary Files
Syntax
Example
Writing the Binary File
Reading the Binary File
Chapter - 22 : XML Files
Input Data
Reading XML File
Get Number of Nodes Present in XML File
Details of the First Node
Get Different Elements of a Node
XML to Data Frame
Chapter - 23 : JSON Files
Install rjson Package
Input Data
Read the JSON File
Convert JSON to a Data Frame
Chapter - 24 : Web Data
Install R Packages
Input Data
Example
Verify the File Download
Chapter - 25 : Databases
RMySQL Package
Connecting R to MySql
Querying the Tables
Query with Filter Clause
Updating Rows in the Tables
Inserting Data into the Tables
Creating Tables in MySql
Dropping Tables in MySql
Chapter - 26 : Pie Charts
Syntax
Example
Pie Chart Title and Colours
Example
Slice Percentages and Chart Legend
3D Pie Chart
Chapter - 27 : Bar Charts
Syntax
Example
Bar Chart Labels, Title and Colours
Example
Group Bar Chart and Stacked Bar Chart
Chapter - 28 : Box Plots
Syntax
Example
Creating the Boxplot
Boxplot with Notch
Chapter - 29 : Histograms
Syntax
Example
Range of X and Y values
Chapter - 30 : Line Graphs
Syntax
Example
Line Chart Title, Color and Labels
Example
Multiple Lines in a Line Chart
Chapter - 31 : Scatterplots
Syntax
Example
Creating the Scatterplot
Scatterplot Matrices
Syntax
Example
Chapter - 32 : Mean, Median and Mode
Mean
Syntax
Example
Applying Trim Option
Applying NA Option
Median
Syntax
Example
Mode
Example
Chapter - 33 : Linear Regression
Steps to Establish a Regression
Input Data
lm() Function
Syntax
Create Relationship Model & get the Coefficients
Get the Summary of the Relationship
predict() Function
Syntax
Predict the weight of new persons
Visualise the Regression Graphically
Chapter - 34 : Multiple Regression
lm() Function
Syntax
Example
Input Data
Create Relationship Model & get the Coefficients
Create Equation for Regression Model
Apply Equation for predicting New Values
Chapter - 35 : Logistic Regression
Syntax
Example
Create Regression Model
Conclusion
Chapter - 36 : Normal Distribution
dnorm()
pnorm()
qnorm()
rnorm()
Chapter - 37 : Binomial Distribution
dbinom()
pbinom()
qbinom()
rbinom()
Chapter - 38 : Poisson Regression
Syntax
Example
Create Regression Model
Chapter - 39 : Analysis of Covariance
Example
Input Data
ANCOVA Analysis
Model with interaction between categorical variable and predictor
variable
Model without interaction between categorical variable and predictor
variable
Comparing Two Models
Chapter - 40 : Time Series Analysis
Syntax
Example
Different Time Intervals
Multiple Time Series
Chapter - 41 : Nonlinear Least Square
Syntax
Example
Chapter - 42 : Decision Tree
Install R Package
Syntax
Input Data
Example
Conclusion
Chapter - 43 : Random Forest
Install R Package
Syntax
Input Data
Example
Conclusion
Chapter - 44 : Survival Analysis
Install Package
Syntax
Example
Applying Surv() and survfit() Function
Chapter - 45 : Chi Square Test
Syntax
Example
Conclusion
Chapter - 1 : Overview
R is a programming language and software environment for statistical
analysis, graphics representation and reporting. R was created by Ross
Ihaka and Robert Gentleman at the University of Auckland, New Zealand,
and is currently developed by the R Development Core Team.
The core of R is an interpreted computer language which allows branching
and looping as well as modular programming using functions. R allows
integration with the procedures written in the C, C++, .Net, Python or
FORTRAN languages for efficiency.
R is freely available under the GNU General Public License, and pre-
compiled binary versions are provided for various operating systems like
Linux, Windows and Mac.
R is free software distributed under a GNU-style copy left, and an official
part of the GNU project called GNU S.
Evolution of R
R was initially written by Ross Ihaka and Robert Gentleman at the
Department of Statistics of the University of Auckland in Auckland, New
Zealand. R made its first appearance in 1993.
1. A large group of individuals has contributed to R by sending
code and bug reports.
2. Since mid-1997 there has been a core group (the "R Core Team")
who can modify the R source code archive.
Features of R
As stated earlier, R is a programming language and software environment
for statistical analysis, graphics representation and reporting. The following
are the important features of R −
1. R is a well-developed, simple and effective programming
language which includes conditionals, loops, user defined
recursive functions and input and output facilities.
2. R has an effective data handling and storage facility,
3. R provides a suite of operators for calculations on arrays, lists,
vectors and matrices.
4. R provides a large, coherent and integrated collection of tools for
data analysis.
5. R provides graphical facilities for data analysis and display either
directly at the computer or printing at the papers.
Linux Installation
R is available as a binary for many versions of Linux at the location R
Binaries.
The instructions to install Linux vary from flavour to flavour. These steps
are mentioned under each type of Linux version in the mentioned link.
However, if you are in a hurry, then you can use yum command to install R
as follows −
$ yum install R
Now you can use the install command at the R prompt to install the
required package. For example, the following command will install the
plotrix package which is required for 3D charts.
> install.packages("plotrix")
R Command Prompt
Once you have R environment setup, then it’s easy to start your R command
prompt by just typing the following command at your command prompt −
$R
This will launch R interpreter and you will get a prompt > where you can
start typing your program as follows −
> myString <- "Hello, World!"
> print ( myString)
[1] "Hello, World!"
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 variable myString.
R Script File
Usually, you will do your programming by writing your programs in script
files and then you execute those scripts at your command prompt with the
help of an R interpreter called Rscript. So let's start with writing following
code in a text file called test.R as under −
Live Demo
# My first program in R Programming
myString <- "Hello, World!"
print ( myString)
Save the above code in a file test.R and execute it at Linux command
prompt as given below. Even if you are using Windows or another system,
syntax will remain the same.
$ Rscript test.R
Comments
Comments are like helping text in your R program and they are ignored by
the interpreter while executing your actual program. Single comment is
written using # in the beginning of the statement as follows −
# My first program in R Programming
R does not support multi-line comments but you can perform a trick which
is something as follows −
Live Demo
if(FALSE) {
"This is a demo for multi-line comments and it should be put inside either
a
single OR double quote"
}
myString <- "Hello, World!"
print ( myString)
[1] "Hello, World!"
Though the above comments will be executed by the R interpreter, they will
not interfere with your actual program. You should put such comments
inside, either single or double quotes.
25 Amer. Journ. Science, xxxv, 1888, 367, 448; Seventh Annual Rep. U. S. G. S., 1888,
545.
TOPOGRAPHIC MODELS.
BY COSMOS MINDELEFF.
To those who must give first place to the quantity of relief rather
than the quality, as, for example, the geologist or the engineer, a
contoured map is now considered essential. On the other hand,
where quality of relief is the prime consideration and the quantity a
secondary one, as, for example, for the use of the army, a hachured
map is considered the best. The method of hachures may be roughly
characterized as a graphic system with a conventional element, and
the contour method as a conventional system with a graphic
element,—for if the contour interval is small enough a sort of
shading is produced which helps considerably the idea of relief.
The uses of models are many and various. Within the past few years
their usefulness has been much extended, and, now that they are
becoming better known, will probably receive a still further
extension. To the geologist they are often of great value in working
out the structure of complicated districts, for the reason that so
many important structural relations can be presented to the eye at a
single glance. Similarly, for the graphic presentation of results there
is no better method, as the topography, the surface geology, and
any number of sections can be shown together and seen in their
proper relationship. To the engineer an accurate model is often of
the greatest assistance in working out his problems, and it is simply
invaluable to explain the details of a plan to anyone who has little or
no technical training; for, as has been stated, a model is easily
comprehended by anyone, while more or less technical knowledge is
required for the proper understanding of even the best maps.
For the construction of a good relief map the first requisite is a good
contoured map. To this should be added, when possible, a good
hachured map, upon which the elevations of the principal points are
stated,—if the interval in the contoured map is a large one,—and as
much material in the way of photographs and sketches as it is
possible to procure. The modeler should, moreover, have some
personal acquaintance with the region to be represented, or, failing
that, a general knowledge of topographic forms, and at least a clear
conception of the general character of the country which he seeks to
represent. This is very important, for it is here that many modelers
fail: the mechanical portion of the work any ordinarily intelligent
person can do. A model may be as accurate as the map from which
it is made, every contour may be placed exactly where it belongs,
and yet the resulting model may be,—indeed, often is—"flat,"
expressionless, and unsatisfactory. Every topographer in drawing his
map is compelled to generalize more or less, and it is fortunate for
the map if this be done in the field instead of in the draughtsman's
office. But topographers differ among themselves: there may be,
and often is, considerable difference in two maps of the same region
made by different men; in other words, the "personal equation" is a
larger element in a map than is usually supposed. This being the
case, there is something more required in a modeler than the mere
transferring of the matter in the map,—giving it three dimensions
instead of two: he must supply through his special knowledge of the
region (or, failing that through his general knowledge) certain
characteristics that do not appear upon the map, and undo, so far as
it is necessary, certain generalizations of the topographer and
draughtsman. This artistic or technical skill required correctly to
represent the individuality of a given district is especially important
in the modeler; it is more important, perhaps, in small-scale maps of
large districts than in large-scale maps of small ones,—for in the
latter the generalizing process has not been carried so far, and the
smaller interval of the contour lines preserves much of the detail.
The methods by which relief maps are made have always received
more attention than would, at first sight, appear to be their proper
proportion. It may be due, however, to the difficulty of applying any
test to determine the accuracy of the finished model, and perhaps
also to the general impression that any one can make a relief map,—
and so he can, though of course there will be a wide difference in
the value of the results. Some, indeed, have devoted their attention
to methods exclusively, letting the result take care of itself,—and the
models show it. There is no more reason why a modeler should tie
himself down to one method of work, than that a water-colorist, or a
chemist, or anyone engaged in technical work, should do so; though
in some cases he might be required, as the chemist is, to show his
methods as well as his results.
The next step in the process is to fill in with clay or wax the intervals
between the contours. I have always found wax more convenient
than clay for this purpose as, unless the surface coating is a thick
one, the clay is difficult to keep moist. To obviate this difficulty, some
modelers have used clay mixed with glycerine instead of water; this,
of course, does not become dry, but the material is, at its best,
unsatisfactory. The filling-in process is the most important one in
relief map making, for it is here that the modeler must show his
knowledge of, and feeling for, topographic forms. Some models seem
to have been constructed with the idea that when the contours have
been accurately placed the work of the modeler is practically done.
This is a great mistake. The card-board contours are only a means of
control, occupying somewhat the same relation to the relief map
that a core or base of bricks, or a frame of wood, does to other
constructions as, for example, an architectural ornament or a bust.
It is sometimes necessary to cut away the contour card; for, as has
been already explained, a map is more or less generalized, and a
contour is frequently carried across a ravine, instead of following it
up, as it would do if the map were on a larger scale. Such
generalizing is of course perfectly proper in a map, but, with the
same scale, we expect more detail in a model. The modeler must
have judgment enough and skill enough to read between the lines,
and to undo the generalizing of the topographer and draughtsman,
thus supplying the material omitted from the map. This can be done
without materially affecting the accuracy of the model, considered
even as a copy of the contoured map.
Casts of models have been made in iron; but this, while suitable
perhaps for models of mounds and subjects of like character, would
hardly be applicable to small scale models with fine detail; such
casts require too much surface finishing. The material known as
Lincrusta-Walton seems to me to be the ideal material for this
purpose. It is tougher than rubber, will take the finest detail, and its
surface can be treated in any way desired. Unfortunately the
manufacture of models in this material would require expensive
machinery, and is outside the scope of a modeling room. Should it
ever become commercially advantageous, however, casts of a model
of ordinary size, in every way equal to the original, can be turned
out in this material at a very small cost.
The paper of the evening was entitled, "Surveys, their Kinds and
Purposes," by Mr. Marcus Baker. The paper was discussed by Messrs.
Ogden, Goodfellow, Gannett and Baker. Published in "Science," Vol.
XII, No. 304.
The Society met in the Society Hall of the Cosmos Club, President
Hubbard in the chair. Owing to the absence from the city of the
Secretaries, Mr. O. H. Tittmann was requested to act as Secretary of
the meeting. The minutes of the first and fourteenth meetings were
read and approved. The report of the Secretaries was read, in their
absence, by the temporary Secretary, and was approved. The
Treasurer's report, showing a balance on hand of $626.70, was read
and approved, as was also that of the auditing committee.
President—GARDINER G. HUBBARD.
Vice-Presidents—HERBERT G. OGDEN, [land]; GEORGE L. DYER,
[sea]; A. W. GREELY, [air]; C. HART MERRIAM, [life]; A. H.
THOMPSON, [art].
Treasurer—CHARLES J. BELL.
Recording Secretary—HENRY GANNETT.
Corresponding Secretary—GEORGE KENNAN.
Managers—CLEVELAND ABBE, MARCUS BAKER, ROGERS BIRNIE, JR., G.
BROWNE GOODE, W. B. POWELL, J. C. WELLING, C. A. KENASTON, O. H.
TITTMANN.
The paper of the evening was entitled, "The Great Plains of Canada,"
and was presented by Professor C. A. Kenaston, of Howard
University.
ARTICLE VI.
MEETINGS.
"The annual meeting for the election of officers shall be the last
regular meeting in December.
The following papers were then presented: "A Trip to Panama and
Darien," by Mr. R. U. Goode, and "Survey of Mason and Dixon's
Line," by Mr. Mark B. Kerr.
The paper of the evening was entitled, "House Life in Mexico," by Mr.
A. B. Johnson.
This meeting was devoted to papers upon the Samoan Islands. The
following programme was presented:
"The Home Life of the Samoans and the Botany of the Islands," by
Mr. W. E. Safford, U. S. N.
PROGRAM.
The work for which a prize is offered is to be a prose essay, a true
historic picture giving a just estimate of the grandeur of the occasion
to be celebrated.
So much has been written on this subject since the opening of the
XVIth century that it would seem difficult to say anything new and
good. Perhaps the details, perhaps the circumstances in the life and
acts of Columbus are worthy of no little research; but already the
Royal Academy of History is engaged in the erudite and diligent task
of bringing together and publishing the un-edited or little known
papers bearing on this question.
Well might Pedro Nuñes exclaim that from that time forth until the
form and size of the terraqueous globe were thoroughly known, the
most to be obtained would not be firmly established, "unless our
mariners sailed away better instructed and provided with better
instruments and rules of Astronomy and Geography than the things
with which cosmographers supplied them."
There may enter into this contest any unpublished work written to
this end in Spanish, Portuguese, English, German, French or Italian.
ebookbell.com