R- Programming Language
R language
R is a popular language for statistical computing and graphical presentation. It is most common use is to analyze and visualize data. R language created by Ross Ihka and Robert Gentlemen in 1993
Why use R?
- It is great resource for data analysis data visualization , data science and machine learning.
- It provide many Statistical technique (such as statistical test , classification , clustering and data reduction.
- It is easy to draw graph in R like pie charts , histogram , boxplot , scatter plot.
- It works on different platform (windows , MacOs , Linux)
- It is a open source
Comments
Comments starts with a # when executing code R will ignore anything that starts with #.
For multiline is also be used #.
Basic Data types
- Numeric - (10.3 , 55,756)
- Integer - (2L, 55L,300L) where the letter L denoted this an integer
- Complex - (8+9i, where "i" imaginary part
- Character - (s,t,u, String ) - ("K","R" is exciting", "FALSE '11.5"
- Logical ( Boolean - (True or False)
R divides the operators as following groups
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Miscellaneous operators
- Additional operator
- Subtraction operator
- Multiplication operator
- Modulus operator
- Increment operator
- Decrement operator
- Equal operator
- Not equal operator
- Greater than operator
- Less than operator
- Greater than or Equal operator
- Less than or Equal operator
- Logical And operator
- Logical OR operator
- Logical NOT operator
R has two types of Loops command
- While loops
- For loops
R Vectors
- Vectors
- Vectors length
- Access Vector
- Repeat Vector
- Generating sequenced Vector
Statistics Introduction
- Mean , Median and mode
- Minimum and Maximum Value
- Percentiles
- Variance and Standard Deviation
- Covariance and correlation
- Probability distribution
Nomenclature
of R variable
The following rules need to be kept in mind while naming
variable:
·
A valid variable name consists of a combination
of alphabets numbers, dot(.) and underscore (_) characters. Example: var.1_is
valid
·
Apart from the dot and underscore operation, no
other special character is allowed. Example: var$1 or var#1 both are invalid.
·
Variables can start with alphabets or dot
characters. Example: or var is valid.
·
The variable should not start with numbers or
underscore. Example 2var or _var is invalid.
·
If a variable starts with a dot the next thing
after the dot cannot be a numbers. Example: 3var is invalid.
Necessary
methods for variables
R provides some useful methods to performs operations on
variable. These methods are used to determine the data types of the variable,
finding a variable, deleting a variable, etc.
Following is some of the methods used to work on variables:
Class () function
This built-in function is used to determine the data types
of the variable provided to it. The variable to be checked is passed to this as
an argument and it prints the data types in return.
SYNTAX
Class (Variable)
Is () function
This built-in function is used to know all the present
variable in the workspace. This generally helpful when dealing with a large
number of variables at once and help prevents overwriting any of them.
SYNTAX
Is ()
rm () function
This is again a built-in function used to delete an unwanted
variable within your workplace. This helps clear the memory space allocated to
certain Variables that are not in use thereby creating more space for others.
The name of the variable to be deleted is passed as an argument to it.
SYNTAX
rm (variable)
Global
Variables:
Global variables are those variables that exist throughout
the execution of a program. It can be changed and accessed from any part of the
program.
·
They are available throughout the lifetime of a
program.
·
They are declared anywhere in the program.
·
They are declared anywhere in the program
outside all of the function or blocks.
Declaring the global variable
# R program to illustrate
# Usage of global variables
# global variable accessed from
# within a function
display = function () {
print(global)
}
display ()
# changing value of global variable
global = 10
display ()
Output:
[1] 5
[1] 10
Local Variable:
Local variable are those variables
that exist only within a certain part of a program like a function and are
released when the function call ends. Local variables do not exist outside the
block in which they are declared, i.e., they cannot be accessed or used outside
that block.
Declaring Local variables:
# R program to illustrate
# Usage of local variables
func = function ( ) {
# this variable is local to the
# function func () and cannot be
# accessed outside this function
age = 19
print (age)
}
cat ( Age is :\)
func ()
·
Output:
[ 1] 18
Taking Input from
User in R programming
·
Using readline () method
·
Using scan () method
Using R language readline ()
method takes input in string format. If one inputs an integer then it inputted
as a string, let’s say, one wants to input 255, then it will input as “255”,
like a string. So one need to convert that inputted value to the format that he
needs. In this case, string. So, one needs to convert that imputed value to the
format that he needs. In this case, string “255” is converted to integer 255.
To convert the imputed value to the desired data type, there are some functions
in R,
·
as. Integer(n)
;-> convert to integer
·
as. numeric(n)
;-> convert to numeric type
(float, double etc.)
·
as. complex(n);-> convert to complex number (i.e
3+2i)
·
as. Date(n)
;-> convert to date …, etc.
Syntax:
Var = reading ();
Var = as. integer(var)
Note that one can use “<- “
Instead of “- “
Using scan () method
Another way to take input in r
language is using a method, called scan () method. This method takes input from
the console. This method is a very handy method while inputs are needed to
taken quickly for any mathematical calculation or for any dataset. This method reads
data in the form of a vector or list. This method also uses to reads from a
file also.
Syntax:
X= scan ()
Scan() method is taking input
continuously, to terminate the input process, need to press Enter key 2 times
on the console.
Comments
Post a Comment