2 A
2.1 absolute path
A file path that starts with / and is not appended to the working directory
For example, if your working directory is /Users/me/study/
and you want to refer to a file in the directory data
called faces.csv
, the absolute path is /Users/me/study/data/faces.csv
.
However, you should normally use relative paths in scripts, not absolute paths, which can change when you move a project directory to a new computer or even a new place on your computer.
The package R.utils has a function getAbsolutePath()
that returns the absolute path of a file path or a file object.
R.utils::getAbsolutePath("../index.Rmd")
#> [1] "/Users/lisad/rproj/psyteachr/index.Rmd"
2.2 adjusted R-squared
A modified version of R-squared adjusted for the number of predictors in the model.
2.3 alpha
(stats) The cutoff value for making a decision to reject the null hypothesis; (graphics) A value between 0 and 1 used to control the levels of transparency in a plot
Can also be a parameter in the beta distribution or refer to Cronbach's alpha.
2.4 alpha (stats)
If you are using null hypothesis significance testing (NHST), then you need to decide on a cutoff value called alpha for making a decision to reject the null hypothesis. We call p-values below the alpha cutoff significant.
In psychology, alpha is traditionally set at \(\alpha\) = .05, but there are good arguments for setting a different criterion in some circumstances.
2.5 alpha (graphics)
A value between 0 and 1 used to control the levels of transparency in a plot
# if you omit alpha, it defaults to 1
alpha1.00 <- ggplot(diamonds, aes(x = depth, fill = cut)) +
geom_density() + xlim(55, 70)
alpha0.25 <- ggplot(diamonds, aes(x = depth, fill = cut)) +
geom_density(alpha = 0.25) + xlim(55, 70)
cowplot::plot_grid(alpha1.00, alpha0.25, nrow = 2,
labels = c("alpha = 1.0", "alpha = 0.25"))

Figure 2.1: Setting alpha to a number lower than 1 lets you see parts of the plot hidden behind an object.
2.6 ampersand
The symbol &, an operator meaning "AND".
A single ampersand is vectorized, so compares each item in the first vector with the corresponding item in the second vector.
A double ampersand is not vectorised, so will ignore all but the first item in vectors, unless your environment has set _R_CHECK_LENGTH_1_LOGIC2_=true
, in which case you will get an error.
The advantage of a double ampersand is that it will stop as soon as the conclusion is obvious. So if the first item is FALSE, the second item won't even be run. This is useful for testing whether an object exists before checking something that requires the object to exist.
if (is.character(x)) {
# do something
}
#> Error in eval(expr, envir, enclos): object 'x' not found
if (exists("x") && is.character(x)) {
# do something
}
2.7 anti_join
A filtering join that returns all rows from the left table where there are not matching values in the right table, keeping just columns from the left table.

Figure 2.2: Anti Join
This is useful when you have a table of data that contains IDs you want to exclude from your main dataset.
all_data <- tibble(
id = 1:5,
x = LETTERS[1:5]
)
to_exclude <- tibble(
id = 2:4
)
data <- anti_join(all_data, to_exclude, by = "id")
id | x |
---|---|
1 | A |
5 | E |
See joins for other types of joins and further resources.
2.8 argument
A variable that provides input to a function.
For example, the first argument to the function rnorm()
is n
(the number of observations).
When you look up the help for a function (e.g., ?sd
), you will see a section called Arguments
, which lists the argument names and their definitions.
The function args()
will show you the argument names and their default values (if any) for any function.
args(rnorm)
#> function (n, mean = 0, sd = 1)
#> NULL
2.9 array
A container that stores objects in one or more dimensions.
You can create an array by specifying a list or vector of values and the number of dimensions. The first two dimensions are rows and columns; each dimension after that is printed separately as a facet.
# 3-dimensional array with 4 rows, 3 columns, and 2 facets
array(1:24, dim = c(4, 3, 2))
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 1 5 9
#> [2,] 2 6 10
#> [3,] 3 7 11
#> [4,] 4 8 12
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 13 17 21
#> [2,] 14 18 22
#> [3,] 15 19 23
#> [4,] 16 20 24
You can give your array dimensions names.
dimnames <- list(
subj_id = c("S1", "S2", "S3", "S4"),
face_id = c("F1", "F2", "F3"),
condition = c("Control", "experimental")
)
array(1:24, dim = c(4, 3, 2), dimnames = dimnames)
#> , , condition = Control
#>
#> face_id
#> subj_id F1 F2 F3
#> S1 1 5 9
#> S2 2 6 10
#> S3 3 7 11
#> S4 4 8 12
#>
#> , , condition = experimental
#>
#> face_id
#> subj_id F1 F2 F3
#> S1 13 17 21
#> S2 14 18 22
#> S3 15 19 23
#> S4 16 20 24
Objects not need to be the same data type.
2.10 aspect ratio
The ratio between the width and height of an image.
You can specify the aspect ratio of your plots by setting the width and height in the first chunk of an R Markdown file like this:
knitr::opts_chunk$set(
fig.width = 7, # default value is 7
fig.height = 7/1.618 # rolden ratio; default value is 7
)
Or in the chunk options:
```{r fig-golden-ratio, fig.width = 7, fig.height = 4.32}
ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Species)) +
geom_point() +
geom_smooth(method = lm, formula = y~x)
```

Figure 2.3: Plot with a 1.618:1 aspect ratio (golden ratio)
```{r fig-square, fig.width = 7, fig.height = 7}
ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Species)) +
geom_point() +
geom_smooth(method = lm, formula = y~x)
```

Figure 2.4: Plot with a 1:1 aspect ratio
Or when you save the image:
ggsave("golden-ratio.jpg", width = 7, height = 7/1.618)
2.11 assignment operator
The symbol <-
, which functions like =
and assigns the value on the right to the object on the left
a <- 1
a
#> [1] 1
The assignment operator can also be reversed
2 -> b
b
#> [1] 2
And even go in both direction at the same time!
x <- 2 -> y
x
#> [1] 2
y
#> [1] 2
2.12 atomic
Only containing objects with the same data type (e.g., all numeric or character).
Vectors are atomic data structures in R.
a <- c(1, 4, 32)
b <- c("somebody", "once", "told me")
class(a)
#> [1] "numeric"
class(b)
#> [1] "character"
If you try to mix data types in an atomic vector, they will be coerced to be the same type. (Note that 32 becomes "32" so it can be in the same vector as a character object).
c(32, TRUE, "eighteen")
#> [1] "32" "TRUE" "eighteen"
Another feature of vectors that makes them atomic is that they are always flat. If you embed vectors within vectors, they will be flattened.
Lists, on the other hand, can be nested.
list(7, c(1, 4, 32), 2, 6) %>% str()
#> List of 4
#> $ : num 7
#> $ : num [1:3] 1 4 32
#> $ : num 2
#> $ : num 6
If you try to extract objects inside an atomic vector, you need to use brackets []
. This can be with an index or a name. Single brackets can return one or more elements, and include names, while double brackets can only return one element and don't have names.
my_vec <- c(x = 3, y = 1, z = 4)
my_vec[2]
#> y
#> 1
my_vec["y"]
#> y
#> 1
my_vec[["y"]]
#> [1] 1
You cannot use the $ operator on an atomic vector, or you will get this very common error.
my_vec$y
#> Error in my_vec$y: $ operator is invalid for atomic vectors
See the Vectors chapter of Advanced R for more advanced discussion of atomic vectors.
2.13 attribute
Extra information about an R object
You can access an object's attributes with attr()
. For example, datasets simulated with faux have an attribute called "design" that details their design.
data <- faux::sim_design(
between = list(pet = c(cat = "Cat Owners",
dog = "Dog Owners")),
within = list(time = c(am = "Morning",
pm = "Night")),
mu = 1:4,
r = list(cat = 0.3, dog = 0.6),
dv = c(score = "Happiness Scale Score"),
plot = FALSE)
attr(data, "design")
#> * [DV] score: Happiness Scale Score
#> * [ID] id: id
#> * Within-subject variables:
#> * time:
#> * am: Morning
#> * pm: Night
#> * Between-subject variables:
#> * pet:
#> * cat: Cat Owners
#> * dog: Dog Owners
#> * Parameters:
#> <table>
#> <thead>
#> <tr>
#> <th style="text-align:left;"> pet </th>
#> <th style="text-align:left;"> time </th>
#> <th style="text-align:right;"> am </th>
#> <th style="text-align:right;"> pm </th>
#> <th style="text-align:right;"> n </th>
#> <th style="text-align:right;"> mu </th>
#> <th style="text-align:right;"> sd </th>
#> </tr>
#> </thead>
#> <tbody>
#> <tr>
#> <td style="text-align:left;"> cat </td>
#> <td style="text-align:left;"> am </td>
#> <td style="text-align:right;"> 1.0 </td>
#> <td style="text-align:right;"> 0.3 </td>
#> <td style="text-align:right;"> 100 </td>
#> <td style="text-align:right;"> 1 </td>
#> <td style="text-align:right;"> 1 </td>
#> </tr>
#> <tr>
#> <td style="text-align:left;"> cat </td>
#> <td style="text-align:left;"> pm </td>
#> <td style="text-align:right;"> 0.3 </td>
#> <td style="text-align:right;"> 1.0 </td>
#> <td style="text-align:right;"> 100 </td>
#> <td style="text-align:right;"> 2 </td>
#> <td style="text-align:right;"> 1 </td>
#> </tr>
#> <tr>
#> <td style="text-align:left;"> dog </td>
#> <td style="text-align:left;"> am </td>
#> <td style="text-align:right;"> 1.0 </td>
#> <td style="text-align:right;"> 0.6 </td>
#> <td style="text-align:right;"> 100 </td>
#> <td style="text-align:right;"> 3 </td>
#> <td style="text-align:right;"> 1 </td>
#> </tr>
#> <tr>
#> <td style="text-align:left;"> dog </td>
#> <td style="text-align:left;"> pm </td>
#> <td style="text-align:right;"> 0.6 </td>
#> <td style="text-align:right;"> 1.0 </td>
#> <td style="text-align:right;"> 100 </td>
#> <td style="text-align:right;"> 4 </td>
#> <td style="text-align:right;"> 1 </td>
#> </tr>
#> </tbody>
#> </table>
2.14 attribute (html)
Extra information about an HTML element
For example, the paragraph element has an attribute of "id" with a value of "feature". This can be used to refer to the element to change its style with CSS or affect its behaviour with JavaScript.
<p id="feature">This is the main paragraoh of my text...</p>