views <= 14

# Function documentation

• install.packages(), which as you can expect, installs a given package.
• library() which loads packages, i.e. attaches them to the search list on your R workspace. / require()
• search(), to look at the currently attached packages and
• qplot(mtcars$wt, mtcars$hp), to build a plot of two variables of the mtcars data frame.

# a powerful package for data visualization

Use lapply with a built-in R function

To put it generally, lapply takes a vector or list X, and applies the function FUN to each of its members.

To put it generally, lapply takes a vector or list X, and applies the function FUN to each of its members.

lapply and anonymous functions

# Use anonymous function inside lapply()

## How to use sapply

sapply() simplifies the list that lapply() would return by turning it into a vector.

sapply() simplifies the list that lapply() would return by turning it into a vector.

## Use vapply

Over the elements inside X, the function FUN is applied. The FUN.VALUE argument expects a template for the return argument of this function FUN. USE.NAMES is TRUE by default; in this case vapply() tries to generate a named array, if possible.

# Data Utilities

R features a bunch of functions to juggle around with data structures::
• seq(): Generate sequences, by specifying the from, to, and by arguments.
• rep(): Replicate elements of vectors and lists.
• sort(): Sort a vector in ascending order. Works on numerics, but also on character strings and logicals.
• rev(): Reverse the elements in a data structures for which reversal is defined.
• str(): Display the structure of any R object.
• append(): Merge vectors or lists.
• is.(): Check for the class of an R object.
• as.
(): Convert an R object from one class to another.
• unlist(): Flatten (possibly embedded) lists to produce a vector.

In their most basic form, regular expressions can be used to see whether a pattern exists inside a character string or a vector of character strings. For this purpose, you can use:
• grepl(), which returns TRUE when a pattern is found in the corresponding character string.
• grep(), which returns a vector of indices of the character strings that contains the pattern.

You can use the caret, ^, and the dollar sign, $to match the content located in the start and end of a string, respectively. This could take us one step closer to a correct pattern for matching only the “.edu” email addresses from our list of emails. But there’s more that can be added to make the pattern more robust: • @, because a valid email must contain an at-sign. • ., which matches any character (.) zero or more times (). Both the dot and the asterisk are metacharacters. You can use them to match any character between the at-sign and the “.edu” portion of an email address. • \.edu​$, to match the “.edu” part of the email at the end of the string. The \\ part escapes the dot: it tells R that you want to use the . as an actual character.

While grep() and grepl() were used to simply check whether a regular expression could be matched with a character vector, sub() and gsub() take it one step further: you can specify a replacement argument. If inside the character vector x, the regular expression pattern is found, the matching element(s) will be replaced with replacement.sub() only replaces the first match, whereas gsub() replaces all matches.

Create and format dates
To create a Date object from a simple character string in R, you can use the as.Date() function. The character string has to obey a format that can be defined using a set of symbols (the examples correspond to 13 January, 1982):
• %Y: 4-digit year (1982)
• %y: 2-digit year (82)
• %m: 2-digit month (01)
• %d: 2-digit day of the month (13)
• %A: weekday (Wednesday)
• %a: abbreviated weekday (Wed)
• %B: month (January)
• %b: abbreviated month (Jan)