3

在 r 中创建校历很酷,为什么 noquote 函数无法删除矩阵中的引号?

start<-as.Date("2013-09-02")
x<-start+0:139
rows<-paste(1:20,"th","week",sep="")
cols<-c("mon","tue","wed","thu","fri","sat","sun") 
y<-matrix(noquote(as.character(format(x,"%m%d"))),nrow=20,byrow=TRUE,dimnames=list(rows,cols))
y
          mon    tue    wed    thu    fri    sat    sun   
1thweek  "0902" "0903" "0904" "0905" "0906" "0907" "0908"
2thweek  "0909" "0910" "0911" "0912" "0913" "0914" "0915"
(omitted.....)

为什么我无法获得以下格式:

        mon    tue    wed    thu    fri    sat    sun   
1thweek  0902  0903   0904 0905 0906 0907 0908
(omitted.....)
4

2 回答 2

4

将呼叫交换到matrixnoquote。比较:

matrix(letters, nrow = 2)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
## [1,] "a"  "c"  "e"  "g"  "i"  "k"  "m"  "o"  "q"  "s"   "u"   "w"   "y"  
## [2,] "b"  "d"  "f"  "h"  "j"  "l"  "n"  "p"  "r"  "t"   "v"   "x"   "z"  
matrix(noquote(letters), nrow = 2)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
## [1,] "a"  "c"  "e"  "g"  "i"  "k"  "m"  "o"  "q"  "s"   "u"   "w"   "y"  
## [2,] "b"  "d"  "f"  "h"  "j"  "l"  "n"  "p"  "r"  "t"   "v"   "x"   "z" 
noquote(matrix(letters, nrow = 2))
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
## [1,] a    c    e    g    i    k    m    o    q    s     u     w     y    
## [2,] b    d    f    h    j    l    n    p    r    t     v     x     z 
于 2013-10-10T07:50:19.810 回答
1

该矩阵中没有引号。这正是 print 函数显示字符向量的方式。该noquote函数改变打印行为。您可以使用 获得相同的输出cat,尽管它无法识别矩阵结构。使用 print(y, quote=FALSE) 的建议似乎是最好的,因为它会尊重矩阵的结构并打印出行名和列名。

y<-matrix(format(x,"%m%d"),nrow=20,byrow=TRUE,dimnames=list(rows,cols))
cat(y)
 0902 0909 0916 0923 0930 1007 1014 1021 1028 1104 1111 1118 1125 1202
 1209 1216 1223 1230 0106 0113 0903 0910 0917 0924 1001 1008 1015 1022
 1029 1105 1112 1119 1126 1203 1210 1217 1224 1231 0107 0114 0904 0911
 0918 0925 1002 1009 1016 1023 1030 1106 1113 1120 1127 1204 1211 1218
 1225 0101 0108 0115 0905 0912 0919 0926 1003 1010 1017 1024 1031 1107
 1114 1121 1128 1205 1212 1219 1226 0102 0109 0116 0906 0913 0920 0927
 1004 1011 1018 1025 1101 1108 1115 1122 1129 1206 1213 1220 1227 0103
 0110 0117 0907 0914 0921 0928 1005 1012 1019 1026 1102 1109 1116 1123
 1130 1207 1214 1221 1228 0104 0111 0118 0908 0915 0922 0929 1006 1013
 1020 1027 1103 1110 1117 1124 1201 1208 1215 1222 1229 0105 0112 0119

print(y,quote=FALSE)
         mon  tue  wed  thu  fri  sat  sun 
1thweek  0902 0903 0904 0905 0906 0907 0908
2thweek  0909 0910 0911 0912 0913 0914 0915
3thweek  0916 0917 0918 0919 0920 0921 0922
4thweek  0923 0924 0925 0926 0927 0928 0929
5thweek  0930 1001 1002 1003 1004 1005 1006
6thweek  1007 1008 1009 1010 1011 1012 1013
7thweek  1014 1015 1016 1017 1018 1019 1020
8thweek  1021 1022 1023 1024 1025 1026 1027
9thweek  1028 1029 1030 1031 1101 1102 1103
10thweek 1104 1105 1106 1107 1108 1109 1110
11thweek 1111 1112 1113 1114 1115 1116 1117
12thweek 1118 1119 1120 1121 1122 1123 1124
13thweek 1125 1126 1127 1128 1129 1130 1201
14thweek 1202 1203 1204 1205 1206 1207 1208
15thweek 1209 1210 1211 1212 1213 1214 1215
16thweek 1216 1217 1218 1219 1220 1221 1222
17thweek 1223 1224 1225 1226 1227 1228 1229
18thweek 1230 1231 0101 0102 0103 0104 0105
19thweek 0106 0107 0108 0109 0110 0111 0112
20thweek 0113 0114 0115 0116 0117 0118 0119

表单matrix(noquote(...))未成功的原因是noquote向对象添加类属性作为信号以调度到print.noquote不显示引号但matrix随后删除大多数属性的方法。

于 2013-10-10T08:40:35.043 回答