functions, factors, data frames, lists Flashcards
write a function
functionname <- function(arg1, arg2){
expressions
value_to_return
}
How can you see the source code for function x?
Type x in console
Write a function that takes another function as an argument
evaluate <- function(func, dat){
func(dat)
}
what does lapply() do?
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)
What does sapply do?
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
What does vapply do?
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))
What does tapply do?
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)
Encode the following vector as factors:
sex_vector <- c(“Male”,”Female”,”Female”,”Male”,”Male”)
factor(sex_vector)
Change the names of the levels of factor_vector
levels(factor_vector) <- c(“name1”, “name2”,…)
Create an ordered factor for my_vector with levels “slow”, “medium”, “fast”
ordered_vector <- factor(my_vector, ordered=TRUE, levels=c(“slow”, “medium”, “fast”))
Print out first 5 obs of a dataframe
Print out last 5 obs of a dataframe
head(dataframe)
tail(dataframe)
What does str() tell you about a dataframe?
- The total number of observations (e.g. 32 car types)
- The total number of variables (e.g. 11 car features)
- A full list of the variables names (e.g. mpg, cyl … )
- The data type of each variable (e.g. num)
- The first observations
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)
planets_df <- data.frame(name, type, diameter, rotation, rings)
Two ways to select column1 from a dataframe
dataframe[,”column1”]
dataframe$column1
How to subset a dataframe
subset(my_df, subset = some_condition)