Uses awk to convert a fixed-width file to a CSV based on stacks of whitespace.

fwf2csv(infile, toDF = FALSE, ...)

Arguments

infile

The input file. Can also be "clipboard" to read directly from the clipboard.

toDF

Logical. Should the file be read in while we are at it? Defaults to FALSE.

...

Other arguments to be passed to read.table.

Value

A vector or a data.frame, depending on the value in toDF.

Note

Only tested on Linux.

References

http://stackoverflow.com/q/30868600/1270695

Author

Ananda Mahto and Ed Morton.

Examples

myfile <- tempfile(fileext = ".txt") Lines <- c("aaa b b ccc 345", "ddd fgt f u 3456", "e r der der 5 674") cat(Lines, sep = "\n")
#> aaa b b ccc 345 #> ddd fgt f u 3456 #> e r der der 5 674
cat(Lines, sep = "\n", file = myfile) fwf2csv(myfile)
#> [1] "aaa,b b,ccc,345" "ddd,fgt,f u,3456" "e r,der,der,5 674"
fwf2csv(myfile, TRUE, header = FALSE)
#> V1 V2 V3 V4 #> 1 aaa b b ccc 345 #> 2 ddd fgt f u 3456 #> 3 e r der der 5 674