functions, factors, data frames, lists Flashcards

1
Q

write a function

A

functionname <- function(arg1, arg2){

expressions

value_to_return

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you see the source code for function x?

A

Type x in console

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write a function that takes another function as an argument

A

evaluate <- function(func, dat){

func(dat)

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what does lapply() do?

A

Note that write function without ()

It takes a list as input, applies a function to each element of the list, then returns a list of the same length as the original one

lapply(list, function)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does sapply do?

A

Basically the same thing as lapply, but instead of returning a list, it returns a vector (when each element of the list is of length 1) or matrix (when each element of the list is a vector of the same length >1)

s stands for simplify

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does vapply do?

A

Tells R to return a character vector of length 1 for each column of the dataset

Takes a list and applies function to it. You can specify the form of the output,

eg. vapply(dataset, function, character(1))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does tapply do?

A

applies the mean function to the ‘animate’ variable separately for each of the ‘landmass’ groups

Allows you to split data into groups, then apply a function to the members of each group.

eg. tapply(flags$animate, flags$landmass, mean)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Encode the following vector as factors:

sex_vector <- c(“Male”,”Female”,”Female”,”Male”,”Male”)

A

factor(sex_vector)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Change the names of the levels of factor_vector

A

levels(factor_vector) <- c(“name1”, “name2”,…)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Create an ordered factor for my_vector with levels “slow”, “medium”, “fast”

A

ordered_vector <- factor(my_vector, ordered=TRUE, levels=c(“slow”, “medium”, “fast”))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Print out first 5 obs of a dataframe

Print out last 5 obs of a dataframe

A

head(dataframe)

tail(dataframe)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does str() tell you about a dataframe?

A
  1. The total number of observations (e.g. 32 car types)
  2. The total number of variables (e.g. 11 car features)
  3. A full list of the variables names (e.g. mpg, cyl … )
  4. The data type of each variable (e.g. num)
  5. The first observations
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Create a dataframe from the following vectors and call it planets_df:

name <- c(“Mercury”, “Venus”, “Earth”, “Mars”, “Jupiter”, “Saturn”, “Uranus”, “Neptune”)
type <- c(“Terrestrial planet”, “Terrestrial planet”, “Terrestrial planet”,
“Terrestrial planet”, “Gas giant”, “Gas giant”, “Gas giant”, “Gas giant”)
diameter <- c(0.382, 0.949, 1, 0.532, 11.209, 9.449, 4.007, 3.883)
rotation <- c(58.64, -243.02, 1, 1.03, 0.41, 0.43, -0.72, 0.67)
rings <- c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE)

A

planets_df <- data.frame(name, type, diameter, rotation, rings)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Two ways to select column1 from a dataframe

A

dataframe[,”column1”]

dataframe$column1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to subset a dataframe

A

subset(my_df, subset = some_condition)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to sort dataframe by column1

A

my_df[order(my_df$column1),]

17
Q

Create a list

Can a list hold different types of data in it?

A

my_list <- list(component1, component2…)

YES, a list can hold vectors, matrices, dataframes, and other lists

18
Q

Three ways to select the first component (comp1) from a list

A

my_list[[1]]

my_list[[“comp1”]]

my_list$comp1

19
Q

Add one component (compx) to my_list

A

ext_list <- c(my_list, compx)