Error: Could Not Find Function "%>%"

Error: could not find function ... in R

There are a few things you should check :

  1. Did you write the name of your function correctly? Names are case sensitive.
  2. Did you install the package that contains the function? install.packages("thePackage") (this only needs to be done once)
  3. Did you attach that package to the workspace ?
    require(thePackage) (and check its return value) or library(thePackage) (this should be done every time you start a new R session)
  4. Are you using an older R version where this function didn't exist yet?
  5. Are you using a different version of the specific package? This could be in either direction: functions are added and removed over time, and it's possible the code you're referencing is expecting a newer or older version of the package than what you have installed.

If you're not sure in which package that function is situated, you can do a few things.

  1. If you're sure you installed and attached/loaded the right package, type help.search("some.function") or ??some.function to get an information box that can tell you in which package it is contained.
  2. find and getAnywhere can also be used to locate functions.
  3. If you have no clue about the package, you can use findFn in the sos package as explained in this answer.
  4. RSiteSearch("some.function") or searching with rdocumentation or rseek are alternative ways to find the function.

Sometimes you need to use an older version of R, but run code created for a newer version. Newly added functions (eg hasName in R 3.4.0) won't be found then. If you use an older R version and want to use a newer function, you can use the package backports to make such functions available. You also find a list of functions that need to be backported on the git repo of backports. Keep in mind that R versions older than R3.0.0 are incompatible with packages built for R3.0.0 and later versions.

Error: could not find function % %

You need to load a package (like magrittr or dplyr) that defines the function first, then it should work.

install.packages("magrittr") # package installations are only needed the first time you use it
install.packages("dplyr") # alternative installation of the %>%
library(magrittr) # needs to be run every time you start R and want to use %>%
library(dplyr) # alternatively, this also loads %>%

The pipe operator %>% was introduced to "decrease development time and to improve readability and maintainability of code."

But everybody has to decide for himself if it really fits his workflow and makes things easier.
For more information on magrittr, click here.

Not using the pipe %>%, this code would return the same as your code:

words <- colnames(as.matrix(dtm))
words <- words[nchar(words) < 20]
words

EDIT:
(I am extending my answer due to a very useful comment that was made by @Molx)

Despite being from magrittr, the pipe operator is more commonly used
with the package dplyr (which requires and loads magrittr), so
whenever you see someone using %>% make sure you shouldn't load dplyr
instead.

How to fix the R/exams error could not find function answerlist

I cannot replicate this problem using the exercise you provided, for me the code works. You should move the text with the table into the Question section, though.

What might have caused the problem is that you did not fully load and attach the R package exams via library("exams") and only used the fully-qualified exams::exams2pdf(...) without attaching the package. If so, then you also need the fully-qualified exams::answerlist(...) and exams::mchoice2string(...) etc. to use the functions from the package. (Personally, I load and attach the package with library("exams") before compiling any exercises.)

error in could not find function in r with pipeline

%>% is from the package "dplyr". So make sure you load it, i.e. library(dplyr).

Next, %>% does not assign the result to a variable. I.e.

a %>% mutate(foo=bar(x))

does not alter a. It will just show the result on the console (and none if you are running the script or calling it from a function).

You might be confusing the pipe-operator with %<>% (found in the package magrittr) which uses the left-hand variable as input for the pipe, and overwrites the variable with the modified result.

Finally, when you write

If I try to assign it to a variable (e.g. baseline <- a %>% filter(Period == "Baseline") %>% group_by(File)%>%)

You are assigning the result from the pipeline to a variable baseline -- this however does not modify the variable-names in the data frames (i.e. the column names).

drop_na in RStudio

You can use this code:

library(palmerpenguins)
library(tidyverse)
penguins %>% group_by(island) %>% drop_na(.) %>% summarise(mean_bill_length_mm = mean(bill_length_mm))

Output:

# A tibble: 3 × 2
island mean_bill_length_mm
<fct> <dbl>
1 Biscoe 45.2
2 Dream 44.2
3 Torgersen 39.0


Related Topics



Leave a reply



Submit