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

MCQs R Language Quiz 8

The post is about MCQ on R Language Quiz with Answers. The quiz covers the topics related to R Package qqplot, R Studio, and Data.Frame. Let us start with MCQs R Language Quiz.

Online Multiple Choice Questions about Learning R Programming. It will help the learners to get knowledge of R not only for learning the language but also to get some practices about Statistics with reference to R Programming.

1. Data frames can be converted to a matrix by calling data.

 
 
 
 

2. When creating a plot in ggplot you must set the mapping argument of a function. Which function has the mapping argument?

 
 
 
 

3. Which of the following is NOT a valid data structure in R?

 
 
 
 

4. What does the data.frame function do in R?

 
 
 
 

5. Which of the following are operations you can perform in ggplot2?

 
 
 
 

6. What function can you use to put a text label inside the grid of your plot to call out specific data points?

 
 
 
 

7. Which of the following aesthetics attributes can you map to the data in a scatterplot?

 
 
 
 

8. Which ggplot function is used to define the mappings of variables to visual representations of data?

 
 
 
 

9. Which of the following are the benefits of using ggplot2?

 
 
 
 

10. The _________ creates a scatterplot and then adds a small amount of random noise to each point in the plot to make the points easier to find.

 
 
 
 

11. A data analyst is working with the penguin’s data. The analyst creates a scatterplot with the following code:

ggplot(data = penguins) +
       geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, alpha = species))

What does the alpha aesthetic do to the appearance of the points in the plot?

 
 
 
 

12. In R studio, what default options does the Export functionality of the Plots tab give for exporting plots?

 
 
 
 

13. Which of the following are operations you can perform in ggplot2?

 
 
 
 

14. What argument of the labs() function can a data analyst use to add text outside of the grid area of a plot?

 
 
 
 

15. Which statement about the ggsave() function is correct?

 
 
 
 

16. Which of the following are the benefits of adding labels and annotations to your plot?

 
 
 
 

17. What is the purpose of the rep() function in R?

 
 
 
 

18. What is the role of the x argument in the following code?

ggplot(data = diamonds) +
       geom_bar(mapping = aes(x = cut))
 
 
 
 

19. Which package is associated with data visualization in R?

 
 
 
 

20. In ggplot2, you use the plus sign (+) to add a layer to your plot.

 
 


MCQs R Language Quiz

  • Which of the following aesthetic attributes can you map to the data in a scatterplot?
  • What is the role of the x argument in the following code?
    ggplot(data = diamonds) +
    geom_bar(mapping = aes(x = cut))
  • Which of the following are the benefits of adding labels and annotations to your plot?
  • What function can you use to put a text label inside the grid of your plot to call out specific data points?
  • Which of the following are operations you can perform in ggplot2?
  • When creating a plot in ggplot you must set the mapping argument of a function. Which function has the mapping argument?
  • Which ggplot function is used to define the mappings of variables to visual representations of data?
  • Which of the following are the benefits of using ggplot2?
  • What argument of the labs() function can a data analyst use to add text outside of the grid area of a plot?
  • Which statement about the ggsave() function is correct?
  • A data analyst is working with the penguin’s data. The analyst creates a scatterplot with the following code:
    ggplot(data = penguins) +
    geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, alpha = species))
  • What does the alpha aesthetic do to the appearance of the points in the plot?
  • The ___________ creates a scatterplot and then adds a small amount of random noise to each point in the plot to make the points easier to find.
  • In R studio, what default options does the Export functionality of the Plots tab give for exporting plots?
  • Which of the following are operations you can perform in ggplot2?
  • In ggplot2, you use the plus sign (+) to add a layer to your plot.
  • Which of the following is NOT a valid data structure in R?
  • What is the purpose of the rep() function in R?
  • Which package is associated with data visualization in R?
  • What does the data.frame function do in R?
  • Data frames can be converted to a matrix by calling data.
Frequently Asked Questions About MCQs R Language Quiz

https://itfeature.com

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

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