23

任何人都知道 R 是否有类似 Perl 的类似引号的运算符qw()来生成字符向量?

4

6 回答 6

25

不,但你可以自己写:

q <- function(...) {
  sapply(match.call()[-1], deparse)
}

只是为了证明它有效:

> q(a, b, c)
[1] "a" "b" "c"
于 2009-07-31T19:37:52.080 回答
11

我已经在我的 Rprofile.site 文件中添加了这个函数(看看?Startup你是否不熟悉)

qw <- function(x) unlist(strsplit(x, "[[:space:]]+"))

qw("You can type    text here
    with    linebreaks if you
    wish")
#  [1] "You"        "can"        "type"       "text"      
#  [5] "here"       "with"       "linebreaks" "if"        
#  [9] "you"        "wish"    
于 2012-04-04T01:56:45.197 回答
8

流行的Hmisc 包提供了Cs()执行此操作的功能:

library(Hmisc)
Cs(foo,bar)
[1] "foo" "bar"

它使用与哈德利的回答类似的策略:

Cs
function (...) 
{
    if (.SV4. || .R.) 
        as.character(sys.call())[-1]
    else {
        y <- ((sys.frame())[["..."]])[[1]][-1]
        unlist(lapply(y, deparse))
    }
}
<environment: namespace:Hmisc>
于 2011-06-28T23:17:07.220 回答
5
qw = function(s) unlist(strsplit(s,' '))
于 2010-12-12T08:31:59.693 回答
3

更简单:

qw <- function(...){
as.character(substitute(list(...)))[-1]
}
于 2015-05-15T22:20:48.597 回答
1

适用于传入向量的情况的片段,例如,v=c('apple','apple tree','apple cider'). You would get c('"apple"','"apple tree"','"apple cider"')

quoted = function(v){
    base::strsplit(paste0('"', v, '"',collapse = '/|||/'), split = '/|||/',fixed = TRUE)[[1]]
}
于 2018-11-05T16:29:26.150 回答