2013-10-10 92 views
3

在r中創建學校日曆很酷,爲什麼noquote函數不能刪除矩陣中的引號?爲什麼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.....) 
+2

嘗試'print(y,quote = FALSE)'。 – A5C1D2H2I1M1N2O1R2T1

+2

或者只是'noquote(y)'!!! –

+0

格式(x,「%m%d」))您忘記的展位? – alap

回答

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 
1

該矩陣中沒有引號。這就是打印功能顯示字符向量的方式。 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然後除去大部分的屬性。

+0

'noquote(y)'也會尊重矩陣的結構並打印出行和列名稱。 –

+0

對。 '''據我所知'noquote(y)'只添加一個類屬性。這是打印的'print.noquote'功能。不知怎的,它讓我困擾,你可以對矩陣做這件事。我想我寧願控制'print'操作而不是掌握屬性。 –

相關問題