我正在R中运行一个示例,完成这些步骤,到目前为止一切正常,除非此代码产生错误:
words <- dtm %>%
as.matrix %>%
colnames %>%
(function(x) x[nchar(x) < 20])
Run Code Online (Sandbox Code Playgroud)
错误:找不到函数"%>%"
我不明白使用这个特殊操作符的好处是什么
%>%,任何反馈都会很棒.
maR*_*tin 92
您需要加载一个首先定义函数的包(如magrittr或dplyr),然后它应该工作.
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 %>%
Run Code Online (Sandbox Code Playgroud)
%>%引入管道操作员"以减少开发时间并提高代码的可读性和可维护性."
但是每个人都必须自己决定它是否真的适合他的工作流程并使事情变得更容易.有关更多信息 magrittr,请单击此处.
不使用管道%>%,此代码将返回与您的代码相同:
words <- colnames(as.matrix(dtm))
words <- words[nchar(words) < 20]
words
Run Code Online (Sandbox Code Playgroud)
编辑:( 我正在扩展我的答案,因为@Molx做了一个非常有用的评论)
尽管来自
magrittr,管道操作员更常用于包装dplyr(需要和装载magrittr),所以每当你看到有人使用时%>%确保你不应该加载dplyr.
Con*_*ngo 13
在Windows上:如果您使用%>%A%dopar%循环内部,你必须添加一个引用加载封装dplyr(或magrittr,它dplyr加载).
例:
plots <- foreach(myInput=iterators::iter(plotCount), .packages=c("RODBC", "dplyr")) %dopar%
{
return(getPlot(myInput))
}
Run Code Online (Sandbox Code Playgroud)
如果省略该.packages命令,并使用%do%它来使所有命令在一个进程中运行,那么工作正常.原因是它全部在一个进程中运行,因此不需要专门加载新包.