0

Some of the output files I use are non-standard, space delimited formats with non-standard extensions (.out) that only have meaning to a particular program I use. For example, I might get an output file for water budgets that looks like: waterbalance.out

I can open these files in Excel by opening the file as a space delimited file and starting import on row 3.

How can I read such file types into R so that I can convert to .csv?

enter image description here

derelict
  • 3,657
  • 3
  • 24
  • 29
  • 1
    You can specify [`sep`](https://stat.ethz.ch/R-manual/R-devel/library/utils/html/read.table.html) parameter to both `read.csv` and `fread` (for efficient reader). – m0nhawk May 04 '15 at 18:20
  • 1
    I think R can handle non-standard extensions without a problem. The space delimited situation is more dire. It won't have the smarts to deal with ambiguous situations that you can easily bring to bear as you've shown. – duffymo May 04 '15 at 18:20
  • 1
    You could try sth like `read.table(yourfilename, sep = "\t", header=TRUE, comment.char = "0")` (skip lines beginning with 0, use tab as delimitter) or `read.table(yourfilename, sep = " ", header=TRUE, sip = 2)` (skip first two lines, use space as delimitter). – lukeA May 04 '15 at 18:23

1 Answers1

2

Say you have a space separated file:

name grade percent
john 1 0.3
brian 2 0.25
joshua 5 1.1

You can specify a delimeter for read.csv:

> read.csv("test.ssv", sep=" ")
    name grade percent
1   john     1    0.30
2  brian     2    0.25
3 joshua     5    1.10

Also, fread from the data.table is able to automatically parse:

> library("data.table")
> fread("test.ssv")
     name grade percent
1:   john     1    0.30
2:  brian     2    0.25
3: joshua     5    1.10
m0nhawk
  • 22,980
  • 9
  • 45
  • 73