【问题标题】:Warning function in RR中的警告功能
【发布时间】:2017-05-04 11:05:29
【问题描述】:

我在 R 中有一个字符列表。我正在执行以下查询:

> a <- c("sepal_length" ,"sepal_width",  "petal_length" ,"petal_width" , "species")
> a
[1] "sepal_length" "sepal_width"  "petal_length" "petal_width" 
[5] "species"     
> warning(a)
Warning message:
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies 

请注意,上述警告消息正在删除字符值之间的空格。我只想使用警告功能,有什么办法可以得到以下格式化输出。

> warning(f(a))
Warning message:
[1] "sepal_length" "sepal_width"  "petal_length" "petal_width" 
[5] "species"

【问题讨论】:

  • 你在写奇怪的警告信息...
  • 哈哈.. 实际上整个警告信息会是这样的。警告:列名更改为“sepal_length”“sepal_width”“petal_length”“petal_width”“species”

标签: r


【解决方案1】:

使用粘贴:

warning(paste0('"', paste(a, collapse = '" "'), '"'))

Warning message:
"sepal_length" "sepal_width" "petal_length" "petal_width" "species" 

或如@akrun 建议的那样,使用dQuote。这使用 fancy 引号,中间没有空格:

warning(dQuote(a))

Warning message:
“sepal_length”“sepal_width”“petal_length”“petal_width”“species” 

【讨论】:

  • 这是一个花哨的报价,但我不确定它在警告消息中是否重要
【解决方案2】:

这是sprintf()的解决方案:

a <- c("sepal_length" ,"sepal_width",  "petal_length" ,"petal_width" , "species")
warning(sprintf('"%s" ', a))
# Warning message:
# "sepal_length" "sepal_width" "petal_length" "petal_width" "species" 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多