R 中是否有 "quote words" 运算符?

Is there a "quote words" operator in R?

R 中是否有类似于 Perl 中的 qw 的 "quote words" 运算符? qw 是一个引用运算符,它允许您创建一个引用项目列表,而不必单独引用每个项目。

如果没有 qw(即使用几十个引号和逗号),您可以这样做:

#!/bin/env perl
use strict;
use warnings;

my @NAM_founders = ("B97",    "CML52",  "CML69", "CML103", "CML228", "CML247",
                    "CML322", "CML333", "Hp301", "Il14H",  "Ki3",    "Ki11",
                    "M37W",   "M162W",  "Mo18W", "MS71",   "NC350",  "NC358"
                    "Oh7B",   "P39",    "Tx303", "Tzi8",
                   );

print(join(" ", @NAM_founders)); # Prints array, with elements separated by spaces

这里是做同样的事情,但是 qw 更干净:

#!/bin/env perl
use strict;
use warnings;

my @NAM_founders = qw(B97    CML52  CML69  CML103 CML228 CML247 CML277
                      CML322 CML333 Hp301  Il14H  Ki3    Ki11   Ky21
                      M37W   M162W  Mo18W  MS71   NC350  NC358  Oh43
                      Oh7B   P39    Tx303  Tzi8
                   );

print(join(" ", @NAM_founders)); # Prints array, with elements separated by spaces

我已经搜索过但没有找到任何东西。

对于 R,我能想到的或到目前为止我发现的最接近的事情是创建一个文本块,然后使用 strsplit 将其拆分,因此:

#!/bin/env Rscript
NAM_founders <- "B97      CML52    CML69    CML103    CML228   CML247  CML277
                 CML322   CML333   Hp301    Il14H     Ki3      Ki11    Ky21
                 M37W     M162W    Mo18W    MS71      NC350    NC358   Oh43
                 Oh7B     P39      Tx303    Tzi8"

NAM_founders <- unlist(strsplit(NAM_founders,"[ \n]+"))

print(NAM_founders)

打印

 [1] "B97"    "CML52"  "CML69"  "CML103" "CML228" "CML247" "CML277" "CML322"
 [9] "CML333" "Hp301"  "Il14H"  "Ki3"    "Ki11"   "Ky21"   "M37W"   "M162W"
[17] "Mo18W"  "MS71"   "NC350"  "NC358"  "Oh43"   "Oh7B"   "P39"    "Tx303"
[25] "Tzi8"

尝试使用 scan 和文本连接:

qw=function(s){scan(textConnection(s),what="")}
NAM=qw("B97      CML52    CML69    CML103    CML228   CML247  CML277
                  CML322   CML333   Hp301    Il14H     Ki3      Ki11    Ky21
                  M37W     M162W    Mo18W    MS71      NC350    NC358   Oh43
                  Oh7B     P39      Tx303    Tzi8")

即使引号中的数据是数字,这也始终是 return 字符串向量:

> qw("1 2 3 4")
Read 4 items
[1] "1" "2" "3" "4"

我认为您不会变得更简单,因为 space 分隔的裸词在 R 中不是有效语法,即使包含在大括号或括号中也是如此。你必须引用它们。