What is happening? 1. /bin/bash -c says “run the following command in bash” - /bin/bash is the path to the shell that runs the script - -c is a flag (option) that means “command”. (Don’t wait for me to type things in or look for a file; just run what comes next.)
"$(…)` runs the command inside the parentheses and substitutes its output into the command line.
curl is a command-line tool that downloads files from the web
-fsSL are four separate curl flags (options)
-f fail silently on server errors
-s silent mode (no progress bar)
-S show errors (with -s)
-L follow redirects (if the URL points somewhere else)
When you run this command, the computer runs curl first, which downloads Homebrew. Then it hands the download to bash, which runs the installer. You will be prompted for your password, and then Homebrew will install itself.
macOS: Install R and RStudio
brew install --cask rbrew install --cask rstudio
--cask means a normal Mac application, the kind that lands in /Applications
Check that it worked:
R--version
You should see something like R version 4.4.1
Windows: Install R and RStudio
Windows 10 and 11 ship with winget, the Microsoft package manager.
The condition builds a logical vector, and the brackets keep where it is TRUE
== tests equality. A single = is assignment
which(wages > 30) gives the positions where the condition holds: 3 4 5
Data Frames
From Vectors to a Table
In base R, a data frame is the standard way to represent a data set.
A data frame lines up several vectors of the same length, side by side.
Every column can be a different type, and every column must be the same length.
Building a data frame
people <-data.frame(name =c("ada", "cal", "ida"),wage =c(18, 31, 44),is_union =c(FALSE, TRUE, TRUE))people#> name wage is_union#> 1 ada 18 FALSE#> 2 cal 31 TRUE#> 3 ida 44 TRUE
Looking at a Data Frame
nrow(people) # how many rows#> [1] 3ncol(people) # how many columns#> [1] 3names(people) # the column names#> [1] "name" "wage" "is_union"str(people) # structure: names, types, first values
head(people) shows the first few rows when the table is long
Getting at individual variables
people$wage # $ = one column, as a vector#> [1] 18 31 44people[1, ] # row 1, all columnspeople[, "wage"] # the wage columnpeople[2, "wage"] # one cell#> [1] 31
$ names a column, and [row, col] indexes like a matrix
Leave a slot empty to mean “all of them”
Working with Columns
A column is a vector, so everything you already know still applies.
mean(people$wage)#> [1] 31# add a columnpeople$annual <- people$wage *40*52# keep the rows you wantpeople[people$is_union, ]#> name wage is_union annual#> 2 cal 31 TRUE 64480#> 3 ida 44 TRUE 91520
Lists
Lists are a more general container
A vector needs one type.
A data frame needs equal-length columns.
A list holds anything in one container: a number, a vector, a whole table
results[["means"]] # double brackets: the thing itself#> [1] 2.1 3.4results$means # $ works too#> [1] 2.1 3.4results["means"] # single brackets: a shorter list#> $means#> [1] 2.1 3.4
Double brackets take the element out. Single brackets give you a smaller list
Single and double brackets syntax is always a bit confusing.
Functions and Loops
Writing a Function
Write a function when you want to apply the same logic to different inputs.
# convert an hourly wage to a rough annual figureto_annual <-function(hourly) { hourly *40*52}to_annual(25)#> [1] 52000to_annual(c(18, 31)) # works on a vector too#> [1] 37440 64480