0% found this document useful (0 votes)
37 views

R Programming - 3

The document discusses variables in R. It explains how to list all variables using the ls() function and patterns. It also covers deleting variables with rm() and removing all variables by passing the result of ls() to rm(). The document then provides an overview of different types of operators in R including arithmetic, relational, logical, and assignment operators.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

R Programming - 3

The document discusses variables in R. It explains how to list all variables using the ls() function and patterns. It also covers deleting variables with rm() and removing all variables by passing the result of ls() to rm(). The document then provides an overview of different types of operators in R including arithmetic, relational, logical, and assignment operators.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Finding Variables

To know all the variables currently available in the workspace we use


the ls() function. Also the ls() function can use patterns to match the variable names.
print(ls())

When we execute the above code, it produces the following result −


[1] "my var" "my_new_var" "my_var" "var.1"
[5] "var.2" "var.3" "var.name" "var_name2."
[9] "var_x" "varname"

Note − It is a sample output depending on what variables are declared in your


environment.
The ls() function can use patterns to match the variable names.
# List the variables starting with the pattern "var".
print(ls(pattern = "var"))

When we execute the above code, it produces the following result −


[1] "my var" "my_new_var" "my_var" "var.1"
[5] "var.2" "var.3" "var.name" "var_name2."
[9] "var_x" "varname"
The variables starting with dot(.) are hidden, they can be listed using "all.names =
TRUE" argument to ls() function.
print(ls(all.name = TRUE))

When we execute the above code, it produces the following result −


[1] ".cars" ".Random.seed" ".var_name" ".varname"
".varname2"
[6] "my var" "my_new_var" "my_var" "var.1"
"var.2"
[11]"var.3" "var.name" "var_name2." "var_x"

Deleting Variables
Variables can be deleted by using the rm() function. Below we delete the variable
var.3. On printing the value of the variable error is thrown.

rm(var.3)
print(var.3)

When we execute the above code, it produces the following result −


[1] "var.3"
Error in print(var.3) : object 'var.3' not found
All the variables can be deleted by using the rm() and ls() function together.

rm(list = ls())
print(ls())

When we execute the above code, it produces the following result −


character(0)

R- Operators

An operator is a symbol that tells the compiler to perform specific mathematical or


logical manipulations. R language is rich in built-in operators and provides following
types of operators.

Types of Operators
We have the following types of operators in R programming −

• Arithmetic Operators
• Relational Operators
• Logical Operators
• Assignment Operators
• Miscellaneous Operators

Arithmetic Operator:

Operator Description Example

+ Adds two vectors v = c( 2,5.5,6)


t = c(8, 3, 4)
print(v+t)

it produces the following result −


[1] 10.0 8.5 10.0

− Subtracts second vector v = c( 2,5.5,6)


from the first t = c(8, 3, 4)
print(v-t)

it produces the following result −


[1] -6.0 2.5 2.0

* Multiplies both vectors v = c( 2,5.5,6)


t = c(8, 3, 4)
print(v*t)

it produces the following result −


[1] 16.0 16.5 24.0

/ Divide the first vector with v = c( 2,5.5,6)


the second t = c(8, 3, 4)
print(v/t)

When we execute the above code, it produces the


following result −
[1] 0.250000 1.833333 1.500000

%% Give the remainder of the v = c( 2,5.5,6)


first vector with the second t = c(8, 3, 4)
print(v%%t)

it produces the following result −


[1] 2.0 2.5 2.0

%/% The result of division of first v <- c( 2,5.5,6)


vector with second t <- c(8, 3, 4)
(quotient) print(v%/%t)

it produces the following result −


[1] 0 1 1

^ The first vector raised to the v <- c( 2,5.5,6)


exponent of second vector t <- c(8, 3, 4)
print(v^t)

it produces the following result −


[1] 256.000 166.375 1296.000

Relational Operators
Following table shows the relational operators supported by R language. Each
element of the first vector is compared with the corresponding element of the second
vector. The result of comparison is a Boolean value.

Operator Description Example


>

v = c(2,5.5,6,9)
Checks if each element of the first vector is t = c(8,2.5,14,9)
greater than the corresponding element of print(v>t)
the second vector. print(v>=t)

it produces the following result −


[1] FALSE TRUE FALSE FALSE
[2] FALSE TRUE FALSE TRUE

< v = c(2,5.5,6,9)
t = c(8,2.5,14,9)
print(v < t)
Checks if each element of the first vector is PRINT(v<= t)
less than the corresponding element of the
second vector. it produces the following result −
[1] TRUE FALSE TRUE FALSE
[2] TRUE FALSE TRUE TRUE

==
v = c(2,5.5,6,9)
Checks if each element of the first vector is t = c(8,2.5,14,9)
equal to the corresponding element of the print(v == t)
second vector.
it produces the following result −
[1] FALSE FALSE FALSE TRUE

<=
v <- c(2,5.5,6,9)
Checks if each element of the first vector is t <- c(8,2.5,14,9)
less than or equal to the corresponding print(v<=t)
element of the second vector.
it produces the following result −
[1] TRUE FALSE TRUE TRUE

>=
v <- c(2,5.5,6,9)
Checks if each element of the first vector is t <- c(8,2.5,14,9)
greater than or equal to the corresponding print(v>=t)
element of the second vector.
it produces the following result −
[1] FALSE TRUE FALSE TRUE
!= v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
Checks if each element of the first vector is print(v!=t)
unequal to the corresponding element of
the second vector. it produces the following result −
[1] TRUE TRUE TRUE FALSE

Logical Operators
Following table shows the logical operators supported by R language. It is applicable
only to vectors of type logical, numeric or complex. All numbers greater than 1 are
considered as logical value TRUE.
Each element of the first vector is compared with the corresponding element of the
second vector. The result of comparison is a Boolean value.

Operator Description Example

& It is called Element-wise Logical AND v = c(3,1,TRUE,2+3i)


operator. It combines each element of the t = c(4,1,FALSE,2+3i)
first vector with the corresponding element print(v&t)
of the second vector and gives a output
TRUE if both the elements are TRUE. it produces the following result −
[1] TRUE TRUE FALSE TRUE

| v = c(3,0,TRUE,2+2i)
It is called Element-wise Logical OR t = c(4,0,FALSE,2+3i)
operator. It combines each element of the print(v|t)
first vector with the corresponding element it produces the following result −
of the second vector and gives a output
TRUE if one the elements is TRUE. [1] TRUE FALSE TRUE TRUE

! v = c(3,0,TRUE,2+2i)
It is called Logical NOT operator. Takes print(!v)
each element of the vector and gives the
opposite logical value. it produces the following result −
[1] FALSE TRUE FALSE FALSE
The logical operator && and || considers only the first element of the vectors and give
a vector of single element as output.

Operator Description Example

&& v = c(3,0,TRUE,2+2i)
t = c(1,3,TRUE,2+3i)
print(v&&t)

v = c(0,0,TRUE,2+2i)
Called Logical AND operator. Takes first
t = c(1,3,TRUE,2+3i)
element of both the vectors and gives the print(v&&t)
TRUE only if both are TRUE.

it produces the following result −


[1] TRUE
[2] FALSE

|| v = c(0,0,TRUE,2+2i)
t = c(0,3,TRUE,2+3i)
print(v||t)

v = c(7,0,TRUE,2+2i)
Called Logical OR operator. Takes first t = c(0,3,TRUE,2+3i)
element of both the vectors and gives the print(v||t)
TRUE if one of them is TRUE.

it produces the following result −


[1] FALSE
[2] TRUE

Assignment Operators
These operators are used to assign values to vectors.

Operator Description Example

Called Left Assignment v1 <- c(3,1,TRUE,2+3i)


<− v2 <<- c(3,1,TRUE,2+3i)
or v3 = c(3,1,TRUE,2+3i)
V4=list( c(3,1),TRUE,2+3i)
= print(v1)
print(v2)
or
print(v3)
<<− it produces the following result −
[1] 3+0i 1+0i 1+0i 2+3i
[1] 3+0i 1+0i 1+0i 2+3i
[1] 3+0i 1+0i 1+0i 2+3i
[2] 3 1 TRUE 2+3i

Called Right Assignment c(3,1,TRUE,2+3i) -> v1


c(3,1,TRUE,2+3i) ->> v2
-> print(v1)
print(v2)
or
->> it produces the following result −
[1] 3+0i 1+0i 1+0i 2+3i
[1] 3+0i 1+0i 1+0i 2+3i

Miscellaneous Operators
These operators are used to for specific purpose and not general mathematical or
logical computation.

Operator Description Example

: Colon v = 2:8
operator. It print(v)
creates the
series of it produces the following result −
numbers in [1] 2 3 4 5 6 7 8
sequence
for a vector.

%in% v1 <- 8
This v2 <- 12
operator is t <- 1:10
used to print(v1 %in% t)
identify if an print(v2 %in% t)
element
it produces the following result −
belongs to a
vector. [1] TRUE
[1] FALSE
%*%
This M = matrix( c(2,6,5,1,10,4), nrow = 2,ncol =
3,byrow = TRUE)
operator is
t = M %*% t(M)
used to print(t)
multiply a
matrix with it produces the following result −
its
[,1] [,2]
transpose.
[1,] 65 82
[2,] 82 117

You might also like