Tuesday, 24 July 2018

Fundamental Concepts of R Language

 Variables in R

A variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.

Data Operators

1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Special Operators

1. Arithmetic Operator

    (" + ") → Add two operands or unary plus.
                             >> 2+3
                              5
                              >>+2
    (" - ") → Subtract two operands or unary subtract.
                             >> 3-1
                              2
                              >>-2
    (" * ") → Multiply two operands
                             >> 2*3
                              6
    (" / ") → Divide left operand with the right and results is in float.
                               >> 6/3
                              2.0
    (" ^ ") → Left operand raised to the power of right
                               >> 2^3
                               8
    (" %% ") → Remainder of the division of left operand by the right
                               >>5%%2
                               1
     (" %/% ") →Division that results into whole number adjusted to the left in the number line.
                              >> 7%/%3
                              2
                            
2. Assignment Operators

     (" = ") →  x = <right operand>
                  >>x=5
                  >>x
                   5
     (" <- ") → x <- <right operand>
                    >>5<-15
                     >> x
                      15
     (" <<- ") → x<<-  <right operand>
                       >> x<<-2
                        >> x
                         2
     (" -> ") → <left operand> -> x
                    >> 25 -> x
                      >> x
                       25

3. Relational Operators
     
      (" > ") → True if left operand is greater than the right
                              >> 2>3
                                  False
       (" < ") → True if left operand is less than the right
                               >> 2>3
                                    True
       (" == ") → True if left operand is equal to right
                               >> 2==2
                                     True
        (" != ") → True if left operand is not equal to the right
                                 >> x >>=2
                                  >> print(x)
                                   1
        (" >= ") → True if left operand is greater than or equal to the right operand
                                  >> 2 >=3
                                    False
        (" =< ") → True if left operand is less than or equal to the right operand
                                  >> 2 =<3
                                    True

4. Logical Operators

         (" & ") → Returns x if x is False , y otherwise
                                  >> 2 &3
                                    3
         (" | ") → Returns y if x is False, x otherwise
                                   >> 2|3
                                    2
         (" ! ") → Returns True if x is True, False otherwise
                                   >> !1
                                    False

5. Special Operators
       
         (" : ") → It creates the series of numbers in sequence for a vector
                                      >> x <- 2:8
                                       >> x
                                       [1] 2 3 4 5 6 7 8
         (" %in% ") → This operator is used to identify if an element belongs to a vector
                                        >> x <-2:8
                                        >> y <- 5
                                        >>y %in% x
                                         True

Data Type
 We do not need to declare a variables before using them.
    

Vectors :-
   A Vector is a sequence of data elements of data elements of the same basic type.
      Example :
                 vtr = (1,3,5,7,9)
                  or
                  vtr <- (1,3,5,7,9)
  There are 5 Atomic vectors, also termed as five classes of vectors.

Lists :-

  Lists are the R objects which contain elements of different types like -numbers, strings, vectors and another list inside it.
    > n = c(2,3,5)
    > 5 = c("aa", "bb", "cc", "dd", "ee")
    >x = list(n, s, TRUE)

Arrays :-

Arrays are the R data objects which can store data in more than two dimensions.
It takes vectors as input and uses the values in the dim parameter to create an array.
       vector 1 <- c(5,9,3)
        vector2 <- c(10,11,12,13,14,15)
  result <- array(c(vector1, vector2), dim = c(3,3,2))

Matrices :-

 Matrices are the R objects in which the elements are arranged in a two-dimensional rectangular layout.
A Matrix is created using the matrix() function.
  matrix(data, nrow, ncol, byrow, dimnames)

- data is the input vector which becomes the data elements of the matrix.
- nrow is the number of rows to be created
- ncol is the number of columns to be created.
- byrow is a logical clue. If TRUE then the vector elements are arranged by row.
- dimname is the names assigned to rows and columns.

Factors:-

Factors are the data objects which are used to categorize the data and store it as levels
They can store both strings and integers.
They are useful in data analysis for statistical modeling.

   data <- c("East","West","East","North","North","East","West","West","East")
                   factor_data <- factor(data)

Data Frames :-

  A data frame is a table or two-dimensional array-like structure in which each column contains values of one variable and each row contains one set of values from each column.
   emp_id = c(1:5),
emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c(623.3,515.2,611.0,729.0,843.25),
 emp.data <- data.frame(emp_id, emp_name, salary)

Flow Control Statements

if → It evaluates a single condition
if .. else → It evaluates a group of condition and selects the statements
Switch → It checks the different known possibilities and selects the statements 


Loops :-

Repeat → Repeat things until the loop condition is true
While → Repeat things until the loop condition is true
For → Repeat things till the given number of times.



0 Comments:

Post a Comment

Popular Posts

Categories

Android (21) AngularJS (1) Books (3) C (75) C++ (81) Data Strucures (4) Engineering (13) FPL (17) HTML&CSS (38) IS (25) Java (85) PHP (20) Python (83) R (68) Selenium Webdriver (2) Software (13) SQL (27)