Apart from the regular arithmetic, logical and relational operators, R has some miscellaneous operators. In this tutorial you will learn about the miscellaneous operators in R with examples.
Miscellaneous Operators in R
In R, miscellaneous operators are used for special purposes like data frame column selection, generating sequences, model formula, matrix multiplication, etc. Following symbols are used as miscellaneous operators in R programming language:
Operator Symbols | Operation | Example |
---|---|---|
$ |
Data frame column selection | df$name |
$ |
Named list selection | lst$name |
: |
Sequence generation | 1:5 |
%in% |
Element belongs to a vector | 5 %in% x |
%*% |
Matrix Multiplication | A %*% B |
~ |
Model formula | x ~ y |
Examples of miscellaneous Operators in R
Data frame column selection ($
)
The $
operator is used to access the column of a data frame or named list. To know more about data frames in R check our post data frames in R.
## trees is a built-in data frame
data("trees")
# To access the Girth column of trees
trees$Girth
[1] 8.3 8.6 8.8 10.5 10.7 10.8 11.0 11.0 11.1 11.2 11.3 11.4 11.4 11.7 12.0
[16] 12.9 12.9 13.3 13.7 13.8 14.0 14.2 14.5 16.0 16.3 17.3 17.5 17.9 18.0 18.0
[31] 20.6
Note that to access the variables of data frame directly with the names you can use the attach()
function.
Using attach()
function the data frame is attached to the R search path and the variables in the data frame can be accessed by simply giving their names.
attach(trees)
Girth
[1] 8.3 8.6 8.8 10.5 10.7 10.8 11.0 11.0 11.1 11.2 11.3 11.4 11.4 11.7 12.0
[16] 12.9 12.9 13.3 13.7 13.8 14.0 14.2 14.5 16.0 16.3 17.3 17.5 17.9 18.0 18.0
[31] 20.6
Named list selection ($
)
The $
operator is used to access components of named list. To know more about lists in R check our post lists in R.
mylist <-list(Number =10:14,
Fruit =c("Apple","Orange","Banana") )
The elements of component Number
from the list mylist
can be accessed using mylist$Number
.
# to access the elements of component Number
mylist$Number
[1] 10 11 12 13 14
The elements of component Fruit
from the list mylist
can be accessed using mylist$Fruit
.
# to access the elements of component Fruit
mylist$Fruit
[1] "Apple" "Orange" "Banana"
Sequence Operator (:
)
The sequence operator :
is used to generate regular sequence.
# generate sequence from 1 to 5
1:5
[1] 1 2 3 4 5
# generate sequence from 8 to 4
8:4
[1] 8 7 6 5 4
To know more about sequence operator and examples of sequence operator check our post about Generating vectors using :
operator
Examples of %in%
operator
The %in%
is a binary operator, which returns a logical vector indicating if there is a match or not for it left operand.
a <- 6
b <- c(4,6,16,6)
a %in% b
[1] TRUE
b %in% a
[1] FALSE TRUE FALSE TRUE
Example of %*%
Operator
The operator %*%
is used for actual matrix multiplication of two matrices, if they are conformable.
A <- matrix(c(1, 2, 3, 4), nrow = 2)
To get the matrix multiplication of A
with A
use below code:
A %*% A # Matrix Multiplication
[,1] [,2]
[1,] 7 15
[2,] 10 22
To know more about matrix in R and various functions related to matrices check our post matrix in R.
The operator *
perform element-wise matrix multiplication whereas the operator %*%
perform actual matrix multiplication.
Example of ~
operator
The ~
(tilde) operator is used to separate the left- and right-hand sides in a model formula.
x <- c(12, 10, 8, 16, 6)
y <- c(15, 8, 11, 14, 7)
To fit a linear regression model of y
on x
, we use ~
operator in lm()
function to specify the model formula.
model <- lm(y ~ x)
model
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
3.6216 0.7095
Endnote
In this tutorial you learned about some miscellaneous operators used in R and how to use these operators in R.
To learn more about other operators in R, please refer to the following tutorials:
Assignment operators in R
Arithmetic operators in R
Relational operators in R
Logical operators in R
Precendence of Operators in R
Operators in R
Hopefully you enjoyed learning this tutorial on miscellaneous operators in R. Hope the content is more than sufficient to understand miscellaneous operators in R.