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

Discover more from R Language Frequently Asked Questions

Subscribe now to keep reading and get access to the full archive.

Continue reading