1
2R version 3.3.2 (2016-10-31) -- "Sincere Pumpkin Patch"
3Copyright (C) 2016 The R Foundation for Statistical Computing
4Platform: x86_64-apple-darwin13.4.0 (64-bit)
5
6R is free software and comes with ABSOLUTELY NO WARRANTY.
7You are welcome to redistribute it under certain conditions.
8Type 'license()' or 'licence()' for distribution details.
9
10R is a collaborative project with many contributors.
11Type 'contributors()' for more information and
12'citation()' on how to cite R or R packages in publications.
13
14Type 'demo()' for some demos, 'help()' for on-line help, or
15'help.start()' for an HTML browser interface to help.
16Type 'q()' to quit R.
17
18> ### tests.write.fwf.R
19> ###------------------------------------------------------------------------
20> ### What: Tests for write.fwf
21> ### $Id: tests.write.fwf.R 1300 2008-08-05 11:47:18Z ggorjan $
22> ### Time-stamp: <2008-08-05 12:25:05 ggorjan>
23> ###------------------------------------------------------------------------
24>
25> library(gdata)
26gdata: read.xls support for 'XLS' (Excel 97-2004) files ENABLED.
27
28gdata: read.xls support for 'XLSX' (Excel 2007+) files ENABLED.
29
30Attaching package: 'gdata'
31
32The following object is masked from 'package:stats':
33
34    nobs
35
36The following object is masked from 'package:utils':
37
38    object.size
39
40The following object is masked from 'package:base':
41
42    startsWith
43
44>
45> ## --- Test data ---
46>
47> num <- round(c(733070.345678, 1214213.78765456, 553823.798765678,
48+                1085022.8876545678,  571063.88765456, 606718.3876545678,
49+                1053686.6, 971024.187656, 631193.398765456, 879431.1),
50+              digits=3)
51>
52> testData <- data.frame(num1=c(1:10, NA),
53+                        num2=c(NA, seq(from=1, to=5.5, by=0.5)),
54+                        num3=c(NA, num),
55+                        int1=c(as.integer(1:4), NA, as.integer(4:9)),
56+                        fac1=factor(c(NA, letters[1:9], "hjh")),
57+                        fac2=factor(c(letters[6:15], NA)),
58+                        cha1=c(letters[17:26], NA),
59+                        cha2=c(NA, "longer", letters[25:17]),
60+                        stringsAsFactors=FALSE)
61> levels(testData$fac1) <- c(levels(testData$fac1), "unusedLevel")
62> testData$Date <- as.Date("1900-1-1")
63> testData$Date[2] <- NA
64> testData$POSIXt <- as.POSIXct(strptime("1900-1-1 01:01:01", format="%Y-%m-%d %H:%M:%S"))
65> testData$POSIXt[5] <- NA
66>
67> ## --- Tests ---
68>
69> ## Default
70> write.fwf(testData)
71num1 num2 num3 int1 fac1 fac2 cha1 cha2 Date POSIXt
72 1               1     f q        1900-01-01 1900-01-01 01:01:01
73 2 1.0  733070.3 2 a   g r longer            1900-01-01 01:01:01
74 3 1.5 1214213.8 3 b   h s y      1900-01-01 1900-01-01 01:01:01
75 4 2.0  553823.8 4 c   i t x      1900-01-01 1900-01-01 01:01:01
76 5 2.5 1085022.9   d   j u w      1900-01-01
77 6 3.0  571063.9 4 e   k v v      1900-01-01 1900-01-01 01:01:01
78 7 3.5  606718.4 5 f   l w u      1900-01-01 1900-01-01 01:01:01
79 8 4.0 1053686.6 6 g   m x t      1900-01-01 1900-01-01 01:01:01
80 9 4.5  971024.2 7 h   n y s      1900-01-01 1900-01-01 01:01:01
8110 5.0  631193.4 8 i   o z r      1900-01-01 1900-01-01 01:01:01
82   5.5  879431.1 9 hjh     q      1900-01-01 1900-01-01 01:01:01
83>
84> ## NA should be -
85> write.fwf(x=testData, na="-")
86num1 num2 num3 int1 fac1 fac2 cha1 cha2 Date POSIXt
87 1   -         - 1 -   f q -      1900-01-01 1900-01-01 01:01:01
88 2 1.0  733070.3 2 a   g r longer -          1900-01-01 01:01:01
89 3 1.5 1214213.8 3 b   h s y      1900-01-01 1900-01-01 01:01:01
90 4 2.0  553823.8 4 c   i t x      1900-01-01 1900-01-01 01:01:01
91 5 2.5 1085022.9 - d   j u w      1900-01-01 -
92 6 3.0  571063.9 4 e   k v v      1900-01-01 1900-01-01 01:01:01
93 7 3.5  606718.4 5 f   l w u      1900-01-01 1900-01-01 01:01:01
94 8 4.0 1053686.6 6 g   m x t      1900-01-01 1900-01-01 01:01:01
95 9 4.5  971024.2 7 h   n y s      1900-01-01 1900-01-01 01:01:01
9610 5.0  631193.4 8 i   o z r      1900-01-01 1900-01-01 01:01:01
97 - 5.5  879431.1 9 hjh - - q      1900-01-01 1900-01-01 01:01:01
98> ## NA should be -NA-
99> write.fwf(x=testData, na="-NA-")
100num1 num2 num3 int1 fac1 fac2 cha1 cha2 Date POSIXt
101   1 -NA-      -NA-    1 -NA- f    q    -NA-   1900-01-01 1900-01-01 01:01:01
102   2  1.0  733070.3    2 a    g    r    longer -NA-       1900-01-01 01:01:01
103   3  1.5 1214213.8    3 b    h    s    y      1900-01-01 1900-01-01 01:01:01
104   4  2.0  553823.8    4 c    i    t    x      1900-01-01 1900-01-01 01:01:01
105   5  2.5 1085022.9 -NA- d    j    u    w      1900-01-01 -NA-
106   6  3.0  571063.9    4 e    k    v    v      1900-01-01 1900-01-01 01:01:01
107   7  3.5  606718.4    5 f    l    w    u      1900-01-01 1900-01-01 01:01:01
108   8  4.0 1053686.6    6 g    m    x    t      1900-01-01 1900-01-01 01:01:01
109   9  4.5  971024.2    7 h    n    y    s      1900-01-01 1900-01-01 01:01:01
110  10  5.0  631193.4    8 i    o    z    r      1900-01-01 1900-01-01 01:01:01
111-NA-  5.5  879431.1    9 hjh  -NA- -NA- q      1900-01-01 1900-01-01 01:01:01
112>
113> ## Some other separator than space
114> write.fwf(testData[, 1:4], sep="-mySep-")
115num1-mySep-num2-mySep-num3-mySep-int1
116 1-mySep-   -mySep-         -mySep-1
117 2-mySep-1.0-mySep- 733070.3-mySep-2
118 3-mySep-1.5-mySep-1214213.8-mySep-3
119 4-mySep-2.0-mySep- 553823.8-mySep-4
120 5-mySep-2.5-mySep-1085022.9-mySep-
121 6-mySep-3.0-mySep- 571063.9-mySep-4
122 7-mySep-3.5-mySep- 606718.4-mySep-5
123 8-mySep-4.0-mySep-1053686.6-mySep-6
124 9-mySep-4.5-mySep- 971024.2-mySep-7
12510-mySep-5.0-mySep- 631193.4-mySep-8
126  -mySep-5.5-mySep- 879431.1-mySep-9
127>
128> ## Justify right for character columns
129> write.fwf(testData, justify="right")
130num1 num2 num3 int1 fac1 fac2 cha1 cha2 Date POSIXt
131 1               1     f q        1900-01-01 1900-01-01 01:01:01
132 2 1.0  733070.3 2   a g r longer            1900-01-01 01:01:01
133 3 1.5 1214213.8 3   b h s      y 1900-01-01 1900-01-01 01:01:01
134 4 2.0  553823.8 4   c i t      x 1900-01-01 1900-01-01 01:01:01
135 5 2.5 1085022.9     d j u      w 1900-01-01
136 6 3.0  571063.9 4   e k v      v 1900-01-01 1900-01-01 01:01:01
137 7 3.5  606718.4 5   f l w      u 1900-01-01 1900-01-01 01:01:01
138 8 4.0 1053686.6 6   g m x      t 1900-01-01 1900-01-01 01:01:01
139 9 4.5  971024.2 7   h n y      s 1900-01-01 1900-01-01 01:01:01
14010 5.0  631193.4 8   i o z      r 1900-01-01 1900-01-01 01:01:01
141   5.5  879431.1 9 hjh          q 1900-01-01 1900-01-01 01:01:01
142>
143> ## Justify right for character columns, track na values
144> write.fwf(testData, justify="right", na="!")
145num1 num2 num3 int1 fac1 fac2 cha1 cha2 Date POSIXt
146 1   !         ! 1   ! f q      ! 1900-01-01 1900-01-01 01:01:01
147 2 1.0  733070.3 2   a g r longer          ! 1900-01-01 01:01:01
148 3 1.5 1214213.8 3   b h s      y 1900-01-01 1900-01-01 01:01:01
149 4 2.0  553823.8 4   c i t      x 1900-01-01 1900-01-01 01:01:01
150 5 2.5 1085022.9 !   d j u      w 1900-01-01                   !
151 6 3.0  571063.9 4   e k v      v 1900-01-01 1900-01-01 01:01:01
152 7 3.5  606718.4 5   f l w      u 1900-01-01 1900-01-01 01:01:01
153 8 4.0 1053686.6 6   g m x      t 1900-01-01 1900-01-01 01:01:01
154 9 4.5  971024.2 7   h n y      s 1900-01-01 1900-01-01 01:01:01
15510 5.0  631193.4 8   i o z      r 1900-01-01 1900-01-01 01:01:01
156 ! 5.5  879431.1 9 hjh ! !      q 1900-01-01 1900-01-01 01:01:01
157>
158> ## With quotes
159> write.fwf(testData, quote=TRUE)
160"num1" "num2" "num3" "int1" "fac1" "fac2" "cha1" "cha2" "Date" "POSIXt"
161" 1" "   " "         " "1" "   " "f" "q" "      " "1900-01-01" "1900-01-01 01:01:01"
162" 2" "1.0" " 733070.3" "2" "a  " "g" "r" "longer" "          " "1900-01-01 01:01:01"
163" 3" "1.5" "1214213.8" "3" "b  " "h" "s" "y     " "1900-01-01" "1900-01-01 01:01:01"
164" 4" "2.0" " 553823.8" "4" "c  " "i" "t" "x     " "1900-01-01" "1900-01-01 01:01:01"
165" 5" "2.5" "1085022.9" " " "d  " "j" "u" "w     " "1900-01-01" "                   "
166" 6" "3.0" " 571063.9" "4" "e  " "k" "v" "v     " "1900-01-01" "1900-01-01 01:01:01"
167" 7" "3.5" " 606718.4" "5" "f  " "l" "w" "u     " "1900-01-01" "1900-01-01 01:01:01"
168" 8" "4.0" "1053686.6" "6" "g  " "m" "x" "t     " "1900-01-01" "1900-01-01 01:01:01"
169" 9" "4.5" " 971024.2" "7" "h  " "n" "y" "s     " "1900-01-01" "1900-01-01 01:01:01"
170"10" "5.0" " 631193.4" "8" "i  " "o" "z" "r     " "1900-01-01" "1900-01-01 01:01:01"
171"  " "5.5" " 879431.1" "9" "hjh" " " " " "q     " "1900-01-01" "1900-01-01 01:01:01"
172>
173> ## With rownames
174> write.fwf(testData, rownames=TRUE)
175num1 num2 num3 int1 fac1 fac2 cha1 cha2 Date POSIXt
1761   1               1     f q        1900-01-01 1900-01-01 01:01:01
1772   2 1.0  733070.3 2 a   g r longer            1900-01-01 01:01:01
1783   3 1.5 1214213.8 3 b   h s y      1900-01-01 1900-01-01 01:01:01
1794   4 2.0  553823.8 4 c   i t x      1900-01-01 1900-01-01 01:01:01
1805   5 2.5 1085022.9   d   j u w      1900-01-01
1816   6 3.0  571063.9 4 e   k v v      1900-01-01 1900-01-01 01:01:01
1827   7 3.5  606718.4 5 f   l w u      1900-01-01 1900-01-01 01:01:01
1838   8 4.0 1053686.6 6 g   m x t      1900-01-01 1900-01-01 01:01:01
1849   9 4.5  971024.2 7 h   n y s      1900-01-01 1900-01-01 01:01:01
18510 10 5.0  631193.4 8 i   o z r      1900-01-01 1900-01-01 01:01:01
18611    5.5  879431.1 9 hjh     q      1900-01-01 1900-01-01 01:01:01
187>
188> ## Without colnames
189> write.fwf(testData, colnames=FALSE)
190 1               1     f q        1900-01-01 1900-01-01 01:01:01
191 2 1.0  733070.3 2 a   g r longer            1900-01-01 01:01:01
192 3 1.5 1214213.8 3 b   h s y      1900-01-01 1900-01-01 01:01:01
193 4 2.0  553823.8 4 c   i t x      1900-01-01 1900-01-01 01:01:01
194 5 2.5 1085022.9   d   j u w      1900-01-01
195 6 3.0  571063.9 4 e   k v v      1900-01-01 1900-01-01 01:01:01
196 7 3.5  606718.4 5 f   l w u      1900-01-01 1900-01-01 01:01:01
197 8 4.0 1053686.6 6 g   m x t      1900-01-01 1900-01-01 01:01:01
198 9 4.5  971024.2 7 h   n y s      1900-01-01 1900-01-01 01:01:01
19910 5.0  631193.4 8 i   o z r      1900-01-01 1900-01-01 01:01:01
200   5.5  879431.1 9 hjh     q      1900-01-01 1900-01-01 01:01:01
201>
202> ## Without colnames, with rownames
203> write.fwf(testData, colnames=FALSE, rownames=TRUE)
2041   1               1     f q        1900-01-01 1900-01-01 01:01:01
2052   2 1.0  733070.3 2 a   g r longer            1900-01-01 01:01:01
2063   3 1.5 1214213.8 3 b   h s y      1900-01-01 1900-01-01 01:01:01
2074   4 2.0  553823.8 4 c   i t x      1900-01-01 1900-01-01 01:01:01
2085   5 2.5 1085022.9   d   j u w      1900-01-01
2096   6 3.0  571063.9 4 e   k v v      1900-01-01 1900-01-01 01:01:01
2107   7 3.5  606718.4 5 f   l w u      1900-01-01 1900-01-01 01:01:01
2118   8 4.0 1053686.6 6 g   m x t      1900-01-01 1900-01-01 01:01:01
2129   9 4.5  971024.2 7 h   n y s      1900-01-01 1900-01-01 01:01:01
21310 10 5.0  631193.4 8 i   o z r      1900-01-01 1900-01-01 01:01:01
21411    5.5  879431.1 9 hjh     q      1900-01-01 1900-01-01 01:01:01
215>
216> ## With rownames and colnames and rowCol
217> write.fwf(testData, rownames=TRUE, rowCol="HI!")
218HI! num1 num2 num3 int1 fac1 fac2 cha1 cha2 Date POSIXt
2191   1               1     f q        1900-01-01 1900-01-01 01:01:01
2202   2 1.0  733070.3 2 a   g r longer            1900-01-01 01:01:01
2213   3 1.5 1214213.8 3 b   h s y      1900-01-01 1900-01-01 01:01:01
2224   4 2.0  553823.8 4 c   i t x      1900-01-01 1900-01-01 01:01:01
2235   5 2.5 1085022.9   d   j u w      1900-01-01
2246   6 3.0  571063.9 4 e   k v v      1900-01-01 1900-01-01 01:01:01
2257   7 3.5  606718.4 5 f   l w u      1900-01-01 1900-01-01 01:01:01
2268   8 4.0 1053686.6 6 g   m x t      1900-01-01 1900-01-01 01:01:01
2279   9 4.5  971024.2 7 h   n y s      1900-01-01 1900-01-01 01:01:01
22810 10 5.0  631193.4 8 i   o z r      1900-01-01 1900-01-01 01:01:01
22911    5.5  879431.1 9 hjh     q      1900-01-01 1900-01-01 01:01:01
230>
231> ## formatInfo is tested with unit tests
232>
233> ###------------------------------------------------------------------------
234> ### tests.write.fwf.R ends
235>
236> proc.time()
237   user  system elapsed
238  0.333   0.044   0.392
239