Reading and Writing Files in Fixed Width Format

Description

Reads and writes files in fixed width format. The functions are written for being more performant than utils::read.fwf.

Usage

1
2
3
write.fwf2(dat, format.full, format.round, file)

read.fwf2( file , format.full , variables = NULL)

Arguments

dat

Data frame (or numeric matrix)

format.full

Vector with fixed width variable lengths

format.round

Vector with digits after decimals

file

File name

variables

Optional vector with variable names

Author(s)

Alexander Robitzsch

See Also

utils::read.fwf

Examples

 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
## Not run: 	
#############################################################################
# EXAMPLE 1: Write and read a file in fixed width format
#############################################################################

# set working directory
path <- "P:/ARb/temp"
setwd(path)

# define a data frame
set.seed(9876)
dat <- data.frame( "x" = seq( 1 , 21 , len=5) , "y" = stats::runif( 5 ) ,
            "z" = stats::rnorm( 5 ) )

# save data frame in fixed width format
format.full <- c(6 , 6 , 8 )
format.round <- c( 0 , 2 , 3 )
write.fwf2( dat , format.full=format.full, format.round=format.round ,
                file = "testdata" )

# read the data
dat1 <- read.fwf2( file = "testdata.dat" , format.full = c(6,6,8) , 
               variables = c("x","y","z") )
# check differences between data frames
dat - dat1

## End(Not run)	

Questions? Problems? Suggestions? or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.