我在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"
答案 0 :(得分:3)
使用粘贴:
warning(paste0('"', paste(a, collapse = '" "'), '"'))
Warning message:
"sepal_length" "sepal_width" "petal_length" "petal_width" "species"
或@akrun建议使用dQuote
。这使用花式引号,中间没有空格:
warning(dQuote(a))
Warning message:
“sepal_length”“sepal_width”“petal_length”“petal_width”“species”
答案 1 :(得分:1)
以下是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"