Fundamentals of R
Variables:
R is a dynamically typed language, i.e. the variables are not declared with a data type rather they take the data type of the R-object assigned to them. In R, the assignment can be denoted in three ways.
- Using equal operator- data is copied from right to left.
variable_name = value
- Using leftward operator- data is copied from right to left.
variable_name <- value
- Using rightward operator- data is copied from left to right.
value -> variable_name
Example:
R
# R program to illustrate # Initialization of variables # using equal to operator var1 = "gfg" print (var1) # using leftward operator var2 <- "gfg" print (var2) # using rightward operator "gfg" -> var3 print (var3) |
Output:
[1] "gfg" [1] "gfg" [1] "gfg"
Note: For more information, refer R – Variables.
Comments:
Comments are the english sentences that are used to add useful information to the source code to make it more understandable by the reader. It explains the logic part used in the code and will have no impact in the code during its execution. Any statement starting with “#” is a comment in R.
Example:
R
# all the lines starting with '#' # are comments and will be ignored # during the execution of the # program # Assigning values to variables a <- 1 b <- 2 # Printing sum print (a + b) |
Output:
[1] 3
Note: For more information, refer Comments in R
Operators
Operators are the symbols directing the various kinds of operations that can be performed between the operands. Operators simulate the various mathematical, logical and decision operations performed on a set of Complex Numbers, Integers, and Numericals as input operands. These are classified based on their functionality –
- Arithmetic Operators: Arithmetic operations simulate various math operations, like addition, subtraction, multiplication, division and modulo.
Example:
R
# R program to illustrate # the use of Arithmetic operators a <- 12 b <- 5 # Performing operations on Operands cat ( "Addition :" , a + b, "\n" ) cat ( "Subtraction :" , a - b, "\n" ) cat ( "Multiplication :" , a * b, "\n" ) cat ( "Division :" , a / b, "\n" ) cat ( "Modulo :" , a %% b, "\n" ) cat ( "Power operator :" , a ^ b) |
Output:
Addition : 17 Subtraction : 7 Multiplication : 60 Division : 2.4 Modulo : 2 Power operator : 248832
- Logical Operators: Logical operations simulate element-wise decision operations, based on the specified operator between the operands, which are then evaluated to either a True or False boolean value.
Example:
R
# R program to illustrate # the use of Logical operators vec1 <- c ( FALSE , TRUE ) vec2 <- c ( TRUE , FALSE ) # Performing operations on Operands cat ( "Element wise AND :" , vec1 & vec2, "\n" ) cat ( "Element wise OR :" , vec1 | vec2, "\n" ) cat ( "Logical AND :" , vec1 && vec2, "\n" ) cat ( "Logical OR :" , vec1 || vec2, "\n" ) cat ( "Negation :" , !vec1) |
Output:
Element wise AND : FALSE FALSE Element wise OR : TRUE TRUE Logical AND : FALSE Logical OR : TRUE Negation : TRUE FALSE
- Relational Operators: The relational operators carry out comparison operations between the corresponding elements of the operands.
Example:
R
# R program to illustrate # the use of Relational operators a <- 10 b <- 14 # Performing operations on Operands cat ( "a less than b :" , a < b, "\n" ) cat ( "a less than equal to b :" , a <= b, "\n" ) cat ( "a greater than b :" , a > b, "\n" ) cat ( "a greater than equal to b :" , a >= b, "\n" ) cat ( "a not equal to b :" , a != b, "\n" ) |
Output:
a less than b : TRUE a less than equal to b : TRUE a greater than b : FALSE a greater than equal to b : FALSE a not equal to b : TRUE
- Assignment Operators: Assignment operators are used to assign values to various data objects in R.
Example:
R
# R program to illustrate # the use of Assignment operators # Left assignment operator v1 <- "w3wiki" v2 <<- "w3wiki" v3 = "w3wiki" # Right Assignment operator "w3wiki" ->> v4 "w3wiki" -> v5 # Performing operations on Operands cat ( "Value 1 :" , v1, "\n" ) cat ( "Value 2 :" , v2, "\n" ) cat ( "Value 3 :" , v3, "\n" ) cat ( "Value 4 :" , v4, "\n" ) cat ( "Value 5 :" , v5) |
Output:
Value 1 : w3wiki Value 2 : w3wiki Value 3 : w3wiki Value 4 : w3wiki Value 5 : w3wiki
Note: For more information, refer R – Operators
Keywords:
Keywords are specific reserved words in R, each of which has a specific feature associated with it. Here is the list of keywords in R:
if | function | FALSE | NA_integer |
else | in | NULL | NA_real |
while | next | Inf | NA_complex_ |
repeat | break | NaN | NA_character_ |
for | TRUE | NA | … |
Note: For more information, refer R – Keywords
Learn R Programming
R is a Programming Language that is mostly used for machine learning, data analysis, and statistical computing. It is an interpreted language and is platform independent that means it can be used on platforms like Windows, Linux, and macOS.
In this R Language tutorial, we will Learn R Programming Language from scratch to advance and this tutorial is suitable for both beginners and experienced developers).
Contact Us