Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

write.fwf2

Reading and Writing Files in Fixed Width Format


Description

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

Usage

write.fwf2(dat, format.full, format.round, file)

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

Arguments

dat

Data frame (or matrix). Variables can be numeric or strings. However, string length of string variables are not allowed to be larger than what is specified in format.full.

format.full

Vector with fixed width variable lengths

format.round

Vector with digits after decimals

file

File name

variables

Optional vector with variable names

See Also

Examples

## 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 <- miceadds::read.fwf2( file="testdata.dat", format.full=c(6,6,8),
               variables=c("x","y","z") )
# check differences between data frames
dat - dat1

#############################################################################
# EXAMPLE 2: Write datasets containing some string variables in fwf format
#############################################################################

n <- 5
dat <- data.frame( "x"=stats::runif(n, 0, 9 ), "y"=LETTERS[1:n] )
write.fwf2(dat, format.full=c(4,2), format.round=c(2,0),  file="testdata")

## End(Not run)

miceadds

Some Additional Multiple Imputation Functions, Especially for 'mice'

v3.11-6
GPL (>= 2)
Authors
Alexander Robitzsch [aut,cre] (<https://orcid.org/0000-0002-8226-3132>), Simon Grund [aut] (<https://orcid.org/0000-0002-1290-8986>), Thorsten Henke [ctb]
Initial release
2021-01-21 11:48:47

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.