R Language: A Quick Reference – IV

R Programming: 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 Quick Reference is classified into different groups. Let us start with R Language: A Quick Reference – IV.

This R Language: A Quick Reference contains R commands about performing different descriptive statistics on vectors, matrices, lists, data frames, arrays, and factors.

Basic Descriptive Statistics in R Language

The following is the list of widely used functions that are further helpful in computing descriptive statistics. The functions below are not direct descriptive statistics functions, however, these functions are helpful to compute other descriptive statistics.

R CommandShort Description
sum(x1, x2, … , xn)Computes the sum/total of $n$ numeric values given as argument
prod(x1, x2, … , xn)Computes the product of all $n$ numeric values given as argument
min(x1, x2, … , xn)Gives smallest of all $n$ values given as argument
max(x1, x2, …, xn)Gives largest of all $n$ values given as argument
range(x1, x2, … , xn)Gives both the smallest and largest of all $n$ values given as argument
pmin(x1, x2, …)Returns minima of the input values
pmax(x1, x2, …)Returns maxima of the input values

Statistical Descriptive Statistics in R Language

The following is the list of functions that are used to compute measures of central tendency (Mean and Median), measures of dispersion (Standard Deviation and Variance), and measures of locations (Quantiles, and Median).

R CommandShort Description
mean(x)Computes the arithmetic mean of all elements in $x$
sd(x)Computes the standard deviation of all elements in $x$
var(x)Computes the variance of all elements in $x$
median(x)Computes the median of all elements in $x$
quantile(x)Computes the median, quartiles, and extremes in $x$
quantile(x, p)Computes the quantiles specified by $p$

Cumulative Summaries in R Language

The following functions are also helpful in computing the other descriptive calculations.

R CommandShort Description
cumsum(x)Computes the cumulative sum of $x$
cumprod(x)Computes the cumulative product of $x$
cummin(x)Computes the cumulative minimum of $x$
cummax(x)Computes the cumulative maximum of $x$

Sorting and Ordering Elements in R Language

The sorting and ordering functions are useful in especially non-parametric methods.

R CommandShort Description
sort(x)Sort the all elements of $x$ in ascending order
sort(x, decreasing = TRUE)Sor the all elements of $x$ in descending order
rev(x)Reverse the elements in $x$
order(x)Get the ordering permutation of $x$

Sequence and Repetition of Elements in R Language

These functions are used to generate a sequence of numbers or repeat the set of numbers $n$ times.

R CommandShort Description
a:bGenerates a sequence of numbers from $a$ to $b$ in steps of size 1
seq(n)Generates a sequence of numbers from 1 to $n$
seq(a, b)Generates a sequence of numbers from $a$ to $b$ in steps of size 1, it is the same as a:b
seq(a, b, by=s)Generates a sequence of numbers from $a$ to $b$ in steps of size $s$.
seq(a, b, length=n)Generates a sequence of numbers having length $n$ from $a$ to $b$
rep(x, n)Repeats the elements $n$ times
rep(x, each=n)Repeats the elements of $x$, each element is repeated $n$ times

R Language: A Quick Reference – I

Visit https://gmstat.com

R Language: A Quick Reference – III

R Programming: 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 Quick Reference is classified into different groups. Let us start with R Language: A Quick Reference – III.

This R Language: A Quick Reference contains R commands about 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 in R Language

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 of 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 Language

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] = dfvSets 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: A Quick Reference – I

R Language: A Quick Reference – II

R Programming: 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 Quick Reference is classified into different groups. Let us start with R Language: A Quick Reference – II.

This R Language: A Quick Reference 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.

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.

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

Namespaces in R Language

In R language, the packages can have namespaces, and currently, all of the base and recommended packages do except the dataset packages. Understanding the use of namespaces is vital if one plans to submit a package to CRAN because CRAN requires that the package plays nicely with other submitted packages on CRAN.

Namesspaces in R Language

Namespaces ensure that other packages will not interfere with your code and that the package works regardless of the environment in which it’s run. In R Language, the namespace environment is the internal interface of the package. It includes all objects in the package, both exported and non-exported to ensure that every function can find every other function in the package.

For example, plyr and Hmisc both provide a function namely summarize(). Loading plyr package and then Hmise, the summarize() function will refer to the Hmisc. However, loading the package in the opposite order, the summarize() function will refer to the plyr package version.

To avoid confusion, one can explicitly refer to the specific function, for example,

> Hmisc::summarize()

and

> plyr::summarize()

Now, the order in which the packages are loaded would not matter.

Namespaces do three things:

  • Namespaces allow the package writer to hide functions and data that are meant only for internal use,
  • Namespaces prevent functions from breaking when a user (or other package writers) picks a name that clashes with one in the package, and
  • Namespaces provide a way to refer to an object within a particular package

Namespace Operators

In R language, two operators work with namespaces.

  • Doule-Colon Operator
    The double-colon operator:: selects definitions from a particular namespace. The transpose function t() will always be available as the base::t because it is defined in the base package. Only functions that are exported from the package can be retrieved in this way.
  • Triple-Colon Operator
    The triple-colon operator ::: acts like the double-colon operator but also allows access to hidden objects. Users are more likely to use the getAnywhere() function, which searches multiple packages.

Packages are often interdependent, and loading one may cause others to be automatically loaded. The colon operators will also cause automatic loading of the associated package. When packages with namespaces are loaded automatically they are not added to the search list.

Basic R Frequently Asked Questions

Online MCQs Test Preparation Website with Answers

x  Powerful Protection for WordPress, from Shield Security
This Site Is Protected By
Shield Security