2

I'm trying to read a lot of txt files that are hash tag delimited into R.

For example:

Dora#58529#26/04/2012#
Dora#58529#26/04/2012#
Dora#58529#26/04/2012#
Dora#58529#26/04/2012#
Dora#58529#26/04/2012#
Dora#58529#26/04/2012#
Dora#58529#26/04/2012#
Dora#58529#26/04/2012#
Dora#58529#26/04/2012#

When I try the following only the first column is loaded, probably because everything after the first hash tag is interpreted as a comment.

(df <- read.table("https://dl.dropboxusercontent.com/u/64191100/hashtagdel.txt",sep="#"))

Output:

#     V1
# 1 Dora
# 2 Dora
# 3 Dora
# 4 Dora
# 5 Dora
# 6 Dora
# 7 Dora
# 8 Dora
# 9 Dora

I do not want to change the hash tag in every file by another character (I didn't create the files). Does somebody know a work-around?

Jonas Tundo
  • 6,137
  • 2
  • 35
  • 45
  • 3
    Actually, this question might be helpful: http://stackoverflow.com/questions/9789282/read-table-while-using-as-delimiter-does-not-work?rq=1 – Thomas Jun 04 '13 at 12:53

2 Answers2

7

From ?read.table:

comment.char character: a character vector of length one containing a single character or an empty string. Use "" to turn off the interpretation of comments altogether.

So you want something like read.table(*, sep="#", comment.char="")

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
6

Use read.delim instead...

df <- read.delim("https://dl.dropboxusercontent.com/u/64191100/hashtagdel.txt" , header = FALSE , sep="#")
df
#   V1    V2         V3 V4
#1 Dora 58529 26/04/2012 NA
#2 Dora 58529 26/04/2012 NA
#3 Dora 58529 26/04/2012 NA
#4 Dora 58529 26/04/2012 NA
#5 Dora 58529 26/04/2012 NA
#6 Dora 58529 26/04/2012 NA
#7 Dora 58529 26/04/2012 NA
#8 Dora 58529 26/04/2012 NA
#9 Dora 58529 26/04/2012 NA
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184