Questions and Answers
1 Questions about R
1.1 Why doesn’t my select
/filter
statement work?
When you are loading packages, sometimes different packages have the same function names in them, and the functions themselves will do very different things. For example, there is a select
function in the tidyverse
, but also another select
function in the package MASS
that does something very different. If we load the tidyverse before loading MASS, then the MASS version of select is the one that will be used?!
To get around this make sure that you load the tidyverse
after MASS
, to be safe you should always load the tidyverse
last.
# A tibble: 53,940 × 3
carat cut color
<dbl> <ord> <ord>
1 0.23 Ideal E
2 0.21 Premium E
3 0.23 Good E
4 0.29 Premium I
5 0.31 Good J
6 0.24 Very Good J
7 0.24 Very Good I
8 0.26 Very Good H
9 0.22 Fair E
10 0.23 Very Good H
# ℹ 53,930 more rows
you can also specify the package that select comes from (in this case from a package within the tidyverse
called dplyr
):
# A tibble: 53,940 × 3
carat cut color
<dbl> <ord> <ord>
1 0.23 Ideal E
2 0.21 Premium E
3 0.23 Good E
4 0.29 Premium I
5 0.31 Good J
6 0.24 Very Good J
7 0.24 Very Good I
8 0.26 Very Good H
9 0.22 Fair E
10 0.23 Very Good H
# ℹ 53,930 more rows
Finally, there is a package that helps your deal with conflicts called conflicted
. At the top of your script you can define which version of a function you prefer, this will be the version that will always be used. If you want to specify another version of a function, you can still use other versions of the function using the ::
notation:
To find out if you have any conflicts, you can run conflict_scout()
.
1.2 How can I unload packages
If you are finding yourself with a conflict as mentioned above and want to unload
packages, then you need to run the following code:
# adapted from: @mmfrgmpds https://stackoverflow.com/questions/7505547/detach-all-packages-while-working-in-r
while(!is.null(sessionInfo()$loadedOnly)){
lapply(names(sessionInfo()$loadedOnly), require, character.only = TRUE)
invisible(lapply(paste0('package:', names(sessionInfo()$otherPkgs)), detach, character.only=TRUE, unload=TRUE, force=TRUE))
}
1.3 Why am I getting the error ‘could not find function “%>%”’
The pipe operator %>%
is loaded when you load the tidyverse package - make sure you have installed tidyverse and loaded it
1.4 I am getting the error: ‘“Error: Mapping should be created with aes()
or aes_()
.”’ when using ggplot
This may be caused by having a bracket after the geom rather than before it
1.5 My axis labels are too long
If you want to use long axis titles you may find they overrun the space available
To insert a line break in the axis label, add \n
to the text where you want line breaks.
1.6 How can I find out the full item labels in the PISA data frame?
The code below will give you a list of all the item labels