...
in the worksheet with codeAt the end of this lesson, students will …
R and RStudio are software tools to help you work with and analyze your data.
c(1, 2, 3)
"USDA"
log(10)
: takes a numeric value as input and returns a numeric value as outputc(1, 5, 6)
: The function c()
takes multiple values as input and returns a vector as output.read.csv('myfile.csv')
: takes a character string as input and returns a data frame as output.+
, -
, *
, /
, ^
to use R as a calculator<-
is used to create a new variable and give it a value. The syntax is variable <- <value>
..
or _
but can’t contain spaces or start with a number.=
as an assignment operator but we will use <-
in this workshop. Consistent code is readable code!#
is a comment and will not be evaluated.()
, like function(<value>)
, will input a value to a function and return some output,
'single quotes'
or "double quotes"
?
to get help about a function??
to search all help documentation for a termCode can produce messages instead of or in addition to output:
[1]
in the output from earlier indicates it is a vector of length 1seq()
with three named arguments,
:
is shorthand'
or double quotes "
to create character vectors[]
containing one or more integer valuesTRUE
and FALSE
x == y
: is x
equal to y
?x != y
: is x
not equal to y
?x > y
: is x
greater than y
?x >= y
: is x
greater than or equal to y
?x < y
: is x
less than y
?x <= y
: is x
less than or equal to y
?x > y & x < z
: is x
greater than y
and less than z
?x > y | x < z
: is x
greater than y
or less than z
?!
is the negation operatorTRUE
values to FALSE
and vice versa.%in%
is an operator comparing two vectorsTRUE
for the values that appear anywhere in the vector on the right-hand side, and FALSE
otherwiseexp()
: the exponential of each element in the vectorPROTIP:
set.seed()
ensures the code produces the same result each time, andhead()
means only print the first few values of a result
length()
, mean()
, median()
, and sd()
return a single value.range()
returns a vector of two values, the minimum and maximum of the vectorquantile()
takes two vectors as input.
probs
, contains the probabilities we want to calculate the quantiles forprobs
containing the percentilesr
, d
, p
, and q
and followed by the (abbreviated) name of the distribution.
r
: random draws from the distributiond
: probability density function (what is the y-value of the function given x?)p
: cumulative density function: (what is the cumulative probability given x?)q
: quantile (what is the x-value given the cumulative probability?); q
is the inverse of p
.rnorm()
, dnorm()
, pnorm()
, and qnorm()
mean = 0
and sd = 1
mean
and sd
argumentsrbinom()
, dbinom()
, pbinom()
, qbinom()
)runif()
, dunif()
, punif()
, qunif()
)rt()
, dt()
, pt()
, qt()
)Type ?Distributions
in your console to see help documentation about all the built-in distributions.
If you get an error or your code doesn’t work, here are some things to check.
x<-log(500,base=2)
and x <- log(500, base = 2)
install.packages()
PROTIP: You can specify the location of the library the package will install into. This means you can specify one that doesn’t require administrator level access.
library()
::
to be explicithelp(package = 'packagename')
.Ctrl+Enter
(Win) or Cmd+Enter
(Mac)Those are really important things but we aren’t going to cover them in this lesson. I strongly encourage you to explore the R resources I’ve provided to learn more. And maybe I’ll discuss them in a future workshop.
Go to the lesson page and try out the exercises!