reading in a text file with a SUB (1a) (Control-Z) character in R on Windows reading in a text file with a SUB (1a) (Control-Z) character in R on Windows windows windows

reading in a text file with a SUB (1a) (Control-Z) character in R on Windows


I think I've figured out a solution - because there appears to be a problem reading a Control-Z in the middle of a file on Windows, we need to read the file in binary / raw mode.

fnam <- 'h3.txt'tmp.bin <- readBin(fnam, raw(), size=1, n=max(2*file.info(dfnam)$size, 100))=1tmp.char <- rawToChar(tmp.bin)txt <- unlist(strsplit(tmp.char, '\r\n', fixed=TRUE))txt[1] "1,34,44.4,\" HIJK\032A \",99"

UpdateThe following better answer was posted by Duncan Murdoch to R-Devel refer. Converting it into a function I get:

sReadLines <- function(fnam) {    f <- file(fnam, "rb")    res <- readLines(f)    close(f)    res}


I also ran into this problem when I used read.csv with a csv file that contained the SUB or CTRL-Z in the middle of the file.

Solved it with the readr package (if your file is comma separated)

library(readr)read_csv("h3.txt")

If you have a ; as a separator, then use:

library(readr)read_csv2("h3.txt")