File: tests.write.fwf.R

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 (57 lines) | stat: -rw-r--r-- 1,759 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
library(gdata)

## 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)

## NA should be -
write.fwf(x=testData, na="-")
## NA should be -NA-
write.fwf(x=testData, na="-NA-")

## Some other separator than space
write.fwf(testData[, 1:4], sep="-mySep-")

## Justify right for character columns
write.fwf(testData, justify="right")

## Justify right for character columns, track na values
write.fwf(testData, justify="right", na="!")

## With quotes
write.fwf(testData, quote=TRUE)

## With rownames
write.fwf(testData, rownames=TRUE)

## Without colnames
write.fwf(testData, colnames=FALSE)

## Without colnames, with rownames
write.fwf(testData, colnames=FALSE, rownames=TRUE)

## With rownames and colnames and rowCol
write.fwf(testData, rownames=TRUE, rowCol="HI!")