Thursday 16 August 2018

Basic of Calculations _Functions_Matrices in R Language

Function :-

Function are a bunch of commands grouped together in a sensible unit.

Functions take input arguments, do calculations (or make some graphics, call other functions) and produce some output and return a result in a variable. The returned variable can be a complex construct, like a list.

Syntax 

Name <- function(Argument1, Argument2, ...)
                {
                   expression
                                     }
Where expression is a single command or a group of commands
  • Function arguments can be given a meaningful name
  • Function arguments can be set to default values
  • Functions can have the special argument '...'
Functions (Single variable)

The sign <- is furthermore used for defining functions:
> abc <- function(x) {
                    x^2
                         }
> abc (3)
  [1]  9

>abc (6)
  [1]  36

> abc (-2)
  [1]   4






Function (Two variables)

>abc  <- function (x,y) {
               x^2+y^2
                      }
> abc (2,3)
   [1]  13
> abc (3,4)
    [1]  25
> abc  (-2,-1)
   [1]  5



Matrix
  • Matrices are important objects in any calculation.
  • A matrix is a rectangular array with p rows and n columns.
  • An element in the i-th row and j-th column is denoted by xij (book version) or x[i,j] ("program version"), i = 1,2,.....,n, j = 1,2,...,p. 
  • An element of a matrix can also be an object, for example a string. However, in mathematics, we are mostly interested in numerical matrices, whose element are generally real numbers
In R, a 4⤫2-matrix x can be created with a following command:

>x <- matrix (nrow = 4 , ncol = 2, data = c(1,2,3,4,5,6,7,8) )

We see:
  • The parameter nrow defines the row number of a matrix.
  • The parameter ncol defines the column number of a matrix.
  • The parameter data assigns specified values to the matrix element.
  • The value from the parameters are written column-wise in matrix.

>  x
              [,1]          [,2]
[1,]           1             5
[2,]           2             6
[3,]           3             7
[4,]           4             8
  • One can access a single element of a matrix with x[i,j] :
> x [3,2]
 [1]   7



0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (112) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (85) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (18) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (43) Meta (18) MICHIGAN (4) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (719) Python Coding Challenge (155) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses