# Category: R FAQS

R Frequently Asked Questions (FAQS)

## Important Frequently Asked Questions about R

This post is about some frequently asked Questions about R Language. These questions will help you prepare for examinations and interviews.

**Question: What is a Compiler in R Language**?**Answer:** A compiler is software that transforms computer code (source code) to another computer language (target language, i.e., object code).

**Question: What is a package in R Language**?**Answer:** The R package is a collection of R functions, compiled code, sample data, and help documentation. The R packages are stored in a directory called “library” in the R environment. The R language also installed a set of packages during installation.

**Question: What is JIT?Answer:** JIT standards for “Just in Time” compiler. It is a method to improve the run-time performance of a computer program.

**Question: What is procedural Programming in R Language?Answer:** Procedural programming is derived from structured programming and it is based on the concept of procedure call. Procedures are also known as routines, subroutines, or functions. It contains a series of computational steps to be carried out. Any procedure may be called (at any point) during a program’s execution.

**Question: What is the recycling of elements in a vector?****Answer: **When a mathematical operation (such as addition, subtraction, multiplication, division, etc) is performed on two vectors of different lengths (the number of elements in both vectors is different), the element having a shorter length is reused to complete the mathematical operations.

vect1 <- c(4, 1, 4, 5, 6, 9) vect2 <- c(2, 5) vect1 * vect2 ### 8, 5, 8, 25, 12, 45

The elements of `vect2`

are recycled to complete the operation of all elements of `vect1`

.

**Question: What is the difference between a data frame and a matrix in R Language?****Answer:** In R, the data frame contains heterogeneous data (different columns of the data frame may have different types of variable) while a matrix contains homogenous data (all the columns of the matrix have the same type of variable). In a matrix, similar data types can be stored while in a data frame, different types of data can be stored.

**See Questions about R language Missing Values**

## R Language: A Quick Reference – IV

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

R Command | Short 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

R Command | Short 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

R Command | Short 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

R Command | Short 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

R Command | Short Description |
---|---|

a:b | Generates 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 – III

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 about setting the different properties related to R language data types.

### Subsetting Vectors in R Language

R Command | Short 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

R Command | Short 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$a | Extract the element with the name “a” from list $x$ |

### Subsetting Matrices in R Language

R Command | Short Description |
---|---|

x[i, j] | Extracts elements of matrix $x$, specified by row $i$ and column $j$ |

x[i, j] = v | Set 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, ] = v | Set 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] = v | Sets or resets the $j$ column of matrix $x$ |

x[i] | Subets a matrix $x$ as a vector |

x[i] = v | Sets or resets the $i$th elements (treated as a vector operation) |

### Subsetting a Data Frame in R Language

R Command | Short Description |
---|---|

df[i, j] | Matrix subsetting of a data frame, specified by $i$th row and $j$th column |

df[i, j] = dfv | Sets 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 – II

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 about setting the different properties related to R language data types.

### Creating Vectors in R Language

R command | Short 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

R Command | Short 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

R Command | Short 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

R Command | Short 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) = v | Set or reset the levels of a factor or ordered factor |

### Creating a Data Frame in R Language

R Command | Short Description |
---|---|

data.frame(n1=x1, n2=x2, ….) | Creates a data frame |

### R Language Data Type Properties

R Command | Short 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)=v | Set 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)=v | Set 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)=v | Sets or resets the names of $x$ to $v$ |

names(x)=NULL | removes the names from $x$ |

row.names(df) | Gives the observation names from a data frame |

row.names(df)=v | Sets or resets the observation names of a data frame |

names(df) | Gives the variables names from a data frame |

names(df)=v | Sets or resets the variable names of a data frame |