在 R 中,如何使用 melt() 隐藏警告消息?

In R, using melt(), how can I hide warning messages?

我正在融化一些数据,不想提供 id.var 参数来融化。数据很好,但我得到

"No id variables; using all as measure variables"

有没有办法阻止该消息出现,或者说 id.var=default 或类似的东西?使用 dplyr 的虹膜示例:

> dt <- iris %>% summarize_at(c("Sepal.Length","Sepal.Width"), funs(mean))
> dt
  Sepal.Length Sepal.Width
1     5.843333    3.057333
> melt(dt, value.name="Mean")
No id variables; using all as measure variables
      variable     Mean
1 Sepal.Length 5.843333
2  Sepal.Width 3.057333

或者有没有办法告诉函数不要打印警告消息或类似的东西?谢谢!

严格来说,这是一条消息,而不是警告。 (参见 ?message?warning)。您可以使用 suppressMessages

抑制消息
suppressMessages({
  reshape2::melt(head(mtcars))
})

具体来说melt,可以使用id.vars = NULL。 (感谢@user20650)