我正在 R 中运行一个示例,完成这些步骤,到目前为止一切正常,除了此代码产生错误:
words <- dtm %>%
as.matrix %>%
colnames %>%
(function(x) x[nchar(x) < 20])
错误:找不到函数“%>%”
我不明白使用这个特殊运算符有什么好处%>%
是的,任何反馈都会很棒。
您需要加载一个包(例如magrittr
or 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 %>%
管道操作员%>%
被介绍给“减少开发时间并提高代码的可读性和可维护性。”
但每个人都必须自己决定这是否真的适合他的工作流程并使事情变得更容易。
欲了解更多信息magrittr
, click here.
不使用管道%>%
,此代码将返回与您的代码相同的结果:
words <- colnames(as.matrix(dtm))
words <- words[nchar(words) < 20]
words
EDIT:(由于 @Molx 提出的非常有用的评论,我扩展了我的答案)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)