**Introduction to R:-**

R is a scripting language for statistical data manipulation and analysis. R provides a variety of statistical and graphical techniques, and it is highly extensible. R is available as free software. R is easy and friendly to new programmers. R compiles and runs on a wide variety of operating systems. Programming in R is an open-source statistical environmental model after S and S. The S language was developed in the late **1980ās** at **AT&T laboratories**.

The R project was started by ā**Robert Gentleman**** and ****Ross Ihakaā **of the **statistics department** of the** University of Auckland** in **1995**. Hence the name ā**R**ā**. **The R has become more popular than S and S-plus because it is free and more people are contributing to it. R is sometimes called āGNUā.

**Why do we use R for our Statistical work?**

R is a scripting language which is inexpensive and beautiful. R is a public domain implementation of the widely regarded āSā-Statistical language. R is available for windows, LINUX, and MACās. In addition to enabling statistical operation, it is a general programming language. So that we can automate our analysis and create new functions. Object oriented and functional programming structure, our data set and save between sessions. So , we donāt have to reload each time. Open software nature means it is easy to get help from the user community and lots of new functions get contributed by the user. Many of which are prominent statisticians.

**ADVANTAGES OF Programming in R:-**

- R is Clearer, more compact code.
- It is potentially much faster execution speed.
- It is less debugging. Since we write less code.
- R is an easier transition to parallel programming.

**VARIABLES: –**

Variables provide us with named storage that are programmes can manipulate. A variable can store an atomic vector, group of atomic vectors or combination of many R objects. A valid variable name consists of alphabets, numbers, dot(.) and underscore(_). The variable name starts with the letter or dot(.).

Example:- var.1, .var1, var_name

The variable name started with dot(.) should not followed by a number

Example:- 1) var.1, var1 are valid.

2) .1Variable is invalid.

**Assignment:- **

The assignment is used to assign the values to the variables. It can be assigned in 3 ways. They are as follows:

- Variable-name <- value ( leftward operator)
- Variable-name = value (assignment operator)
- Value -> variable-name (rightward operator)

Examples:-

> x <- 10

> x

[1] 10

> y=20

> x+y

[1] 30

>print(x)

[1] 10

> print(x+y)

[1] 30

**Data type of a variable:- **

In programming in R, a variable itself is not declared of any data type, rather it gets the data type of the āRā object assigned to it. So, R is called a Dynamically** typed language. **Which means, it can change the variables data type of the same variable again and again while using it in a program.

Example:-

- >x<-āhariā
- >class(x)

[1] ācharacterā

- >x=10
- >class(x)

[1] ānumericā

- To find all the variables currently available in the workplace, we use ā
**ls() functionā**. - Ls(pattern=—) to specify variable names.

Example:-

- >ls(pattern=ā^vā) to get variable names starting with āvā (e.g:Var,Very,–).
- >ls(pattern=āoā) then we display the whole word with āoā (e.g:Over,moni,–).
- >ls(pattern=ā^a|^bā) then we get all the words starting with āaā and ābā (e.g:Apple,Bat,Bhanu,–).
- ls(pattern=āa$ā) then we get all words ending with āaā (e.g: divya).

**Deleting Variables:-**

We can remove variables from memory and therefore permanently delete them using ā**rm()**ā or ā**remove()ā.**

**Syntax:- **rm(list)

Remove(list)

**Example:- **

- rm(x)
- x #warning:object not found
- Remove(x,y) #delete multiple objects
- remove(list=ls(pattern=ā^bā)) #remove variables starts with ābā.

**Data Types:- **

There are several basic data types in R which are of frequent occurrence in coding R calculation and programs. There are five frequently used data types are as follows:

- Numeric
- Integer
- Logical
- Complex
- Character
- Numeric Data Type:

The most commonly used data type is numeric. This is similar to float or tuple in other languages. It handles integers, decimals, +ve, -ve, including ā0ā.

Example: >g=6.25

Testing whether a variable is numeric or not we use a function called **is.numeric()**.** **

**e.g: >**is.numeric(g)

** **[1] TRUE

**Integer Date Type:**

If we want to create any integer variable in āRā we have to invoke the **as.Integer(). **We can be certain that a variable is definitely an integer by applying the **is.Integer()**.

**e.g: **>x=15

>s=as.Integer(x)

>s

[1] 15

> class(s)

[1] āIntegerā

>is.integer(x)

[1] TRUE

>y=as.integer(3.15)

>y

[1] 3

**Complex Data Type:**

Complex values for coding in R can be defined using the pure imaginary values(i).

**e.g:** >k=3+5i

** >**k

** **[1]3+5i

**Logical Data Type:**

A logical value is mostly created when comparison between variables are done.

**e.g: **>a=5;b=6

** **>g=a>b

>g

[1] FALSE

**Character Data Type:**

** **The Character data type is a string type data which is very common in statistical analysis. R as 2 primary ways of handling character data.

a) character

b) factor

**e.g: **>x=ādataā

** **>x

[1] ādataā

>y=factor(x)

>y

[1] data

levels: data

- Notice that āxā contains the word data encapsulated with ā ā and āyā as the word data without code and second line information is about the levels of āyā.
- For finding the length of the character data or Numeric data we use the function
**nchar()**.

**e.g: **>x=ādataā

>nchar(x)

[1] 4

>nchar(12345)

[1] 5

**Vectors:-**

Vectors are the most basic R data objects which are further divided into 5 types. They are as follows:

1) Character

2) Complex

3) Integer

4) Ddouble

5) Logical

- A vector is a collection of elements all of the same type for instance.
- C(1,2,3,4,5) is a vector consisting of numbers 1,2,3,4,5. Similarly, c(āRā, ācā, āc++ā)
- A vector cannot be a mixed type.
- Vector plays a crucial and helpful role in R.
- Vectors do not have a dimension. i.e: there is no such column vector or row vector.
- Vectors are generally created using the c function, the ācā stands for combine or concatenate because multiple elements are being combined into a vector.
- A vector type can be checked with the type of function.

**e.g: **>typeof(x) # To find the data type of vector.

** **[1] ācharacterā

>length(x) # To find the length of the vector.

[1] 3

>nchar(x) # To find each character of a vector.

[1] 1 1 3

**Operations on Vectors:- **

Now, that we have a vector of first numbers of might want to multiply each element by 3 in R this is the simple operation using just multiplication operator ā*ā.

**e.g:** >x<-c(1,2,3,4,5)

>x*3

[1] 3 6 9 12 15

>x+3

[1] 3 5 6 7 8

This brings us to the end of the blog on Programming in R. We hope that you have understood the concepts of programming in R clearly and comprehensively. Happy Learning!Ā