2

I've a dataset in tab delimited text file. The data have been exported from an old-school relational database software 4D. Most of the lines seems to be well formated but some lines include an ASCII carriage return character (^M in Emacs or Ascii code 13). I would like to read the data in R using a function such as read.table() and to find a way to ignore those ascii carriage return symbols. Does anyone have a solution ?

Tim Penner
  • 3,551
  • 21
  • 36
PAC
  • 5,178
  • 8
  • 38
  • 62
  • Another way to ask the same question would be : how to delete DOS returns ^M in R ? – PAC Jul 26 '13 at 10:38
  • You can remove `^M` from dataset before import. There are many ways - you can try perl, see this [question](http://stackoverflow.com/questions/650743/in-perl-how-to-do-you-remove-m-from-a-file) – Tomas Greif Jul 26 '13 at 12:16
  • 1
    That's what I've finally done. I've used Vim to delete all the `^M` from the original text file : `:%s/\r//g` (http://rayninfo.co.uk/vimtips.html). It would be great to find a solution from within R. – PAC Jul 26 '13 at 12:33
  • I can't replicate this - `read.table` reads mixed endline files just fine for me – eddi Jul 26 '13 at 15:14
  • @eddi : Interesting. What's your OS ? – PAC Jul 26 '13 at 15:25
  • `> paste(R.Version(), collapse = ",") [1] "x86_64-unknown-linux-gnu,x86_64,linux-gnu,x86_64, linux-gnu,,2,15.0,2012,03,30,58871,R,R version 2.15.0 (2012-03-30),"` – eddi Jul 26 '13 at 15:29
  • Ok, the problem maybe specific to Mac OS – PAC Jul 26 '13 at 15:45
  • Use `readLines()`,replace them using `gsub()` and then do `read.table()`? – Axeman Feb 25 '16 at 22:32

1 Answers1

0

In Vim you can create the ^M character by typing control-v control-m

So you could replace every occurence of ^M with:

:%s/<c-v><c-m>//g
Emile Swarts
  • 103
  • 5