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 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.
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)
> x <- 10
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.
- To find all the variables currently available in the workplace, we use “ls() function”.
- Ls(pattern=—) to specify variable names.
- >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).
We can remove variables from memory and therefore permanently delete them using ‘rm()’ or ‘remove()’.
- x #warning:object not found
- Remove(x,y) #delete multiple objects
- remove(list=ls(pattern=’^b’)) #remove variables starts with ‘b’.
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 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’.
Testing whether a variable is numeric or not we use a function called is.numeric().
- 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().
- Complex Data Type:
Complex values for coding in R can be defined using the pure imaginary values(i).
- Logical Data Type:
A logical value is mostly created when comparison between variables are done.
- 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.
- 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().
Vectors are the most basic R data objects which are further divided into 5 types. They are as follows:
- 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.
>length(x) # To find the length of the vector.
>nchar(x) # To find each character of a vector.
 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 ‘*’.
 3 6 9 12 15
 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!