R Programming Flashcards
How to create a vector in R?
Use the c() function which means to combine the elements into a vector. For example:
apple
How to create a list in R?
A list is an R-object which can contain many different types of elements inside it like vectors, functions and even another list inside it.
list1
How to create Matrices in R?
A matrix is a two-dimensional rectangular data set. It can be created using a vector input to the matrix function.
M = matrix( c(‘a’, ‘a’, ‘b’, ‘c’, ‘b’ , ‘a’), nrow = 2, ncol = 3, byrow =TRUE).
print(M)
“a” “a” “b”
“c” “b” “a”
How to create arrays in R?
The array function has an advantage being created with any number of dimensions. For example:
A
How to create a data frame in R?
Data frames are tabular data objects. For example:
BMI
What is the basic syntax for creating a matrix in R
matrix(data, nrow, ncol, byrow,dimnames)
data: is the input vector which becomes the data elements of the matrix
nrow : number of rows created
ncol: number of columns created
byrow: a logical clue. If TRUE then the input vector elements are arranged by row
dimname: the names assigned to the rows and columns
What is lapply()? What is the syntax?
lapply() applies a function over a list or a vector. The output of lapply is a list
lapply(list, triple):
where triple is a function applied to each element in a list
What is sapply()?
sapply() applies a function over a list or vector.It’s ouput is an array
What is vapply()?
vapply() applies a function over a list or vector. The output is explicitly specified.
What is the syntax for the read.csv function
read.csv (file, header= False, sep =” , “ )
Where file = the file path i.e where the file is located on your computer
header = default value is False. If the file does have a header row value should be changed to False
sep = default value is “ , “ if the values in the file are separated by tab or semi-colon this should be specified in this argument.
What is the separate function used for and what is it’s syntax
The separate function separates the values in one column into multiple columns. The syntax is as follows
separate(dataframe, column, into*, sep = “-“)
into is a character vector which provides the names of the new variables
In order to use this function the tidyr library must be installed and imported
What is the string detect function and what is its syntax
The string detect function is used to detect the presence or absence of a pattern in a string.
str_detect(input vector, pattern)
What is the unite function and what is it’s syntax
The unite function is used to paste together multiple columns into one.
unite(dataframe, name of new column, selection of columns to unite, sep =”_”)
What is gather function and when is it typically used.
Gather takes multiple columns and collapses into key-value pairs, duplicating all other columns as needed