As for package:
how to load package ( we must know the difference between load and install)
# Load the ggplot2 package
library("ggplot2")
# Retry the qplot() function
qplot(mtcars$wt, mtcars$hp)
# Check out the currently attached packages again
search()
we can use search to find out how many packages we have already installed
use split to seperate names from pioneers vectors
# The vector pioneers has already been created for you
pioneers <- c("GAUSS:1777", "BAYES:1702", "PASCAL:1623", "PEARSON:1857")
# Split names from birth year
split_math <- strsplit(pioneers, split = ":")
# Convert to lowercase strings: split_low
split_low<-lapply(split_math,tolower)
# Take a look at the structure of split_low
str(split_low)
Here is code about how to use the lapply function:
lapply(x, FUN) FUN is function, x is the vector, the varable
# Code from previous exercise:
pioneers <- c("GAUSS:1777", "BAYES:1702", "PASCAL:1623", "PEARSON:1857")
split <- strsplit(pioneers, split = ":")
split_low <- lapply(split, tolower)
# Write function select_first()
select_first <- function(x) {
x[1]
}
# Apply select_first() over split_low: names
names<-lapply(split_low,select_first)
# Write function select_second()
select_second<-function(y){
y[2]
}
# Apply select_second() over split_low: years
years<-lapply(split_low,select_second)
names
years
As for the anonymous function:
# split_low has been created for you
split_low
# Transform: use anonymous function inside lapply
function(x) {x[1]}
names <- lapply(split_low, function(x) {x[1]})
# Transform: use anonymous function inside lapply
function(x) {x[2]}
years <- lapply(split_low, function(x) {x[2]})
use the function on spit_low, change the index and then assign the index to names and years
# Definition of split_low
pioneers <- c("GAUSS:1777", "BAYES:1702", "PASCAL:1623", "PEARSON:1857")
split <- strsplit(pioneers, split = ":")
split_low <- lapply(split, tolower)
# Generic select function
select_el <- function(x, index) {
x[index]
}
# Use lapply() twice on split_low: names and years
names<-lapply(split_low,select_el,index=1)
years<-lapply(split_low,select_el,index=2)
EXERCISE
Apply functions that return NULL
In all of the previous exercises, it was assumed that the functions that were applied over vectors and lists actually returned a meaningful result. For example, the tolower()
function simply returns the strings with the characters in lowercase. This won't always be the case. Suppose you want to display the structure of every element of a list. You could use the str()
function for this, which returns NULL
:
lapply(list(1, "a", TRUE), str)
This call actually returns a list, the same size as the input list, containing all NULL
values. On the other hand calling
str(TRUE)
on its own prints only the structure of the logical to the console, not NULL
. That's because str()
uses invisible()
behind the scenes, which returns an invisible copy of the return value, NULL
in this case. This prevents it from being printed when the result of str()
is not assigned.
nchar takes a character vector as an argument and returns a vector whose elements contain the sizes of the corresponding elements of x.
In the next couple of exercises, you'll be working with the variable temp, that contains temperature measurements for 7 days. temp is a list of length 7, where each element is a vector of length 5, representing 5 measurements on a given day. This variable has already been defined in the workspace: type str(temp) to see its structure.
the difference between lapply and sapply, sapply returns a vector for example:
lapply returns:
1
2
3
but sapply returns:
1,2,3
The former returns a list, while the latter returns a vector that is a simplified version of this list. Notice that this time, unlike in the cities example of the instructional video, the vector is not named
In the previous exercises, you've seen how sapply()
simplifies the list that lapply()
would return by turning it into a vector. But what if the function you're applying over a list or a vector returns a vector of length greater than 1?
And then, for another exercise:
we have 2 vectors
# temp is already available in the workspace
# Create a function that returns min and max of a vector: extremes
extremes <- function(x) {
c(min = min(x), max = max(x))
}
# Apply extremes() over temp with sapply()
sapply(temp,extremes)
# Apply extremes() over temp with lapply()
lapply(temp,extremes)
here is the result:
sapply(temp,extremes)
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
min -1 5 -3 -2 2 -3 1
max 9 13 8 7 9 9 9
lapply(temp,extremes)
[[1]]
min max
-1 9
[[2]]
min max
5 13
........
Next exercise:
- Apply
below_zero()
overtemp
usingsapply()
and store the result infreezing_s
. - Apply
below_zero()
overtemp
usinglapply()
. Save the resulting list in a variablefreezing_l
. - Compare
freezing_s
tofreezing_l
using theidentical()
function.
Next exercise:
firs,about the function cat()
cat converts its arguments to character strings, concatenates them, separating them by the given sep= string, and then prints them. No linefeeds are printed unless explicitly requested by "\n" or if generated by filling (if argument fill is TRUE or numeric.) cat is useful for producing output in user defined functions.
# Definition of print_info()
print_info <- function(x) {
cat("The average temperature is", mean(x), "\n")
}
# Apply print_info() over temp using sapply()
sapply(temp,print_info)
# Apply print_info() over temp using lapply()
lapply(temp,print_info)
vapply() function:
vapply(X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE)
Math.difftime# temp is already available in the workspace
# Definition of basics()
basics <- function(x) {
c(min = min(x), mean = mean(x), max = max(x))
}
# Apply basics() over temp using vapply()
vapply(temp,basics,numeric(3))
just as with sapply(), vapply() neatly transfers the names that you specify in the basics() function to the row names of the matrix that it returns.
the result:
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
min -1.0 5 -3.0 -2.0 2.0 -3.0 1.0
mean 4.8 9 2.2 2.4 5.4 4.6 4.6
max 9.0 13 8.0 7.0 9.0 9.0 9.0
FUN
, does not correspond to the template you specify in FUN.VALUE
. In that case, vapply()
will throw an error that informs you about the misalignment between expected and actual output.