0% found this document useful (0 votes)
16 views28 pages

R Programming Basics Overview

The document provides an introduction to R programming, covering its basic syntax, built-in functions, user-defined functions, and control flow statements. It discusses the capabilities of R software for statistical computing and data manipulation, along with its history and features. Additionally, it outlines various mathematical and statistical functions available in R, making it a powerful tool for scientific computing.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views28 pages

R Programming Basics Overview

The document provides an introduction to R programming, covering its basic syntax, built-in functions, user-defined functions, and control flow statements. It discusses the capabilities of R software for statistical computing and data manipulation, along with its history and features. Additionally, it outlines various mathematical and statistical functions available in R, making it a powerful tool for scientific computing.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Basics of R programming

Vijay Kumar

Department of Mathematics and Statistics


DDU Gorakhpur University, Gorakhpur
vkgkp@[Link]

November 2022
Introduction Basics of R

R Software:

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 2 / 28


Introduction Basics of R

Outline

1 Introduction
2 The R programming language’s basic syntax (function)
3 Function
4 Built-in functions/commands
5 Writing function(User defined function)
6 Loops and control flow statements
Decision control(if, if...else etc.)
Loop
7 Basic statistical functions
8 Use of help function.

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 3 / 28


Introduction Basics of R

Computational problems:

Numerical differentiation and integration,


Numerical optimization,
Matrix algebra
Solution of systems of linear algebraic equations and nonlinear equations,
Interpolation and Curve fitting,
Solution of ordinary differential equations and partial differential equations
Computationally intensive Monte Carlo methods
Higher dimension integration (Markov Chain Monte Carlo)
Fourier analysis and Special functions etc.

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 4 / 28


Introduction Basics of R

R Software

Multi-paradigm: functional, imperative, object-oriented, reflective


Designed by Ross Ihaka and Robert Gentleman
Developer(s) : R Core Team
Initial release : 1996; 26 years ago
Stable release :4.2.2 ;October 2022
OS : Windows, macOS, Linux
Type : Language and environment for statistical computing
License : GPL
Website : [Link]

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 5 / 28


Introduction Basics of R

R software:

Created by Ross Ihaka Created at


and Robert Gentleman Auckland New Zealand

Named after first name Project considered


of first 2 authors in 1992

I iti l version
Initial i released
l d in
i
Stable beta version in 2000
1995
Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 6 / 28
Introduction Basics of R

R software contd...

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 7 / 28


Introduction Basics of R

Ross Ihaka and Robert Gentleman started at the University of Auckland, New Zealand, to
provide a statistical computing environment in their teaching lab.

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 8 / 28


Introduction Basics of R

Free Software

Free software is a matter of the users’ freedom to run, copy, distribute, study, change and
improve the software. More precisely, it refers to four kinds of freedom, for the users of the
software:
The freedom to run the program, for any purpose.
The freedom to study how the program works, and adapt it to our needs.
The freedom to redistribute copies so one can help.
The freedom to improve the program, and release the improvements to the scientific
community.
Access to the source code is a precondition for this.

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 9 / 28


Introduction Basics of R

Why R?

R combines remarkable power with clean, simple and easy-to-understand syntax.


Several robust packages have been written in R makes it a natural choice for scientific
computational tasks.
It provides print quality graphics.
It represents a philosophy of turning ideas into programs quickly and easily.
Object-oriented programming (OOP) makes R easy and flexible!
R is a powerful tool for scientific computing, graphics, and statistical program-
ming.

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 10 / 28


Introduction Basics of R

What is R

R (Ihaka and Gentleman 1996) is a free, cooperatively developed, open-source implemen-


tation of S, a powerful and flexible statistical programming language and computing envi-
ronment
A system for statistical analysis and graphics
Works in various OS(Windows/Unix/Linux/Mac)
Free available at [Link]
An interpreted language → easy to program
Promotes reproducible research by providing open and accessible tools.
Most of R is written in R! This makes it quite easy to see what functions are actually
doing

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 11 / 28


Introduction Basics of R

R environment cont...

R is an integrated suite of software facilities for data manipulation, calculation and graphical
display. Among other things it has
an effective data handling and storage facility,
a suite of operators for calculations on arrays, in particular matrices,
a well developed, simple and effective programming language which includes
conditionals, loops, user defined recursive functions and input/output facilities.

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 12 / 28


Introduction Basics of R

Where to get R?

1 Go to [Link]
2 Downloads: CRAN(Comprehensive R Archive Network)
3 Set your Mirror: Anyone is fine.
4 Select Windows.
5 Select base.
6 Select [Link]

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 13 / 28


Introduction Basics of R

R environment cont...

Learning any new language requires

an initial investment of time to internalize the syntax and

intuition behind its structure.

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 14 / 28


Introduction Basics of R

Basic Data Representation

Data Types
TRUE, FALSE logical true and false
1, 2.5, 117.333 simple numbers
1.23e20 scientific notation, 1.23 x 1020
3 + 4i complex numbers
”hello, world” a character string
NA missing value (in any type of vector)
NULL missing value indicator in lists
NaN not a number
Inf positive infinity
-Inf negative infinity
‘var‘ quotation for special variable name

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 15 / 28


Introduction Basics of R

Special Characters & Values


Special Characters
<- assignment statement (also allowed: =, ->)
[] Indexing of arrays, matrices, dataframes, lists
() encloses function input variables
{} embraces statements (e.g. loops, function definition, if)
... unspecified function input variables
; separates statements written on a single line
# comment(R will ignore everything after the symbol #)
$ extracting elements from lists, data frames
pi value of π

Example
> x <- 4 # assign the number 4 to the object x
> y <- sqrt(x) # assign square root of x to new object y
>x # print value(s) of x

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 16 / 28


Introduction Basics of R

Basic Arithmetic Operators


Basic Arithmetic Operations Example
x +y addition > x <- c(1,3,4,6)
x -y subtraction > y <- c(1,1,2,1)
x /y division > x + y
x ^y exponentiation [1] 2 4 6 7
x %% y remainder > x - y
x %/% y integer division [1] 0 2 2 5
> x * y
Example [1] 1 3 8 6
> x <- 5 ; y <- 4 > x ^ y
> x ^ y [1] 1 3 16 6
[1] 625 > x / y
> x / y [1] 1 3 2 6
[1] 1.2 > x %% y # remainder
> x %% y # remainder [1] 0 0 0 0
[1] 1

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 17 / 28


Introduction Basics of R

Logical & Relational Operators

The logical operators all return logical values.


< less than
<= less or equal
== equal
!= not equal
> greater than
>= greater or equal
! Logical NOT
& Logical AND
| Logical OR
&& & Expression TRUE for all elements in vector
|| | Expression TRUE for all elements in vector

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 18 / 28


Introduction Basics of R

Mathematical Functions
Rounding
round(x) x to nearest integer
round(x,d) x to d decimal places
floor(x) down to next lowest integer Example
ceiling(x) up to next highest integer > x <- 3.1456
> round(x)
Example [1] 3
> x <- c(5.1456, 4.5432, pi) > round(x,2)
> round(x) [1] 3.15
[1] 5 5 3 > floor(x)
> round(x, 2) [1] 3
[1] 5.15 4.54 3.14 > ceiling(x)
> floor(x) [1] 4
[1] 5 4 3
> ceiling(x)
[1] 6 5 4
Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 19 / 28
Introduction Basics of R

Sequences & Sorting


a: b Example
sequence from a to bin steps of size 1 > 1:5
[1] 1 2 3 4 5
seq(n)
> seq(1,5)
same as 1 : n
[1] 1 2 3 4 5
seq(a, b) > seq(5)
same as a : b [1] 1 2 3 4 5
seq(a, b, by = s) > seq(1, 2, by = 0.25)
a to b in steps of size s [1] 1.00 1.25 1.50 1.75 2.00
seq(a, b, length = n) > seq(1, 2, length = 5)
sequence of length n from a to b [1] 1.00 1.25 1.50 1.75 2.00
sort(x) > x <- c(3,1,4,9,2,7,5)
sort into ascending order > sort(x)
[1] 1 2 3 4 5 7 9
sort(x, decreasing=TRUE)
> sort(x, decreasing=TRUE)
sort into descending order
[1] 9 7 5 4 3 2 1
Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 20 / 28
Introduction Basics of R

Mathematical Functions
Example
> x <- c(2,3)
> sqrt(x)
Common Mathematical Functions [1] 1.414214 1.732051
abs(x) absolute values > log(x)
sqrt(x) square root [1] 0.6931472 1.0986123
exp(x) exponential function > exp(x)
log(x) natural logarithms [1] 7.389056 20.085537
log10(x) common logarithms > log10(x)
log2(x) base 2 logarithms [1] 0.3010300 0.4771213
log(x,base=b) base b logarithms > log2(x)
[1] 1.000000 1.584963
> log(x,base=3)
[1] 0.6309298 1.0000000

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 21 / 28


Introduction Basics of R

Special Mathematical Functions


Example
Functions > choose(5, 3)
[1] 10
beta(x,y) beta function
> factorial(3)
lbeta(x,y) log beta function
[1] 6
gamma(x) gamma function
> lfactorial(3)
lgamma(x) log gamma function
[1] 1.791759
psigamma(x,deriv=0) psigamma function
> x<-c(5, 4)
digamma(x) digamma function
> y<-c(3,2)
trigamma(x) trigamma function
> choose(x, y)
Combinatorics
[1] 10 6
choose(n, k) binomial coefficients
> factorial(x)
lchoose(n, k) log binomial coefficients
[1] 120 24
factorial(x) factorials
> gamma(0.5)
lfactorial(x) log factorials
[1] 1.772454

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 22 / 28


Introduction Basics of R

Trigonometric and Hyperbolic Functions

Example
Functions > x <- c(pi/3, pi/6)
> y <- sin(x)
Trigonometric functions
> y
sin(x), cos(x), tan(x)
[1] 0.8660254 0.5000000
Inverse trigonometric functions
> z <- asin(y)
asin(x), acos(x), atan(x)
> x
Hyperbolic functions
[1] 1.0471976 0.5235988
sinh(x), cosh(x), tanh(x)
> z
Inverse hyperbolic functions
[1] 1.0471976 0.5235988
asinh(x), acosh(x), atanh(x)
> k <- sinh(x)
arc tangent with two arguments
> asinh(k)
atan2(x,y)
[1] 1.0471976 0.5235988

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 23 / 28


Introduction Basics of R

Statistical Summaries
Statistical Summaries Example
mean(x) mean of elements > x <- c(1,2,3,6,4)
sd(x) standard deviation > mean(x)
var(x) variance of elements [1] 3.2
median(x) median of elements > sd(x)
quantile(x) quartiles and extremes [1] 1.923538
quantile(x, p) specified quantiles > length(x)
min(x),max(x) minimum, maximum of x [1] 5
summary(x) summary statistics of x > median(x)
sum(x),prod(x) sum and product of x [1] 3
cumsum(x) cumulative sum of x > min(x)
cumprod(x) cumulative product of x [1] 1
cov(x,y) variance-covariance matrix > max(x)
cor(x,y) correlation matrix [1] 6

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 24 / 28


Introduction Basics of R

User defined function:

These functions in R programming language are declared, and defined by a user according to the
requirements, to perform a specific task.
A function is a block of organized and reusable code that is used to perform a specific task in a
program.
A function is created by using the function keyword.
The basic syntax of a function is:

Func_Name <‐ function(arg1, arg2) {


unction(arg1, arg2) {
Function Body
return(varname)
}

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 25 / 28


Introduction Basics of R

User defined function:

FunctionName is the name of a function that is stored as an R object.


Arguments are used to provide specific inputs to a function while a function is invoked. A
function can have zero, single, multiple, or default arguments.
Function Body contains the block of code that performs the specific task assigned to a function.
Return Value is the value that a function returns when it succeeds in performing the task. We
can also make a function with no return value.

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 26 / 28


Introduction Basics of R

User defined function:

Sphere <- function(radius)


{
volume <- 4/3 * pi * radius ^ 3
return(volume)
}
>Sphere(5)
[1] 523.5988

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 27 / 28


Introduction Basics of R

UDF contd...

Sphere <- function(radius)


{
volume <- 4/3 * pi * radius ^ 3
surface <- 4 * pi * radius ^ 2
return(list(volume=volume, surface=surface))
}
>Sphere(5)
$volume
[1] 523.5988

$surface
[1] 314.1593

Vijay Kumar, Gorakhpur University R Programming: Basics(Part-I) November 2022 28 / 28

You might also like