错误:找不到函数“%>%”。 [英] Error: could not find function "%>%"

查看:95
本文介绍了错误:找不到函数“%>%”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在R中运行一个示例,逐步进行操作,到目前为止,所有工作都正常,除了此代码会产生错误:

I'm running an example in R, going through the steps and everything is working so far except for this code produces an error:

 words <- dtm %>%
 as.matrix %>%
 colnames %>%
 (function(x) x[nchar(x) < 20])




错误:找不到函数%>%

Error: could not find function "%>%"

我不明白使用此特殊运算符
%>%有什么好处是,任何反馈都很好。

I don't understand what the benefit of using this special operator %>% is, and any feedback would be great.

推荐答案

您需要加载软件包(例如 magrittr dplyr )首先定义该函数,然后它应该起作用。

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."

但是每个人都必须自己决定是否真的适合他的工作流程并使事情变得容易。
有关 magrittr 的更多信息,请单击此处

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






编辑:
(由于@Molx发表了非常有用的评论,我在扩展我的答案)


尽管源于 magrittr ,但管道运算符更常见于
和软件包 dplyr (需要并加载 magrittr ),因此只要您看到有人在使用%>%确保不要加载 dplyr

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.

这篇关于错误:找不到函数“%&gt;%”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆