Unit-2-Start Learning R
Unit-2-Start Learning R
> rm(list=c("sum"))
> ls()
[1] "a" "b" "c"
>
> rm(list = ls())
> ls() character(0)
Getting and setting the current working directories
> getwd()
[1] "/home/user"
Similarly, we can set the current wor directory by calling setwd() function:
> setwd("/home/user/prog")
Comments
Single comment is written using # in the beginning of the statement as follows:
# My first program in R Programming
R does not support multi-line comments but you can perform a trick which is something as follows:
if(FALSE)
{
"This is a demo for multi-line comments and it should be put inside either a single of double quote"
}
myString <- "Hello, World!" print ( myString)
Though above comments will be executed by R interpreter, they will not interfere with
your actual program. You should put such comments inside, either single or double quote.
R Reserved Words
if else repeat while function
for in next break TRUE
FALSE NULL Inf NaN NA
NA_integer_ NA_real_ NA_complex_ NA_character_ ...
Constants in R
Constants, as the name suggests, are entities whose value cannot be altered. Basic types of constant
are numeric constants and character constants.
Numeric Constants
All numbers fall under this category. They can be of type integer, double or complex.
It can be checked with the typeof() function.
Numeric constants followed by L are regarded as integer and those followed by i are regarded as
complex.
> typeof(5)
[1] "double"
> typeof(5L)
[1] "integer"
> typeof(5i)
[1] "complex"
Numeric constants preceded by 0x or 0X are interpreted as hexadecimal numbers.
> 0xff [1] 255
> 0XF + 1 [1] 16
Character Constants
Character constants can be represented using either single quotes (') or double quotes (") as
delimiters.
> 'example'
[1] "example"
> typeof("5")
[1] "character"
Built-in Constants
Some of the built-in constants defined in R along with their values is shown below.
> LETTERS
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q"
"R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"
> letters
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
"r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
> pi
[1] 3.141593
Example: Hello Gacr Program
> # We can use the print() function
> print("Hello Gacr!")
[1] "Hello Gacr!"
> # If there are more than 1 item, we can concatenate using paste()
> print(paste("How","are","you?"))
[1] "How are you?"
R Operators
R has several operators to perform tasks including arithmetic, logical and bitwise operations.
R has many operators to carry out different mathematical and logical operations. Operators in R can
mainly be classified into the following categories.
Arithmetic operators
Relational operators
Logical operators
Assignment operators
R Arithmetic Operators
These operators are used to carry out mathematical operations like addition and multiplication. Here
is a list of arithmetic operators available in R.
Arithmetic Operators in R
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponent
%% Modulus (Remainder from division)
%/% Integer Division
Example
> x <- 5
> y <- 16
> x+y [1]
21
R Relational Operators
Relational Operators in R
Operator Description
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to
Example
> x <- 5
> y <- 16
> x<y
[1] TRUE
> x>y
[1] FALSE
> x<=5
[1] TRUE
> y>=20
[1] FALSE
> y == 16
[1] TRUE
> x != 5
[1] FALSE
R Logical Operators
Logical Operators in R
Operator Description
! Logical NOT
& Element-wise logical AND
&& Logical AND
| Element-wise logical OR
|| Logical OR
R Assignment Operators
Assignment Operators in R
Operator Description
<-, <<-, = Leftwards assignment
->, ->> Rightwards assignment
> # If there are more than 1 item, we can concatenate using paste()
> print(paste("How","are","you?"))
[1] "How are you?"
Example:
my.name <- readline(prompt="Enter name: ")
my.age <- readline(prompt="Enter age: ")
R Functions
Syntax
func_name <- function (argument)
{
statement
}
Example
pow <- function(x, y)
{
result <- x^y
print(paste(x,"raised to the power", y, "is", result))
}
> pow(2, 8)
[1] "2 raised to the power 8 is 256"
Here, the arguments used in the function declaration (x and y) are called formal arguments and
those used while calling the function are called actual arguments.
Named Arguments
In the above function calls, the argument matching of formal argument to the actual arguments
takes place in positional order.
This means that, in the call pow(8,2), the formal arguments x and y are assigned 8 and 2
respectively.
We can also call the function using named arguments.
When calling a function in this way, the order of the actual arguments doesn't matter. For
example, all of the function calls given below are equivalent.
> pow(8, 2)
[1] "8 raised to the power 2 is 64"
> pow(x = 8, y = 2)
[1] "8 raised to the power 2 is 64"
> pow(y = 2, x = 8)
[1] "8 raised to the power 2 is 64"
In such case, all the named arguments are matched first and then the remaining unnamed arguments
are matched in a positional order.
> pow(x=8, 2)
[1] "8 raised to the power 2 is 64"
Sample runs
> check(1)
[1] "Positive"
> check(-10)
[1] "Negative"
> check(0)
[1] "Zero"
Here, we create a list my_list with multiple elements and return this single list.
> a$size
[1] 20
> a$shape
[1] "round"
R Recursive Function
Example
recursive.factorial <- function(x)
{
if (x==0) return(1)
else return(x * recursive.factorial(x-1))
}
> recursive.factorial(0)
[1] 1
> recursive.factorial(5) [1]
120
> recursive.factorial(7) [1]
5040