0% found this document useful (0 votes)
2 views22 pages

R Programming

R programming Note

Uploaded by

radhika.jejurkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views22 pages

R Programming

R programming Note

Uploaded by

radhika.jejurkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 22

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

#Built-in Math Functions :-


•R also has many built-in math functions that allows you to perform mathematical tasks on numbers.
#Strings :-
•Strings are used for storing text.
•A string is surrounded by either single quotation marks, or double quotation marks.

#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

Operator Description Example


: Creates a x <-1:10
series of
numbers in a
sequence
%in% Find out if an x %in% y
element
belongs to a
vector
%*% Matrix x <-Matrix1
Multiplication %*% Matrix2
1.x <-1:10
2.print(x)
3.print(1 %in% x)
#R Vector :-
•A vector is simply a list of items that are of the same type.
•To combine the list of items to a vector, use the c() function and separate the items by a comma.

-------
•We can create a vector that combines numerical values
•To create a vector with numerical values in a sequence, use the:operator.

#Vector Length & Sort :-


•To find out how many items a vector has, use the length() function.
•To sort items in a vector alphabetically or numerically, use the sort() function.
#Access Vectors :-
•The vector items can be accessed by referring to its index number inside brackets []. The first item has index 1, the second item has index 2, and so on.
•Multiple elements can be accessedby referring to different index positions with the c() function.
•Also, negative index numbers is use to access all items except the ones specified.

#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.

#Generating Sequenced Vectors :-


•One of the examples on top, showed you how to create a vector with numerical values in a sequence with the : operator
•To make bigger or smaller steps in a sequence, use the seq()function
•The seq()function has three parameters:
From is where the sequence starts , to is where the sequence stops, and by is the interval of the sequence.

#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.

#Check if Item Exists:-


•To find out if a specified item is present in a list, use the %in% operator.
#Add List Items
•To add an item to the end of the list, use the append() function .
•To add an item to the right of a specified index, add "after=index number" in the append() function .

#Remove List Items :-


•You can also remove list items. The following example creates a new, updated list without an "apple" item .
•You can specify a range of indexes by specifying where to start and where to end the range, by using the : operator.
•You can loop through the list items by using a for loop.

#Join Two Lists :-


There are several ways to join, or concatenate, two or more lists in R.
The most common way is to use the c() function, which combines two elements together.
#Matrices :-
•A matrix is a two dimensional data set with columns and rows.
•A column is a vertical representation of data, while a row is a horizontal representation of data.
•A matrix can be created with the matrix() function. Specify the nrow and ncol parameters to get the amount of rows and columns .
•the c() function is used to concatenate items together.
#Access Matrix Items:-
•You can access the items by using[ ]brackets. The first number "1" in the bracket specifies the row-position, while the second number "2" specifies the
column-position.
•The whole row can be accessed if you specify a comma after the number in the bracket.
•The whole column can be accessed if you specify a comma before the number in the bracket.
•More than one row or column can be accessed if you use the c() function.

#Add Rows and Columns:-


•Use the cbind() function to add additional columns in a Matrix .
•Use the rbind() function to add additional rows in a Matrix.
#Remove Rows and Columns:-
#Check if an Item Exists in matrix :-
•To find out if a specified item is present in a matrix, use the %in% operator.

•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() ).

You might also like