R Programming - SPLessons

R Variables And Operators

Home > Lesson > Chapter 4
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

R Variables And Operators

R Variables And Operators

shape Description

Every programming language will have operators and variables to performs operations. There are various commands or functions in R which can be used to get built-in help. This will work in offline mode also, as it uses the available resources installed on your local machine. Help commands are used to search local R documentation for any R function or object. So if user not sure about working with any function or object then user can use help commands to get instant help. It will search the installed documentation for your search term and will provide you relevant results.

R Operators

shape Description

Operators are one of the pillar blocks in any programming language. Typically an Operator can work on one or more objects, also known as Operands, and it will return some of the results. The following are the operators of any language.

Arithmetic operators

Arithmetic operators performs mathematical calculations like addition, subtraction, multiplication, division, and modulus.
Operators Operation
+ Addition
- Subtraction
* Multiplication
Mod Remainder
/ Division
Div Integer Division

Logical operators

Logical operators performs logical operations on given expressions. The following are the various operations.
Operators Operation
or Bitwise or
xor Bitwise xor
and Bitwise and
not Bitwise negation
<< Bitwise shift to the left
>> Bitwise shift to the right

Relational operators

Relational operators can be used to compare two variables. The following are the various operators.
Operators Operation
= Equal
< Stricty less than
> Stricty greater than
Not equal
<= Less than or equal
>= Greater than or equal
in Elements of

R Variables

shape Description

The different memory locations where data resides are called objects. To access such an object it must have a name, and such a named object is called a Variable. The information stored inside an object is called as a value. Of course the machine itself typically has no idea how to interpret the information it contains, but the program certainly needs to know. For this reason a variable has a specific type. And it’s this type that constrains the use of that variable, such that only operations appropriate to the type may be performed.

Variables Assigning

The variables can be assigned with equal operator or leftward or rightward. The following is an example. [c] # Assignment using equal operator. var.1 = c(0,1,2,3) # Assignment using leftward operator. var.2 <- c("learn","R") # Assignment using rightward operator. c(TRUE,1) -> var.3 print(var.1) cat ("var.1 is ", var.1 ,"\n") cat ("var.2 is ", var.2 ,"\n") cat ("var.3 is ", var.3 ,"\n") [/c] In the above example, print() and cat() functions are used to display the results, but cat() function will combine different items. Output: Now compile the code result will be as follows. [c] > print(var.1) [1] 0 1 2 3 > cat ("var.1 is ", var.1 ,"\n") var.1 is 0 1 2 3 > cat ("var.2 is ", var.2 ,"\n") var.2 is learn R > cat ("var.3 is ", var.3 ,"\n") var.3 is 1 1[/c]

Data type of variables

Every variable will have data type to store the variable value, the following is an example. [c]var_x <- "SPLESSONS" cat("The class of var_x is ",class(var_x),"\n") var_x <- 92.5 cat(" Now the class of var_x is ",class(var_x),"\n") var_x <- 32L cat(" Next the class of var_x becomes ",class(var_x),"\n")[/c] Output: Now compile the code result will be as follows. [c] > cat("The class of var_x is ",class(var_x),"\n") The class of var_x is character > > var_x <- 92.5 > cat(" Now the class of var_x is ",class(var_x),"\n") Now the class of var_x is numeric > > var_x <- 32L > cat(" Next the class of var_x becomes ",class(var_x),"\n") Next the class of var_x becomes integer [/c]

Summary

shape Key Points

  • The ls() is the function to know the present variable in the work space.
  • The rm() is the function to delete the variable from the work space.
  • The %in% operator finds whether the given element is from vector or not.