Assignments Flashcards
What happens if you do the following:
c(1,2,3,4) + c(5,15) ?
The second vector is recycled, so you get
6 17 8 19
What does the upward arrow do in Rstudio?
It gives the previous command
What does the tab do?
It gives suggestions when you start typing a name of a variable.
How do you change your working directory?
setwd(‘name’) sets the current working directory to the directory with that name.
How do you ask for help/info about a function?
Put a ‘?’ in front of it and remove the parentheses.
Example: ?file.exists for the file.exists() function.
How do you get the current working directory?
getwd()
How do you get a list of all the objects in the current workspace?
ls()
How do you get a list of the files in the current working directory?
list.files()
How do you create a new directory?
dir.create(‘name’)
How do you check if a file exists?
file.exists(‘name’)
How do you create a new filepath name that can be used across platforms?
file.path(‘a’, ‘b’)
How do you create a directory within a directory?
dir.create((‘nametop’, ‘namenested’), recursive = TRUE)
How do you get a sequence of the numbers 1 to 20, including 1 and 20?
1:20 or seq(1,20)
What happens when you use seq(x,y) with real numbers?
It gives the sequence from x to y (inclusive) with increments of 1. May never reach y
How do you get help/info about an operator like : ?
? :
So put the operator between back ‘s
How do you adjust the increments when creating a sequence of numbers from x to y?
seq(x,y, by = z)
With z the size of increment.
What command do you give if you want a sequence of 30 numbers between 5 and 10?
seq(5, 10, length=30)
What is the best way to get a sequence of numbers with the same length as a vector variable?
seq.along(variablename)
How do you get the length of a variable?
length(variable)
How do you create a vector of thirty zeros?
rep(0, times=30)
How do you create a vector that repeats the pattern 0 1 2 ten times?
rep(c(0,1,2), times = 10)
How do you create a vector with ten 0s, ten 1s and ten 2s?
rep(c(0,1,2), each=10)
What are the two types of vectors?
Atomic vectors and lists.
Atomatic vector
Vector that contains only one data type
Logical vectors
Atomatic vectors that can be only TRUE, FALSE or NA (not available).
What do you use to create a character vector?
Double quotes: “x”.
How do you concatenate and print the elements of a given character vector?
paste(vectorname, collapse=” “)
How do you join two character vectors v1 and v2?
paste(v1, v2, sep=” “)
What is LETTERS in R?
A predefined character vector that contains all 26 letters of the alphabet.
What happens when you use the paste() function on a number vector?
It transforms the numbers to characters, so the program will read them as chars.
What does NA represent?
A missing value.
What does an operation involving NA result in?
NA
How can you make a vector of 100 random values out of two other vectors?
sample(c(v1,v2),100)
What does is.na(vector) do?
It creates a vector with all the values in the vector that are NA.
How does R represent TRUE and FALSE?
By 1 and 0.
What happens if you get the sum of all logical values in R?
You essentially get the number of TRUE values, since these are 1.
How do you get the sum of all values of a vector?
sum(…)
What does NaN stand for?
not a number
What does inf stand for?
infinity
How do you retrieve a subset from a vector?
vectorname[0:10] for slice 0:10.
What are the four kinds of index vectors?
- Logical vectors
- Vectors of positive integers
- Vectors of negative integers
- Character vectors
What does x[is.na(x)] do?
It gives a vector containing all NAs from x.
How do you get for example the 3rd, 5th and 7th element from a vector?
vectorname[c(3,5,7)].
How do you get all elements of vector v except the second element?
v[c(-2)]
What is a name vector?
A vector that contains both names and numbers, kind of like a 2d vector.
How can you give names to a vector v with only numbers?
names(v) <- c(“name a”, “name b”, … )
How do you check whether two vectors are the same?
Identical(v1,v2)
What is the difference between a matrix and a data.frame?
A matrix can only contain one class of data, a data.frame can contain multiple.
How do you get the dimensions of an object x?
dim(x)
How do you get the class of an object?
class(x)
How do you make a matrix of r rows and c columns, containing numbers 1 through 20?
matrix(data=seq(1,20), nrows = r, ncol =c)
What does cbind() do?
It combines the columns of for example a vector and a matrix. Implicit coercion then happens.
What you need to do if you want to combine different data types?
Use data.frame(vector, matrix)
What is the difference between && and &, | and || ?
The double one && / || evaluates only the first element of a vector whilst the single one & / | evaluates all elements.
How do you use a function for checking boolean value?
isTRUE(x)
What is the function for exclusive or?
xor(x)
What does which(x) do?
It finds the indices of the object that satisfy the constraint, for example which(vector1 >5) gives indices of elements of vector1 that are bigger than 5.
How do you get a preview of a dataset saved in a data.frame?
head(x) for the first 6 rows, tail(x) for the last. Use argument n=x for x amount of rows.
What two functions can you use to get an overview of the data in an object?
summary(x) or str(x).
How do you clear an R script from all variables?
rm(list=ls())
How do you clear all the graphs from memory in R?
graphics.off()