Two installs, in this order. R itself comes from CRAN, the Comprehensive R Archive Network - that is the language and the interpreter. RStudio Desktop, from Posit, is the environment you will actually work in, and it needs R to already be there. Installing RStudio first and wondering why nothing runs is the usual false start.
The four panes. RStudio's default layout is worth understanding rather than tolerating. The Console, bottom left, runs one line at a time and is where you experiment. The Source editor, top left, is where the script you are keeping lives; Ctrl+Enter (Cmd+Enter on a Mac) sends the current line to the console, which is how you actually work - write in the editor, run from it, never type into the console what you want to keep. The Environment pane, top right, lists every object currently in memory and is the fastest way to see what a line did to your data. Plots, Files, Packages and Help share the bottom right.
One setting to change on day one: Tools → Global Options → General, and turn off "Restore .RData into workspace at startup", and set "Save workspace to .RData on exit" to Never. Otherwise R silently reloads yesterday's objects every morning, and you end up with analyses that work only on your machine because they depend on something invisible. A script should produce its results from nothing.
Assignment uses <-. = also works and is not wrong, but <- is the convention everywhere in R, and there is one place they genuinely differ: inside a function call, f(x = 1) names an argument while f(x <- 1) assigns to x and passes the value. Use <- for assignment and = for arguments and the ambiguity never arises. Alt+- types it.
Packages are where R's real power is - around 20,000 on CRAN. install.packages("dplyr") downloads and installs, once, permanently. library(dplyr) loads it into the current session, and must be run again every session. That distinction confuses everybody once: installing is like buying the book, loading is like taking it off the shelf. Never put install.packages() in a script you share - it modifies the reader's machine every time they run it.
# ---- R as a calculator ----------------------------------------------
2 + 2
10 / 3 # 3.333333 - R does not truncate, unlike C or Python 2
10 %/% 3 # 3 - integer division
10 %% 3 # 1 - modulo
2^10 # 1024 - ^ not **
# ---- Assignment ------------------------------------------------------
x <- 5 # the convention. Alt+- types it.
y = 3 # works, and is not what R code looks like
7 -> z # legal, and nobody does this
# Why <- is worth the habit:
# mean(x = c(1, 2, 3)) names the argument. x is NOT created.
# mean(x <- c(1, 2, 3)) assigns to x AND passes it. Different thing.
x + y # 8
# ---- Everything is a vector -----------------------------------------
# There is no scalar type in R. This is not pedantry; it explains the
# whole language.
length(5) # 1 - the number 5 is a vector of length one
is.vector(5) # TRUE
# Which is why arithmetic is elementwise with no loop in sight:
c(1, 2, 3) * 2 # 2 4 6
c(1, 2, 3) + c(10, 20, 30) # 11 22 33
# ---- Asking R what something is -------------------------------------
class(x) # "numeric"
typeof(x) # "double" - class is what it behaves as, typeof is
# how it is stored. They differ often.
str(x) # "structure": the one function to reach for first
summary(x)
# ---- Packages --------------------------------------------------------
# install.packages("dplyr") # ONCE, ever. Not in a shared script.
# library(dplyr) # EVERY session. Put this at the top.
# Use one function from a package without loading it:
# dplyr::filter(data, x > 5)
#
# The :: form is also how you resolve a conflict. dplyr::filter and
# stats::filter are different functions with the same name, and
# whichever you loaded last wins - which is why library() prints those
# "The following objects are masked from" messages. They are not
# warnings to ignore; they are telling you which filter you now have.
# ---- Getting help ----------------------------------------------------
?mean # the help page
??regression # search all help pages for a phrase
example(mean) # run the examples from the help page
Discussion
No questions on this topic yet. Sign in to ask the first one.