howtofixit

Common problems in R and how to fix them!

Typos

Paste("my", "text")
#> Error in Paste("my", "text"): could not find function "Paste"

What it means: there isn’t a function called Paste.

How to fix it: check the spelling, names in R are case-sensitive and the function is called paste().

subset(df, TRUE)
#> Error in x[subset & !is.na(subset)]: object of type 'closure' is not subsettable

What it means: We commonly use df as the name for a data-frame, but df is also the name of a function and things like subset() don’t work on functions.

How to fix it: check what your data-frame is called and it’s definitely been created.

filter(mtcars, wt > 2)
#> Error: object 'wt' not found

What it means: you’ve tried to use the base filter() function but probably wanted to use filter() from dplyr instead.

How to fix it: make sure you run library(dplyr) first! Or refer to it as dplyr::filter().

mean(mtcars)
#> Warning in mean.default(mtcars): argument is not numeric or logical: returning
#> NA
#> [1] NA

What it means: mean() works with numbers and mtcars is a data-frame.

How to fix it: if you want the mean of a specific column (say wt), try mean(mtcars$wt).

x <- 2
if (x = 2) {
  print("x = 2")
}
#> Error in parse(text = input): <text>:2:7: unexpected '='
#> 1: x <- 2
#> 2: if (x =
#>          ^

What it means: R doesn’t know what if (x = 2) means!

How to fix it: to check if x is 2, use x == 2 rather than x = 2.

Files and folders

read.csv("data.csv")
#> Warning in file(file, "rt"): cannot open file 'data.csv': No such file or
#> directory
#> Error in file(file, "rt"): cannot open the connection

What it means: there isn’t a file called data.csv in the folder you’re working from.

How to fix it: check the name of your file is right, including the extension. Check where you’re running R from with getwd(). You might find it helpful to use RStudio projects to make sure you’re always working in the right place.

Packages

library(xyz)
#> Error in library(xyz) : there is no package called ‘xyz’

xyz::fn()
#> Error in loadNamespace(x) : there is no package called ‘xyz’

What it means: you’re trying to use a package you don’t have installed, either by loading the package with library() or using a function from it like stats::cov().

How to fix it: try installing the package with install.packages("xyz").

install.packages(purrr)
#> Error in install.packages : object 'purrr' not found

What it means: install.packages() is looking for an object called purrr to get the package name from.

How to fix it: make sure you quote the name of packages you want to install: install.packages("purrr").

install.packages("isthisapackagename")
#> Installing package into 'C:/Users/dof-paulinm/Documents/R/win-library/4.4'
#> (as 'lib' is unspecified)
#> Warning: package 'isthisapackagename' is not available for this version of R
#> 
#> A version of this package for your version of R might be available elsewhere,
#> see the ideas at
#> https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages

What it means: there isn’t a package with this name available to install.

How to fix it: check the spelling of the package name (remember they’re case-sensitive). If that doesn’t fix it, try searching for the package name on google to see if it’s still on CRAN.

library(rlang)
install.packages("rlang")
#> Error in install.packages : Updating loaded packages

What it means: you’ve tried to install a package that is already in use. This could be because you’ve already loaded it with library(), or it could be a package used by RStudio.

How to fix it: try restarting R (Ctrl + Shift + F10 in RStudio) and installing the package in a fresh session. If that doesn’t work, try opening R directly from the start menu and installing the package from there.

install.packages("xyz")
#> Warning in install.packages :
#>  installation of package ‘xyz’ had non-zero exit status

What it means: install.packages() hasn’t worked. This can happen for a few different reasons, like having more than one R session running or trying to install a package from source.

How to fix it: try install.packages("xyz", type = "win.binary", dependencies = TRUE). If that doesn’t fix it, make sure you don’t have any other R sessions running and try installing in a fresh R session.