You are on page 1of 57

Mathematics assignment

Learning R language

Submitted by, submitted


to,

Ashay Vishwanath K.P


14mn05 Assistant professor
NITK Surathkal. Dept of
mathematics
Nitk surathkal
INTRODUCTION: R is a language for statistical computing and graphics, data
miningetc, created by Ross Ihaka and Robert gentleman hence the name R
to this language.

Motivation: it is a very simple language with easy words as commands, it can


be very useful in my future.

Basics of R language syntax

To add numbers, we need to enter numbers inside print.


To print string, we need to use mystring command and enter the strings need to
be printed

Data types -Recognising the type

Logical- TRUE, FALSE.


Numeric-numbers

Integers- L
Complex numbers,
Character includes numbers and words

Any character is converted into its raw form using charToRaw


Creating a vector using command <-c() and then calling the vector which
created

Creating a list the elements of the are entered , when called it comes in the
form of lists .
Matrix can be created by defining its elements , and the required number of rows
and columns

Arrays are created by defining the dimensions and the elements.


Creating a vector, and defining the factor the vector , and then calling the factor
and by specifying the numbers of elements required to be shown.

Data frames are created by defining the different data like gender, height etc..
VARIABLES

Elements can be assigned to variables- calling these variables gives us the


elements .
Data type variables- it can be integer, numeric or character.

Printls is the command which shows us all the variables declared in the
environment.
Operators by defining elements to a variable , we can have any operations on
those elements

Addition +
Subtraction -

Multiplication *
Division /

Remainder %%
Subtracting a 1st vector by the quotient of 2 nd vector

1st Vector raised to powers of 2nd


Relational operators, compares two vectors and tells us if it is true or false
Logical operators

AND , OR, NOT operators


Assignment operators says if it has to be left or right assigned
Miscellaneous operators colon : represents series within example-2:50 , it shows
all numbers between 2 and 50
%in% command Identifies if the 2nd vector belongs to the given 1st vector

%*% - multiplies the matrix with its transpose


Decision making

If statement, if-else statement , switch statement


Loop types- Repeat loop, while loop, for loop -

Repeat loop just repeats for specified number of times

While- when a condition is satisfied then the looping occurs

For loop- looping occurs for only given conditions


Break statement breaks immediately and goes to next loop.
Next statement is similar to switch statement it goes to next command

Function components

Built in function like- sequence, mean, max,sum etc


User defined function user defines the functions based on the definition data is
processed

Calling function without argument


Argument calling a function by giving an argument ie., for

Calling a function with a default argument- no need to specify


Lazy evaluation evaluates when only needed by body function

Strings

Defining a string and calling that string


Strings inside mixed quotes- single and double

String manipulation concatenating joining different strings


Formatting functions by format() function digits, nsmall ,scientific .
Counting number of characters by nchar command

Converting letters to upper and lower case, by toupper and tolower command
Substring gives the character at certain position

Vector creation

Single element vector


Multiple elements vector

Using sequence(seq) , using colon (:)


Accessing vector elements

Logical, negative, 0/1 indexing


Vector manipulation

Vector arithmetic operations add.result, sub.result, multi.result, divi.result


Vector element recycling, same elements are repeated, vector sorting.

Creating a list using list_data function.


Creating a list with matrix and vector
Manipulating list
Unlisting the elements from list and then manipulating it

Creating a matrix by defining elements and number of rows and columns


Accessing the matrix and Operations of matrices
Creating matrices and operations applied to them
Arrays

Creating vectors these vectors are inputs to the array

Accessing the array elements


Manipulating the array elements- Calculations across array elements using x,
margin ,

Factor functions , store data as factors and then calling factors


Factors in data frame- data frame gives details

Changing the order levels by command of new_ordder_data , now new ordered


data is printed
Generating factor levels specifying the levels n, k, label.

Creating data frames we give all details and create a data frame
Extraction of data from data frame
Extracting specific column specifying requirements
Adding an extra column
Binding two column using rbind() command

Packages

Library contains packages which are available in default.

To know the packages we call them using library() function.


We can search for the packages by search() function

These are the available CRAN packages


Data reshaping

We can reshape the data like joining column and rows rbind(), cbind()

Merging the data by merge() functions


Melting and casting the data by melt() and cast() functions.

You might also like