R Operators – Arithmetic, Relational, Logical, Assignment

R Operators – Arithmetic, Relational, Logical, Assignment

R Operators – Arithmetic, Relational, Logical, Assignment R Tutorial – We shall learn about R Operators – Arithmetic, Relational, Logical, Assignment and some of the Miscellaneous Operators that R programming language provides. R Operators There are four main categories of Operators in R programming language. They are shown in the following picture : We shall learn about these operators in detail with Example R programs. R Arithmetic Operators Arithmetic Operators are used to accomplish arithmetic operations. They can be operated on the basic data types Numericals, Integers, Complex Numbers. Vectors with these basic data types can also participate in arithmetic operations, during which the operation is performed on one to one element basis. Operator Description Usage + Addition of two operands a + b – Subtraction of second operand from first a – b * Multiplication of two operands a * b / Division of first operand with second a / b %% Remainder from division of first operand with second a %% b %/% Quotient from division of first operand with second a %/% b ^ First operand raised to the power of second operand a^b An example for each of the arithmetic operator on Numerical values is provided below : r_op_arithmetic.R R Script File # R Arithmetic Operators Example for integers a# <R- A7r.i5thmetic Operators Example for integers b <- 2 par i<nt- (7 .a5+b ) #addition print ( a-b ) #bs <ub- t2raction p rint ( a*b ) #multiplication pprriinntt (( aa/+bb ) ) # #Daivdidsitoinon print ( a%%b ) #pRrienmt i(n dae-rb ) #subtraction pprriinntt (( aa%*b/% ) b# )m ultiplication #Quotient pprrinintt (( aa^/bb )) ##DPiovwiseiorn opfrint ( a%%b ) #Reminder print ( a%/%b ) #Quotient print ( a^b ) #Power of Output $ Rscript r_op_arithmetic.R [1] 9.5 [1] 5.5 [$1 ]R s1c5ript r_op_arithmetic.R [1] 3.75 [[11]] 19..55 [[11]] 35.5 [1] 56.25 [1] 15 [1] 3.75 [1] 1.5 [1] 3 [1] 56.25 An example for each of the arithmetic operator on Vectors is provided below : r_op_arithmetic_vector.R R Script File # R Operators - R Arithmetic Operators Example for vectors a# <R- Ocp(8e,r a9t,o r6s) - R Arithmetic Operators Example for vectors b <- c(2, 4, 5) par i<nt- (c (a8+,b 9 ), 6#)addition print ( a-b ) #bs <ub- tcra(2c,t io4n, 5) p rint ( a*b ) #multiplication pprriinntt (( aa/+bb ) ) # #Daivdidsitoinon print ( a%%b ) #pRrienmt i(n dae-rb ) #subtraction pprriinntt (( aa%*b/% ) b# )m ultiplication #Quotient pprrinintt (( aa^/bb )) ##DPiovwiseiorn opfrint ( a%%b ) #Reminder print ( a%/%b ) #Quotient print ( a^b ) #Power of Output $ Rscript r_op_arithmetic_vector .R [ 1] 10 13 11 [$1 ]R s6c 5ri p1t r_op_arithmetic_vector.R [1] 16 36 30 [[11]] 41.00 01 32 .1215 1.20 [[11]] 06 15 11 [1] 4 2 1 [[11]] 1 64 3 65 3601 7776 [1] 4.00 2.25 1.20 [1] 0 1 1 [1] 4 2 1 [1] 64 6561 7776 R Relational Operators Relational Operators are those that find out relation between the two operands provided to them. Following are the six relational operations R programming language supports.The output is boolean (TRUE or FALSE) for all of the Relational Operators in R programming language. Operator Description Usage < Is first operand less than second operand a < b > Is first operand greater than second operand a > b == Is first operand equal to second operand a == b <= Is first operand less than or equal to second operand a <= b >= Is first operand greater than or equal to second operand a > = b != Is first operand not equal to second operand a!=b An example for each of the relational operator on Numberical values is provided below : r_op_relational.R R Script File # R Operators - R Relational Operators Example for Numbers a# <R- O7p.5erators - R Relational Operators Example for Numbers b <- 2 par i<nt- (7 .a5<b ) # less than pbr in<t- (2 a>b ) # greater than print ( a==b ) # equal tpor int ( a<b ) # less than print ( a<=b ) # less tphrainnt o(r ae>qbu a)l #to greater than pprriinntt (( aa>===bb )) ## equal to greater than or equal tporint ( a<=b ) # less than or equal to pprriinntt (( aa!>==bb ) )# # n gorte ater than or equal to equal to print ( a!=b ) # not equal to Output $ Rscript r_op_relational.R [1] FALSE [1] TRUE [$1 ]R sFcArLipStE r_op_relational.R [1] FALSE [[11]] TFRAULESE [[11]] TTRRUUEE [1] FALSE [1] FALSE [1] TRUE [1] TRUE An example for each of the relational operator on Vectors is provided below : r_op_relational_vector.R R Script File # R Operators - R Relational Operators Example for Numbers # R Operators - R Relational Operators Example for Numbers a <- c(7.5, 3, 5) b <- c(2, 7, 0) print ( a<b ) # less than print ( a>b ) # greater than print ( a==b ) # equal to print ( a<=b ) # less than or equal to print ( a>=b ) # greater than or equal to print ( a!=b ) # not equal to Output $ Rscript r_op_relational_vector. R [1] FALSE TRUE FALSE [$1 ]R s TcrRipUtE rF_AoLpS_Ere laTtRioUnEal_vector.R [1] FALSE FALSE F[A1L] SFEALSE TRUE FALSE [[11]] FTARLUSEE F ATLRSUEE TFRAULESE [1] TRUE FALSE TRUE [[11]] TFRAULES ET RFUAEL STER UFAELSE [1] FALSE TRUE FALSE [1] TRUE FALSE TRUE [1] TRUE TRUE TRUE R Logical Operators Logical Operators in R programming language work only for the basic data types logical, numeric and complex and vectors of these basic data types. Operator Description Usage & Element wise logical AND operation. a & b | Element wise logical OR operation. a | b ! Element wise logical NOT operation. !a && Operand wise logical AND operation. a && b || Operand wise logical OR operation. a || b An example for each of the logical operators on Numerical values is provided below : r_op_logical.R R Script File # R Operators - R Logical Operators Example for basic logical elements # R Operators - R Logical Operators Example for basic logical elements a <- 0 # logical FALSE b <- 2 # logical TRUE a <- 0 # logical FALSE print ( a & b ) # logical AbN <D- e 2le #m elongt icwaisl eTRUE print ( a | b ) # logical OR element wise pprriinntt (( !aa &) #b )lo #gi cloagl ical AND element wise NOT element wise pprrinintt (( aa &|& b b ) )# # l ogical OR element wise lporginicta (l A!aN D) # logical NOT element wise consolidated for all eplerimnte n( tas && b ) # logical AND consolidated for all elements pprriinntt (( aa |||| bb )) ## logical OR consolidated for all elements logical OR consolidated f or all elements Output $ Rscript r_op_logical.R [1] FALSE [1] TRUE [1] TRUE [$1 ]R sFcArLipStE r_op_logical.R [1] TRUE [1] FALSE [1] TRUE [1] TRUE [1] FALSE [1] TRUE An example for each of the logical operators on Vectors is provided below : R Script File # R Operators - R Logical Operators Example for boolean v ectors # R Operators - R Logical Operators Example for boolean vectors a <- c(TRUE, TRUE, F ALSE, FALSE) ba <<-- cc((TTRRUUEE,, FTARLUSEE,, FALSE, FALSE) TRUE, FALSE) b <- c(TRUE, FALSE, TRUE, FALSE) print ( a & b ) # logical AND element wise pprriinntt (( aa |& bb )) ## lologgicicaal l AND element wise OR element wise pprrinintt (( !aa |) b# )lo #g icloagl ical OR element wise NpOrinTt e(l e!ma e)n #t wloigseical NOT element wise print ( a && b ) # lopgriincta l( AaN &D& b ) # logical AND consolidated for all elements cporinnsto (li daa t|e|d b f o) r# a lllo gical OR consolidated for all elements elements p rint ( a || b ) # logical OR consolidated for all elements Output $ Rscript r_op_logical_vector.R [1] TRUE FALSE FALSE FALSE $ Rscript r_op_logical_vector.R [1] TRUE FALSE FALSE FALSE [1] TRUE TRUE TRUE FALSE [1] FALSE FALSE TRUE TRUE [1] TRUE [1] TRUE R Assignment Operators Assignment Operators are those that help in assigning a value to the variable. Operator Description Usage = Assigns right side value to left side operand a = 3 <- Assigns right side value to left side operand a <- 5 -> Assigns left side value to right side operand 4 -> a <<- Assigns right side value to left side operand a <<- 3.4 ->> Assigns left side value to right side operand c(1,2) ->> a An example for each of the assignment operators is provided below : r_op_assignment.R R Script File # R Operators - R Assignment Operators a = 2 print ( a ) a <- TRUE print ( a ) 454 -> a print ( a ) a <<- 2.9 print ( a ) c(6, 8, 9) -> a print ( a ) # R Operators - R Assignment Operators a = 2 print ( a ) a <- TRUE print ( a ) 454 -> a print ( a ) a <<- 2.9 print ( a ) c(6, 8, 9) -> a print ( a ) Output $ Rscript r_op_assignment.R [1] 2 [1] TRUE [$1 ]R s4c5r4ipt r_op_assignment.R [1] 2.9 [[11]] 62 8 9 [1] TRUE [1] 454 [1] 2.9 [1] 6 8 9 R Miscellaneous Operators These operators does not fall into any of the categories mentioned above, but are significantly important during R programming for manipulating data. Operator Description Usage : Creates series of numbers from left operand to right operand a:b %in% Identifies if an element(a) belongs to a vector(b) a %in% b %*% Performs multiplication of a vector with its transpose A %*% t(A) An example for each of the Miscellaneous operators is provided below : r_op_misc.R R Script File # R Operators - R Misc Operators a = 23:31 p#r inRt O(p ae r)ators - R Misc Operators a = c(25, 27, 76) ba == 2273:31 print ( b %in% a ) print ( a ) M = matrix(c(1,2,3,4), 2, 2, TRUE) par i=nt c (( 2M5 ,% 2*7%, 7 t6()M) ) b = 27 print ( b %in% a ) M = matrix(c(1,2,3,4), 2, 2, TRUE) print ( M %*% t(M) ) Output $ Rscript r_op_misc.R [1] 23 24 25 26 27 28 29 30 31 [1] TRUE $ R s[c,1ri]p t[ ,r2_]op_misc.R [1,] 5 11 [[21,]] 2 31 12 4 25 26 27 28 29 30 31 [1] TRUE [,1] [,2] [1,] 5 11 [2,] 11 25 Conclusion : In this R Tutorial, we have learnt about R Operators – R Arithmetic Operators, R Relational Operators, R Logical Operators, R Assignment Operators, R Miscellaneous Operators with example R commands and R script files.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    11 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us