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

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

问题描述

我正在 R 中运行一个示例,执行这些步骤,到目前为止一切正常,除了此代码产生错误:

 字 <- dtm %>%as.matrix%>%列名 %>%(函数(x) x[nchar(x) <20])

<块引用>

错误:找不到函数%>%"

我不明白使用这个特殊运算符有什么好处%>% 是,任何反馈都会很棒.

解决方案

你需要先加载一个定义函数的包(比如 magrittrdplyr),然后它应该可以工作.

install.packages("magrittr") # 包安装只在第一次使用时才需要install.packages("dplyr") # %>% 的替代安装library(magrittr) # 每次启动 R 时都需要运行,想使用 %>%library(dplyr) # 或者,这也会加载 %>%

管道运算符 %>% 被引入以减少开发时间并提高代码的可读性和可维护性."

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

不使用管道 %>%,此代码将返回与您的代码相同的代码:

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

<小时>

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

<块引用>

尽管来自 magrittr,但管道运算符更常用使用包 dplyr(需要并加载 magrittr),所以每当您看到有人使用 %>% 时,请确保您不应该加载 dplyr相反.

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.

解决方案

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.

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

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