Send code to run in the console:
- Run the whole chunk with the green play button (top right of the chunk)
- Run single line with ⌘+return or ctrl+return
Send code to run in the console:
2 + 2
[1] 4
2 * 4
[1] 8
2^3
[1] 8
Note: when you enter your command in the Console, R inherently thinks you want to print the result.
2 + (2 * 3)^2
[1] 38
(1 + 3) / 2 + 45
[1] 47
6 / 2 * (1 + 2)
[1] 9
Try evaluating the following:
2 + 2 * 3 / 4 -3
2 * 3 / 4 * 2
2^4 - 1
#
creates a comment in R code
# this is a comment # nothing to its right is evaluated # this # is still a comment ### you can use many #'s as you want 1 + 2 # Can be the right of code
[1] 3
In an .Rmd
file, you can write notes outside the R chunks.
object: an object is something that can be worked with or on in R - can be lots of different things! You can think of objects as nouns in R.
<-
to assign values to an object name (you might also see =
used, but this is not best practice)x <- 2 x
[1] 2
x * 4
[1] 8
x + 2
[1] 4
data.frame
data.frame
s are somewhat advanced objects in R; we will start with simpler objectsBut what can we do with objects… ?
function: a piece of code that allows you to do something in R. You can write your own, use functions that come directly from installing R, or use functions from code developers.
You can think of a function as verb in R.
A function might help you add numbers together, create a plot, or organize your data.
class()
function to check the class of an object.class(x)
[1] "numeric"
y <- "hello world!" class(y)
[1] "character"
We will talk in-depth about classes. For now:
numeric
2
character
"hello!"
Object names are case-sensitive, i.e., X
and x
are different
x
[1] 2
X
Error in eval(expr, envir, enclos): object 'X' not found
Commas separate objects in R, so they shouldn’t be used when entering big numbers.
z <- 3,000
Error: <text>:1:7: unexpected ',' 1: z <- 3, ^
10 /
Error: <text>:2:0: unexpected end of input 1: 10 / ^
+
indicates an incomplete statement. Hit “esc” to clear and bring back the >
.
Try assigning your full name to an R object called name
Try assigning your full name to an R object called name
name <- "Ava Hoffman" name
[1] "Ava Hoffman"
c()
The function c()
collects/combines/joins single R objects into a vector of R objects. It is mostly used for creating vectors of numbers, character strings, and other data types.
x <- c(1, 4, 6, 8) x
[1] 1 4 6 8
class(x)
[1] "numeric"
c()
Try assigning your first and last name as 2 separate character strings into a single vector called name2
c()
Try assigning your first and last name as 2 separate character strings into a length-2 vector called name2
name2 <- c("Ava", "Hoffman") name2
[1] "Ava" "Hoffman"
argument - information you pass to a function, usually inside of parentheses. These tell the function how to work and what to work on, sort of like an adverb.
name2 <- c("Ava", "Hoffman") # Arg 1 ^^^^^
name2 <- c("Ava", "Hoffman") # Arg 2 ^^^^^^^^^
length
of R objectslength()
: Get or set the length of vectors (including lists) and factors, and of any other R object for which a method has been defined.
length(x)
[1] 4
y
[1] "hello world!"
length(y)
[1] 1
length
of R objectsWhat do you expect for the length of the name
object? What about the name2
object?
What are the lengths of each?
length
of R objectsWhat do you expect for the length of the name
object? What about the name2
object?
What are the lengths of each?
length(name)
[1] 1
length(name2)
[1] 2
You can get more attributes than just class. The function str()
gives you the structure of the object.
str(x)
num [1:4] 1 4 6 8
str(y)
chr "hello world!"
This tells you that x
is a numeric vector and tells you the length.
Package - a package in R is a bundle or “package” of code (and or possibly data) that can be loaded together for easy repeated use or for sharing with others.
Packages are analogous to a software application like Microsoft Word on your computer. Your operating system allows you to use it, just like having R installed (and other required packages) allows you to use packages.
Most of the packages we will use will come from CRAN.
Packages must be both installed and loaded.
Installation – Must be done once for each installation of R.
Loading – Must be done every time you open R/RStudio.
You can install packages from CRAN using the tool menu in RStudio:
tools > Install Packages
Type in the package name to install.
We use a function called install.packages()
for CRAN packages.
Here is an example where we “install” the tidyverse
package:
install.packages("tidyverse")
The package name is enclosed in quotation marks.
After installing packages, you will need to “load” them into memory so that you can use them.
This must be done every time you start R.
We use a function called library
to load packages.
Here is an example where we “load” the tidyverse
package. This is an important package of many objects for dealing with data!
library(tidyverse)
Quotation marks are optional.
When you type in a function name, a pop up will preview documentation to help you. It also helps you remember the name of the function if you don’t remember all of it!
?
If you know the name of a package or function:
Type ?package_name
or ?function_name
in the console to get information about packages and functions.
For example: ?readr
or ?read_csv
.
If you haven’t loaded a package yet into R than you may get a response that there is no documentation.
Typing in ??package_name
can show you packages that you haven’t loaded yet.
<-
to save (assign) values to objectsc()
to combine vectorslength()
, class()
, and str()
tell you information about an objectinstall.packages()
library()
?
or help pane