R中有“引用词”运算符吗?

时间:2015-02-28 12:43:15

标签: r quotes

R中是否有“引用词”运算符,类似于Perl中的qwqw是一个引用运算符,允许您创建引用项列表,而无需单独引用每个项。

如果没有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

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

2 个答案:

答案 0 :(得分:4)

尝试使用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")

即使引号中的数据是数字,也会返回字符串向量:

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

我不认为你会变得更简单,因为空格分隔的裸词在R中不是有效的语法,甚至用大括号或parens包裹。你必须引用它们。

答案 1 :(得分:1)

对于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"