R语言使用dplyr时出现‘找不到函数"%.%"’错误,新手求助
%.%函数找不到的错误 Hey there! Let's break down what's going on here and fix that error quickly.
错误原因
The %.% operator was used in very old versions of dplyr (pre-0.4.0, which is outdated now). Modern dplyr (version 1.0.0+) uses the %>% pipe operator instead. Since you're loading a current version of dplyr, R can't locate the outdated %.% function, which causes the error you're seeing.
修正后的代码
You have two solid options to fix this:
Option 1: Use dplyr's %>% pipe (most widely used)
Replace all instances of %.% with %>%. Here's your updated code:
library(MASS) library(dplyr) painters %>% group_by(school) %>% mutate(Len = n()) %>% filter(Len > 6)
Option 2: Use R's native |> pipe (R 4.1.0+)
If you're using a newer version of R (4.1.0 or later), you can use the built-in pipe operator |> instead—no extra setup needed beyond loading your required packages:
library(MASS) library(dplyr) painters |> group_by(school) |> mutate(Len = n()) |> filter(Len > 6)
Quick tip for new R users
Both pipe operators do the same core job: they pass the result from the left side of the pipe into the first argument of the function on the right. This makes your code way easier to read and follow, compared to nesting functions inside each other.
If you want to confirm your dplyr version, run this command:
packageVersion("dplyr")
You should see a version number like ‘1.1.0’ or higher—all these versions use %>% (or |>) instead of the old %.%.
内容的提问来源于stack exchange,提问作者user1652926

