R Language Reference Guide III: A Quick Guide

The post is about the R Language Reference Guide subsetting Vectors, Lists, Matrices, and Data Frames in R Language.

R Language A Quick Reference

R language Reference Guide is about learning R Programming with a short description of the widely used commands. It will help the learner and intermediate user of the R Programming Language to get help with different functions quickly. This R language reference is classified into different groups. Let us start with the R Language Reference Guide – III.

This R Language Quick Reference contains R commands about subsetting in R, such as subsetting of vectors, matrices, lists, data frames, arrays, and factors. It also discusses setting the different properties related to R language data types.

Subsetting Vectors: Quick R Language Reference

The following are ways to subset or slice the values from a vector.

R CommandShort Description
x[1:5]Select elements of $x$ by index
x[-(1:5)]Exclude elements of $x$ by index
x[c(TRUE, FALSE)]Select elements of $x$ corresponding to the True value
x[c(“a”, “b”)]Select elements of $x$ by name

Subsetting Lists in R Language

The following methods are used to subset or slice a list in R Language.

R CommandShort Description
x[1:5]Extracts a sublist of the list $x$
x[-(1:5)]Extract a sublist by excluding elements of list $x$
x[c(TRUE, FALSE)]Extract a sublist with logical subscripts
x[c(“a”, “b”)]Extract a sublist by name
x[[2]]Extract an element of the list $x$
x[[“a”]]Extract the element with the name “a” from list $x$
x$aExtract the element with the name “a” from list $x$

Subsetting Matrices in R: A Quick Reference

To subset or extract certain elements from a matrix follow the ways described below.

R CommandShort Description
x[i, j]Extracts elements of matrix $x$, specified by row $i$ and column $j$
x[i, j] = vSet or rest the elements of matrix $x$, specified by row $i$ and column $j$
x[i, ]Extracts $i$th row of a matrix $x$
x[i, ] = vSet or resets the $i$th row of a matrix $x$ specified by $i$th row
x[ , j]Extracts the $j$ column of a matrix $x$
x[ , j] = vSets or resets the $j$ column of matrix $x$
x[i]Subets a matrix $x$ as a vector
x[i] = vSets or resets the $i$th elements (treated as a vector operation)

Subsetting a Data Frame in R Language

One can easily subset or slice a Data Frame in R.

R CommandShort Description
df[i, j]Matrix subsetting of a data frame, specified by $i$th row and $j$th column
df[i, j] = dfSets or resets a subset of a data frame
subset(df, subset = i)Subset of the $i$ cases/ observations of a data frame
subset(df, select = i)Subset of the $i$ variables/ columns of a data frame
subset(df, subset=i, select=j)Subset of the $i$ cases and $j$ variables of a data frame
R Language Reference Guide

R Language: A Quick Reference – I

https://gmstat.com

R Language Quick Reference Guide II

The article is about the R Language Quick Reference Guide. This Quick Reference will help you to learn about creating vectors, matrices, data frames, lists, and factors. You will also learn about setting properties of different data types in R Language.

R Language Quick Reference Guide

R Language Quick Reference Guide

R language Quick Reference Guide is about learning R Programming with a short description of the widely used commands. It will help the learner and intermediate user of the R Programming Language to get help with different functions quickly. This Quick Reference is classified into different groups. Let us start with R Language Quick Reference Guide – II.

This R Language Quick Reference Guide contains R commands about creating vectors, matrices, lists, data frames, arrays, and factors. It also discusses setting the different properties related to R language data types.

Creating Vectors in R Language

The creation of a row or column vector in the R Language is very important. One can easily create a vector of numbers, characters/ strings, complex numbers, and logical values, and can concatenate the elements. The following are different commands for creating Vectors in R

R commandShort Description
c(a1, a2, …, an)Concatenates all $n$ elements to a vector
logical(n)Creates a logical vector of length $n$ (containing false)
numeric(n)Creates a numeric vector of length $n$ (containing zeros)
character(n)Creates a character vector of length $n$ (containing an empty string)
complex(n)Creates a complex vector of length $n$ (containing zeros)

Creating Lists in R Language

Creating Lists in R is important as it can store different types of data and even lists. A vector can also be used to create a list of $k$ elements. The following are ways for creating lists in R language.

R CommandShort Description
list(e1, e2, … ek)Combines all $k$ elements as a list
vector(k, “list”)Creates a list of length $k$ (the elements are all NULL)

Creating Matrices in R Language

Two-dimensional data can be created using the matrix command in R.

R CommandShort Description
matrix(x, nr = r, nc = c)Creates a matrix from $x$ (column as major order)
matrix(x, nr = r, nc = c)Creates a matrix from $x$ (row as major order)

Creating Factors in R Language

To create categorical variables, R has a concept of factors as variables. All factors have levels that may have ordered factors.

R CommandShort Description
factor(x)Creates a factor from the values of variable $x$
factor(x, levels = 1)Creates a factor with the given level set from the values of the variable $x$
ordered(x)Creates an ordered factor with the given level set from the values of the variable $x$
levels(x)Gives the levels of a factor or ordered factor
levels(x) = vSet or reset the levels of a factor or ordered factor

Creating a Data Frame in R Language

A data frame is a tabular data format used for statistical data analysis. The format of the data is like data entered in spreadsheets for data analysis.

R CommandShort Description
data.frame(n1=x1, n2=x2, ….)Creates a data frame

R Language Data Type Properties

Every data object has different properties. These properties can be used to find out the number of rows in a vector or matrix, the number of columns, names of rows and columns of a matrix or data frame.

R CommandShort Description
length(x)Gives the number of elements in a variable $x$
mode(x)Tells about the data type of the variable $x$
nrow(x)Displays the number of rows of a vector, array, or data frame $x$
ncol(x)Displays the number of columns (variable) of a vector, array, or data frame $x$
dim(x)Displays the dimension (number of rows and columns) of a matrix, data frame, array, or list $x$
row(x)Matrix of row indices for matrix-like object $x$
col(x)Matrix of column indices for matrix-like object $x$
rownames(x)Get the row names of the matrix-like object $x$
rownames(x)=vSet the row names of the matrix-like object $x$ to $v$
colnames(x)Get the column names of the matrix-like object $x$
colnames(x)=vSet the column names of the matrix-like object $x$ to $v$
dimnames(x)Get both the row and column names (in a matrix, data frame, or list)
dimnames(x)=list(rn, cn)Set both the row and column names
names(x)Gives the names of $x$
namex(x)=vSets or resets the names of $x$ to $v$
names(x)=NULLremoves the names from $x$
row.names(df)Gives the observation names from a data frame
row.names(df)=vSets or resets the observation names of a data frame
names(df)Gives the variables names from a data frame
names(df)=vSets or resets the variable names of a data frame

R Language: A Quick Reference – I

https://itfeature.com, https://gmstat.com

R Quick Reference I

The article is about R Quick Reference related to Data representation in R Language, Data Types in R, Checking/Testing of special values in R, Changing the basic data types, use of Mathem operations, rounding of the numbers and outputs, and mathematical functions.

R Quick Reference

R Language A Quick Reference

R language: A Quick Reference is about learning R Programming with a short description of the widely used commands. It will help the learner and intermediate user of the R Programming Language to get help with different functions quickly. This R Quick Reference is classified into different groups. Let us start with R Language: A Quick Reference – I.

Basic Data Representation in R

In R, data may be represented as logical values, in scientific notation, as a complex, or as a float number. The are certain values such as NA, NULL, NaN, and Inf values.

R CommandShort Description
True, FalseLogical true or false
1.23e10A number in scientific notation $1.23\times 10^{20}$
3.4iA complex number
“Hello”A String/ Characters
NAMissing Value representation (in any type of vector)
NULLMissing Value indicator in lists
NaNNot a number
-InfNegative Infinity
InfPositive infinity

Checking/ Testing the Basic Data Types in R

The type of data can be checked using some functions such as is.logical(), is.numeric(), is.list(), is.character(), is.vector() or is.complex() function.

R CommandShort Description
is.logical(x)Results in true for logical vectors
is.numeric(x)Results in true for numeric vectors
is.character(x)Results in true for character vectors
is.list(x)Results in true for lists
is.vector(x)Results in true for both lists and vectors
is.complex(x)Results in true for complex vectors

Checking/ Testing the Special Values

The type of special values can be checked using is.na(), is.nan(), is.finite(), is.ordered(), and is.factor() etc., functions

R CommandShort Description
is.na(x)Results in true for elements that are NA or NaN
is.nan(x)Results in true for elements that are NaN
is.null(x)Results in true whether $x$ is NULL
is.finite(x)Results in true for finite elements (e.g., not NA, NaN, Inf or -Inf)
is.infinite(x)Results in true for elements equal to Inf or -Inf
is.factor(x)Results in true for a factors and ordered factors
is.ordered(x)Results in true for ordered factors

Changing Basic Data Types in R

The Data Types in R can be changed by using functions such as, as.logical(), as.numeric(), as.list(), or as.numeric() etc., functions.

Type CoercionShort Description
as.logical(x)Coerces to a vector (However, lists remain lists)
as.numeric(x)Coerces a vector to a numeric vector
as.character(x)Coerces a vector to a character vector
as.list(x)Coerces a vector to a list
as.vector(x)Coerces to a vector (However, lists remains lists)
unlist(x)Converts a list to a vector
as.complex(x)Coerces to a vector (However, lists remain lists)

Basic Mathematical Operations

R can be used as a calculator. Mathematical operations such as addition, subtraction, multiplication, and division can also be performed.

Basic Math OperationShort Description
x + yPerform addition between the $x$ and $y$ vector
x – yPerform subtraction between the $x$ and $y$ vector
x * yPerform multiplication between the $x$ and $y$ vector
x / yPerform division between the $x$ and $y$ vector
x ^ yPerform exponentiation, “$x$ raised to power $y$”
x %% yComputes remainder, “$x$ modulo $y$”
x %/% yPerforms Integer division, “$x$ divided by $y$”, discard the fractional part

Rounding off the Numbers

The numbers or values of a variable can be rounded as desired.

R CommandShort Description
round(x)Round down the values of a variable to the next lowest integer
round(x, d)Round the values of a variable $x$ to the $d$ decimal places
signif(x, d)Round the values of a variable $x$ to $d$ significant digits
floor(x)Round down the values of a variable to next lowest integer
ceiling(x)Round up the values of a variable to the highest integer

Common Mathematical Functions

The commonly used mathematical functions in the R language are abs(), sqrt(), exp(), log(), and different bases of log functions.

R CommandShort Description
abs(x)Absolute values
sqrt(x)Computes the square root of the values of a variable
exp(x)Computes $e^x$
log(x)Computes the log values of the variable $x$
log10(x)Computes the log base 10 (common log) of the variable $x$
log2(x)Computes the log base 2 of the variable $x$
log(x, base=b)Computes the log base $b$ of the variable $x$

Trigonometric and Hyperbolic Functions

Following is the list of different trigonometric and Hyperbolic functions

Trigonometric FunctionsShort Description
sin(x), cos(x), tan(x)Computes the trigonometric values, sin, cos, and tan of a vector $x$
asin(x), acos(x), atan(x)Computes the inverse trigonometric values of a vector $x$
atan2(x, y)Computes arc tangent with two arguments
sinh(x), cosh(x), tanh(x)Computes hyperbolic values of a vector $x$
asinh(x), acosh(x), atanh(x)Computes the inverse hyperbolic values of a vector $x$

Special Mathematical Functions

The following is the list of special mathematical functions.

Mathematical FunctionsShort Description
beta(x, y)The beta function
lbeta(x, y)The log beta function
gamma(x)The gamma function
lgamma(x)The log gamma function
psigamma(x, deriv = 0)The psigamma function
digamma(x)The digamma function
trigamma(x)The trigamma function

R Frequently Asked Questions

MCQs in Statistics