File: tests.write.fwf.Rout.save

package info (click to toggle)
gdata 3.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 964 kB
  • sloc: sh: 27; makefile: 15
file content (223 lines) | stat: -rw-r--r-- 11,301 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223

R version 4.3.0 (2023-04-21) -- "Already Tomorrow"
Copyright (C) 2023 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(gdata)

Attaching package: 'gdata'

The following object is masked from 'package:stats':

    nobs

The following object is masked from 'package:utils':

    object.size

The following object is masked from 'package:base':

    startsWith

> 
> ## Test data
> 
> num <- round(c(733070.345678, 1214213.78765456, 553823.798765678,
+                1085022.8876545678, 571063.88765456, 606718.3876545678,
+                1053686.6, 971024.187656, 631193.398765456, 879431.1), digits=3)
> 
> testData <- data.frame(num1=c(1:10, NA),
+                        num2=c(NA, seq(from=1, to=5.5, by=0.5)),
+                        num3=c(NA, num),
+                        int1=c(as.integer(1:4), NA, as.integer(4:9)),
+                        fac1=factor(c(NA, letters[1:9], "hjh")),
+                        fac2=factor(c(letters[6:15], NA)),
+                        cha1=c(letters[17:26], NA),
+                        cha2=c(NA, "longer", letters[25:17]),
+                        stringsAsFactors=FALSE)
> levels(testData$fac1) <- c(levels(testData$fac1), "unusedLevel")
> testData$Date <- as.Date("1900-1-1")
> testData$Date[2] <- NA
> testData$POSIXt <- as.POSIXct(strptime("1900-1-1 01:01:01",
+                                        format="%Y-%m-%d %H:%M:%S"))
> testData$POSIXt[5] <- NA
> 
> ## Tests
> 
> ## Default
> write.fwf(testData)
num1 num2 num3 int1 fac1 fac2 cha1 cha2 Date POSIXt
 1               1     f q        1900-01-01 1900-01-01 01:01:01
 2 1.0  733070.3 2 a   g r longer            1900-01-01 01:01:01
 3 1.5 1214213.8 3 b   h s y      1900-01-01 1900-01-01 01:01:01
 4 2.0  553823.8 4 c   i t x      1900-01-01 1900-01-01 01:01:01
 5 2.5 1085022.9   d   j u w      1900-01-01                    
 6 3.0  571063.9 4 e   k v v      1900-01-01 1900-01-01 01:01:01
 7 3.5  606718.4 5 f   l w u      1900-01-01 1900-01-01 01:01:01
 8 4.0 1053686.6 6 g   m x t      1900-01-01 1900-01-01 01:01:01
 9 4.5  971024.2 7 h   n y s      1900-01-01 1900-01-01 01:01:01
10 5.0  631193.4 8 i   o z r      1900-01-01 1900-01-01 01:01:01
   5.5  879431.1 9 hjh     q      1900-01-01 1900-01-01 01:01:01
> 
> ## NA should be -
> write.fwf(x=testData, na="-")
num1 num2 num3 int1 fac1 fac2 cha1 cha2 Date POSIXt
 1   -         - 1 -   f q -      1900-01-01 1900-01-01 01:01:01
 2 1.0  733070.3 2 a   g r longer -          1900-01-01 01:01:01
 3 1.5 1214213.8 3 b   h s y      1900-01-01 1900-01-01 01:01:01
 4 2.0  553823.8 4 c   i t x      1900-01-01 1900-01-01 01:01:01
 5 2.5 1085022.9 - d   j u w      1900-01-01 -                  
 6 3.0  571063.9 4 e   k v v      1900-01-01 1900-01-01 01:01:01
 7 3.5  606718.4 5 f   l w u      1900-01-01 1900-01-01 01:01:01
 8 4.0 1053686.6 6 g   m x t      1900-01-01 1900-01-01 01:01:01
 9 4.5  971024.2 7 h   n y s      1900-01-01 1900-01-01 01:01:01
10 5.0  631193.4 8 i   o z r      1900-01-01 1900-01-01 01:01:01
 - 5.5  879431.1 9 hjh - - q      1900-01-01 1900-01-01 01:01:01
> ## NA should be -NA-
> write.fwf(x=testData, na="-NA-")
num1 num2 num3 int1 fac1 fac2 cha1 cha2 Date POSIXt
   1 -NA-      -NA-    1 -NA- f    q    -NA-   1900-01-01 1900-01-01 01:01:01
   2  1.0  733070.3    2 a    g    r    longer -NA-       1900-01-01 01:01:01
   3  1.5 1214213.8    3 b    h    s    y      1900-01-01 1900-01-01 01:01:01
   4  2.0  553823.8    4 c    i    t    x      1900-01-01 1900-01-01 01:01:01
   5  2.5 1085022.9 -NA- d    j    u    w      1900-01-01 -NA-               
   6  3.0  571063.9    4 e    k    v    v      1900-01-01 1900-01-01 01:01:01
   7  3.5  606718.4    5 f    l    w    u      1900-01-01 1900-01-01 01:01:01
   8  4.0 1053686.6    6 g    m    x    t      1900-01-01 1900-01-01 01:01:01
   9  4.5  971024.2    7 h    n    y    s      1900-01-01 1900-01-01 01:01:01
  10  5.0  631193.4    8 i    o    z    r      1900-01-01 1900-01-01 01:01:01
-NA-  5.5  879431.1    9 hjh  -NA- -NA- q      1900-01-01 1900-01-01 01:01:01
> 
> ## Some other separator than space
> write.fwf(testData[, 1:4], sep="-mySep-")
num1-mySep-num2-mySep-num3-mySep-int1
 1-mySep-   -mySep-         -mySep-1
 2-mySep-1.0-mySep- 733070.3-mySep-2
 3-mySep-1.5-mySep-1214213.8-mySep-3
 4-mySep-2.0-mySep- 553823.8-mySep-4
 5-mySep-2.5-mySep-1085022.9-mySep- 
 6-mySep-3.0-mySep- 571063.9-mySep-4
 7-mySep-3.5-mySep- 606718.4-mySep-5
 8-mySep-4.0-mySep-1053686.6-mySep-6
 9-mySep-4.5-mySep- 971024.2-mySep-7
10-mySep-5.0-mySep- 631193.4-mySep-8
  -mySep-5.5-mySep- 879431.1-mySep-9
> 
> ## Justify right for character columns
> write.fwf(testData, justify="right")
num1 num2 num3 int1 fac1 fac2 cha1 cha2 Date POSIXt
 1               1     f q        1900-01-01 1900-01-01 01:01:01
 2 1.0  733070.3 2   a g r longer            1900-01-01 01:01:01
 3 1.5 1214213.8 3   b h s      y 1900-01-01 1900-01-01 01:01:01
 4 2.0  553823.8 4   c i t      x 1900-01-01 1900-01-01 01:01:01
 5 2.5 1085022.9     d j u      w 1900-01-01                    
 6 3.0  571063.9 4   e k v      v 1900-01-01 1900-01-01 01:01:01
 7 3.5  606718.4 5   f l w      u 1900-01-01 1900-01-01 01:01:01
 8 4.0 1053686.6 6   g m x      t 1900-01-01 1900-01-01 01:01:01
 9 4.5  971024.2 7   h n y      s 1900-01-01 1900-01-01 01:01:01
10 5.0  631193.4 8   i o z      r 1900-01-01 1900-01-01 01:01:01
   5.5  879431.1 9 hjh          q 1900-01-01 1900-01-01 01:01:01
> 
> ## Justify right for character columns, track na values
> write.fwf(testData, justify="right", na="!")
num1 num2 num3 int1 fac1 fac2 cha1 cha2 Date POSIXt
 1   !         ! 1   ! f q      ! 1900-01-01 1900-01-01 01:01:01
 2 1.0  733070.3 2   a g r longer          ! 1900-01-01 01:01:01
 3 1.5 1214213.8 3   b h s      y 1900-01-01 1900-01-01 01:01:01
 4 2.0  553823.8 4   c i t      x 1900-01-01 1900-01-01 01:01:01
 5 2.5 1085022.9 !   d j u      w 1900-01-01                   !
 6 3.0  571063.9 4   e k v      v 1900-01-01 1900-01-01 01:01:01
 7 3.5  606718.4 5   f l w      u 1900-01-01 1900-01-01 01:01:01
 8 4.0 1053686.6 6   g m x      t 1900-01-01 1900-01-01 01:01:01
 9 4.5  971024.2 7   h n y      s 1900-01-01 1900-01-01 01:01:01
10 5.0  631193.4 8   i o z      r 1900-01-01 1900-01-01 01:01:01
 ! 5.5  879431.1 9 hjh ! !      q 1900-01-01 1900-01-01 01:01:01
> 
> ## With quotes
> write.fwf(testData, quote=TRUE)
"num1" "num2" "num3" "int1" "fac1" "fac2" "cha1" "cha2" "Date" "POSIXt"
" 1" "   " "         " "1" "   " "f" "q" "      " "1900-01-01" "1900-01-01 01:01:01"
" 2" "1.0" " 733070.3" "2" "a  " "g" "r" "longer" "          " "1900-01-01 01:01:01"
" 3" "1.5" "1214213.8" "3" "b  " "h" "s" "y     " "1900-01-01" "1900-01-01 01:01:01"
" 4" "2.0" " 553823.8" "4" "c  " "i" "t" "x     " "1900-01-01" "1900-01-01 01:01:01"
" 5" "2.5" "1085022.9" " " "d  " "j" "u" "w     " "1900-01-01" "                   "
" 6" "3.0" " 571063.9" "4" "e  " "k" "v" "v     " "1900-01-01" "1900-01-01 01:01:01"
" 7" "3.5" " 606718.4" "5" "f  " "l" "w" "u     " "1900-01-01" "1900-01-01 01:01:01"
" 8" "4.0" "1053686.6" "6" "g  " "m" "x" "t     " "1900-01-01" "1900-01-01 01:01:01"
" 9" "4.5" " 971024.2" "7" "h  " "n" "y" "s     " "1900-01-01" "1900-01-01 01:01:01"
"10" "5.0" " 631193.4" "8" "i  " "o" "z" "r     " "1900-01-01" "1900-01-01 01:01:01"
"  " "5.5" " 879431.1" "9" "hjh" " " " " "q     " "1900-01-01" "1900-01-01 01:01:01"
> 
> ## With rownames
> write.fwf(testData, rownames=TRUE)
num1 num2 num3 int1 fac1 fac2 cha1 cha2 Date POSIXt
1   1               1     f q        1900-01-01 1900-01-01 01:01:01
2   2 1.0  733070.3 2 a   g r longer            1900-01-01 01:01:01
3   3 1.5 1214213.8 3 b   h s y      1900-01-01 1900-01-01 01:01:01
4   4 2.0  553823.8 4 c   i t x      1900-01-01 1900-01-01 01:01:01
5   5 2.5 1085022.9   d   j u w      1900-01-01                    
6   6 3.0  571063.9 4 e   k v v      1900-01-01 1900-01-01 01:01:01
7   7 3.5  606718.4 5 f   l w u      1900-01-01 1900-01-01 01:01:01
8   8 4.0 1053686.6 6 g   m x t      1900-01-01 1900-01-01 01:01:01
9   9 4.5  971024.2 7 h   n y s      1900-01-01 1900-01-01 01:01:01
10 10 5.0  631193.4 8 i   o z r      1900-01-01 1900-01-01 01:01:01
11    5.5  879431.1 9 hjh     q      1900-01-01 1900-01-01 01:01:01
> 
> ## Without colnames
> write.fwf(testData, colnames=FALSE)
 1               1     f q        1900-01-01 1900-01-01 01:01:01
 2 1.0  733070.3 2 a   g r longer            1900-01-01 01:01:01
 3 1.5 1214213.8 3 b   h s y      1900-01-01 1900-01-01 01:01:01
 4 2.0  553823.8 4 c   i t x      1900-01-01 1900-01-01 01:01:01
 5 2.5 1085022.9   d   j u w      1900-01-01                    
 6 3.0  571063.9 4 e   k v v      1900-01-01 1900-01-01 01:01:01
 7 3.5  606718.4 5 f   l w u      1900-01-01 1900-01-01 01:01:01
 8 4.0 1053686.6 6 g   m x t      1900-01-01 1900-01-01 01:01:01
 9 4.5  971024.2 7 h   n y s      1900-01-01 1900-01-01 01:01:01
10 5.0  631193.4 8 i   o z r      1900-01-01 1900-01-01 01:01:01
   5.5  879431.1 9 hjh     q      1900-01-01 1900-01-01 01:01:01
> 
> ## Without colnames, with rownames
> write.fwf(testData, colnames=FALSE, rownames=TRUE)
1   1               1     f q        1900-01-01 1900-01-01 01:01:01
2   2 1.0  733070.3 2 a   g r longer            1900-01-01 01:01:01
3   3 1.5 1214213.8 3 b   h s y      1900-01-01 1900-01-01 01:01:01
4   4 2.0  553823.8 4 c   i t x      1900-01-01 1900-01-01 01:01:01
5   5 2.5 1085022.9   d   j u w      1900-01-01                    
6   6 3.0  571063.9 4 e   k v v      1900-01-01 1900-01-01 01:01:01
7   7 3.5  606718.4 5 f   l w u      1900-01-01 1900-01-01 01:01:01
8   8 4.0 1053686.6 6 g   m x t      1900-01-01 1900-01-01 01:01:01
9   9 4.5  971024.2 7 h   n y s      1900-01-01 1900-01-01 01:01:01
10 10 5.0  631193.4 8 i   o z r      1900-01-01 1900-01-01 01:01:01
11    5.5  879431.1 9 hjh     q      1900-01-01 1900-01-01 01:01:01
> 
> ## With rownames and colnames and rowCol
> write.fwf(testData, rownames=TRUE, rowCol="HI!")
HI! num1 num2 num3 int1 fac1 fac2 cha1 cha2 Date POSIXt
1   1               1     f q        1900-01-01 1900-01-01 01:01:01
2   2 1.0  733070.3 2 a   g r longer            1900-01-01 01:01:01
3   3 1.5 1214213.8 3 b   h s y      1900-01-01 1900-01-01 01:01:01
4   4 2.0  553823.8 4 c   i t x      1900-01-01 1900-01-01 01:01:01
5   5 2.5 1085022.9   d   j u w      1900-01-01                    
6   6 3.0  571063.9 4 e   k v v      1900-01-01 1900-01-01 01:01:01
7   7 3.5  606718.4 5 f   l w u      1900-01-01 1900-01-01 01:01:01
8   8 4.0 1053686.6 6 g   m x t      1900-01-01 1900-01-01 01:01:01
9   9 4.5  971024.2 7 h   n y s      1900-01-01 1900-01-01 01:01:01
10 10 5.0  631193.4 8 i   o z r      1900-01-01 1900-01-01 01:01:01
11    5.5  879431.1 9 hjh     q      1900-01-01 1900-01-01 01:01:01
> 
> proc.time()
   user  system elapsed 
  0.104   0.020   0.119