R Programming
R Programming
# R Programming :-
•R is a popular programming language used for statistical computing and graphical presentation.
•Its most common use is to analyze and visualize data.
•It is a great resource for data analysis, data visualization, data science and machine learning
•It provides many statistical techniques (such as statistical tests, classification, clustering and data reduction)
•It is easy to draw graphs in R, like pie charts, histograms, box plot, scatter plot, etc++
•It works on different platforms (Windows, Mac, Linux)
•It is open-source and free
•It has a large community support
•It has many packages (libraries of functions) that can be used to solve different problems
#Variables:-
•Variables are containers for storing data values.
•R does not have a command for declaring a variable. A variable is created the moment you first assign a value to it. To assign a value to
a variable, use the<-sign.
•You can also concatenate, or join, two or more elements, by using thepaste() function.
•To combine both text and a variable, R uses comma.
#variable names in R:
A variable name must start with a letter (e.g., age, name).
It can include letters, numbers, periods (.), and underscores (_), but not start with a number or underscore.
If a name starts with a period (.), it cannot be followed by a digit.
Variable names are case-sensitive. For example, age, Age, and AGE are three different variables.
Reserved words (like TRUE, FALSE, NULL, if, etc.) cannot be used as variable names.
In short, make sure your variable name:
Starts with a letter,
Uses letters, numbers, periods, or underscores,
Avoids starting with a period followed by a number, and
Doesn’t use reserved words.
OR
•Variable Names
•A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for R variables are:Avariable name must
start with a letter and can be a combination of letters, digits, period(.)and underscore(_). If it starts with period(.), it cannot be followed by a digit.
•A variable name cannot start with a number or underscore (_)
•Variable names are case-sensitive (age, Age and AGE are three different variables)
•Reserved words cannot be used as variables (TRUE, FALSE, NULL, if...)
#example->
#Data Types :-
•In programming, data type is an important concept.
•Variables can store data of different types, and different types can do different things.
•In R, variables do not need to be declared with any particular type, and can even change type after they have been set.
•Basic data types in R can be divided into the following types:
•numeric-(10.5, 55, 787)
•integer-(1L, 55L, 100L, where the letter "L" declares this as an integer)
•complex-(9 + 3i, where "i" is the imaginary part)
•character(a.k.a. string) -("k", "R is exciting", "FALSE", "11.5")
•logical(a.k.a. boolean) -(TRUE or FALSE)
•theclass()function to check the data type of a variable
#Arithmetic Operators :-
Operator Name Example
+ Addition x + y
- Subtracti x -y
on
* Multiplic x * y
ation
/ Division x/y
^ Exponen x ^ y
t
%% Modulus x %% y
(Remain
der from
division)
%/% Integer x%/%y
Division
1.q <-12
2.w <-23
3.print(q+w)
4.print(q-w)
5.print(q*w)
6.print(q/w)
7.print(q%%w)
8.print(q%/%w)
9.print(2^5)
#Assignment Operators :-
1.a <-3
2.b <<-3
3.3 -> c
4.3 ->> d
5.a
6.b
7.c
8.d
-------
•We can create a vector that combines numerical values
•To create a vector with numerical values in a sequence, use the:operator.
#Repeat Vectors :-
•The rep() function is use to repeat vectors.
•The rep() function has following parameters:-
•Each -Repeat each value.
•Times -Repeat the sequence of the vector or Repeat each value independently with help of a vector.
#Lists :-
•A list in R can contain many different data types inside it. A list is a collection of data which is ordered and changeable.
•To create a list, use the list() function.
•You can access the list items by referring to its index number, inside brackets. The first item has index 1, the second item has index 2, and so on.
•To change the value of a specific item, refer to the index number.
#List Length :-
•To find out how many items a list has, use the length() function.
•Use the dim() function to find the number of rows and columns in a Matrix.
•Use the length() function to find the dimension of a Matrix.
#Data Frames:-
•Data Frames are data displayed in a format as a table.
•Data Frames can have different types of data inside it. While the first column can be character, the second and third can be numericorlogical. However,
each column should have the same type of data.
•Use the data.frame() function to create a data frame.
•We can use single brackets[ ] , double brackets[[ ]] or $ to access columns from a data frame.
•Use the summary() function to summarize the data from a Data Frame.
#Add Rows and Columns:-
•Use the rbind() function to add new rows in a Data Frame.
•Use the cbind() function to add new columns in a Data Frame.
#Remove Rows and Columns:-
•Use the dim() function to find the amount of rows and columns in a Data Frame .
•You can also use the ncol() function to find the number of columns and nrow() to find the number of rows .
•Use the length() function to find the number of columns in a Data Frame (similar to ncol() ).