Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read data with different separators?

Tags:

r

read.table

I have a file looks like:

a 1,2,3,5
b 4,5,6,7
c 5,6,7,8
...

That the separator between 1st and 2nd is '\t', other separators are comma. How can I read this kind of data set as as dataframe having 5 fields.

like image 693
yliueagle Avatar asked May 09 '14 15:05

yliueagle


1 Answers

I'd probably do this.

read.table(text = gsub(",", "\t", readLines("file.txt")))
  V1 V2 V3 V4 V5
1  a  1  2  3  5
2  b  4  5  6  7
3  c  5  6  7  8

Unpacking that just a bit:

  • readLines() reads the file into R as a character vector with one element for each line.
  • gsub(",", "\t", ...) replaces every comma with a tab, so that now we've got lines with just one kind of separating character.
  • The text = argument to read.table() lets it know you are passing it a character vector to be read directly (rather than the name of a file containing your text data).
like image 67
Josh O'Brien Avatar answered Oct 23 '22 16:10

Josh O'Brien