• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/42

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

42 Cards in this Set

  • Front
  • Back

What data type is a single number?
E.g. x <- 1

A length-one vector - not a scalar

Three properties of a vector

type, length, attributes

Four types of atomic vectors (plus two rare?)

logical, integer, double, character (complex, raw)

Talk to me about NA values in vectors and coercion

Any NA in a vector will be coerced to the correct type, respecting the construction/storage rules for vectors

What is the hierarchy of coercion (also, vectors & lists?)

logical < integer < double < character


(vector + list) -> list gets coerced to list

What are the three most important attributes (of a vector)?

Name(s), class, length(s)

What type of vector is a factor?

Integer!

What happens to a factor when you modify its levels?

The levels are actually names, and apply arbitrarily to the values in the vector. Switching one doesn't switch the other, but switching both is identical to the original

What's the difference between a matrix and an array?

The number of dimensions (matrix is a special case of an array that has two)

How are data frames like lists?

They're actually a list of vectors, and can hold different data types, as long as the lists (i.e. columns) are all the same length ("exception" - as long as the row dimensionality is correct, you can store an array in a column)

What are the three subsetting operators?

$, [, @

What is the result of subsetting a vector with positive integers, negative integers, a logical vector, or a character vector?

Get some indices, remove some indices, get some indices, and get some named indices

What's the difference between [, [[, and $ when applied to a list

[ gets the list at an index, [[ the contents at an index, and $ is like [[

How can you use a named vector to relabel categorical variables?

A named character vector can act as a simple lookup table: c(x = 1, y = 2, z = 3)[c("y", "z", "x")]

How do you get a simple vector from a data frame by subsetting? How do you get a small version of the data frame instead?

matrix subsetting [,"x"], list subsetting ["a"]

What's the difference between subsetting and preserving, and what are the consequences for each data type?

preserving keeps the same type of variable. When simplified instead, atomic vectors lose their names, data frames lose all irrelevant columns and become vectors, lists return the object instead of a list (unless the object is a list), factors drop unused levels, and matrices/arrays drop unused dimensions

how would you uncollapse a dataframe that has been aggregated by row

Use the rep function to generate indices for the number of times a row has been repeated, and then subset the dataframe with those indices

How would you order a data frame by row? by column?

df[order(df$x),]

df[,order(names(df))]


How would you match a list of (e.g.) integer values to a table describing the properties of those integers (e.g. grades to pass/fail and letter grade)?

match from the grade vector to the data frame's grade vector - this gives positions with which to subset the dataframe

What are the three parts of a function?

Body, arguments, environment

what are the four principles of lexical scoping

fresh start, name masking, functions vs. variables, dynamic lookup

How do you send a list of arguments to a function?

do.call(mean,list(1:10, na.rm = TRUE))

how do you define an infix function?

'%...%' <-

what is a replacement function and how do you define one?

a replacement function modifies its argument (e.g. a vector) in place (e.g. changing the second value of a vector)




defined with 'name<-' <- function(x, ..., value)




All of that's necessary, and gives you access to the object itself as well as the value you want to replace one of its elements with




n.b. this is a modified copy, not an actual modification (this behavior is standard)

how do you make sure that a certain portion of code executes before/as a function ends?

on.exit() before the "risky" part of the function

How is an environment different from a list?

There are four ways: every object in an environment must have a name; order doesn’t matter; environments have parents; environments have reference semantics.


Also, rm() is the only way to get rid of things in an environment (not <-NULL)

Talk to me about the global environment and parents and parentless environments

The parent of the global environment is the last package that you loaded. The only environment that doesn’t have a parent is the empty environment.

Which is the enclosing environment of a function and why does it matter?

Wherever it was created, and this determines where it looks for values of variables

How do you determine the environment from which a function was called?

use parent.frame()

How do you bind to the parent environment? How do you copy to the current environment

This is the difference between <- and <<-, the former of which copies in the current environment

What's the easiest way to view an environment's namespace and structure?

ls() and ls.str() respectively

How does subsetting with respect to an environment work?

Exactly like you'd expect, as long as you have the environment and object's names (e$a[[1]])

How do you look for particular bindings in an environment?

get() or exists(), both of which can take inherits = false

How do you figure out which environment a binding is tied to?

where("name","parent to start with"

What are the three relevant environments for a function?

Binding (enclosing), executing, and calling.




Binding is where it is named (<-; special case: enclosing environment can be the same), executing is ephemeral and where it does its running, and calling is the place from which the execution was initiated

What's the most useful/central function for debugging? What are some others?

traceback()




breakpoint (shift+f9) or browser() (or options(error = browser) in a function

What are the useful functions inside browser()?

n, execute the next command; s, step into the next function; f, finish the current loop or function; c, continue execution normally; Q, stop the function and return to the console.

What function can you use to avoid errors with some block of code?

try()

Talk to me about try()

Useful for handling error-prone code - can produce a message on error, can have its output captured (error or not), can be particularly useful for lapply or assigning default values (default<-value; try(default<-riskybusiness)

Talk to me about tryCatch()

This allows you to assign particular behavior to each of the handlers (error, warning, etc.)

How do you make a list of functions, and then call from it?

This isn't do.call(), actually.




funlist <- list (name1 <- function(x) x/2, ...)




system.time(funlist[[1]](x))




Or, use lapply(vectorx, function(x) f(x))

How would you create a function to create power-generating functions (x^y)?

powers <- function(exp) {


function (x) {


x^^exp


}


}