-1

I have a big list of the format Variable = Value. 99% of the list is in this format. However, very few entries need to have a = within the value part. So they look like Variable = Value = something

So my read.table function throws an error:

Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  line 2 did not have 2 elements

Is there a way to avoid this without having to change the original file? This is my read table command:

VarNamesDescription<-read.table(paste(FilePathVariableDescription), sep="=", skip=0, header=FALSE,stringsAsFactors=FALSE)

EDIT: Cell with one =

AABB NA=HOLDING NV

Cell with two =

AA=ETX Sml = PrM013)
MichiZH
  • 5,587
  • 12
  • 41
  • 81

1 Answers1

2

If you know that you'll have a maximum of 3 ='s in a line, you can force read.table to allocate an extra column with colClasses and fill.

txt <- "a=2
b=3
c=4
d=5=6
e=7"
read.table(text=txt, sep="=", header=FALSE,
    colClasses=c("character","character","character"), # create a 3rd column
    fill=TRUE # don't fail because data for last column doesn't exist 
    )

  V1 V2 V3
1  a  2   
2  b  3   
3  c  4   
4  d  5  6
5  e  7   
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
  • VarNamesDescription<-read.table(FilePathVariableDescription, sep="=",skip=0,colClasses=c("character", "character", "character"),header=FALSE,fill=TRUE,stringsAsFactors=FALSE) Warning message: In read.table(FilePathVariableDescription, sep = "=", skip = 0, : cols = 2 != length(data) = 3 – MichiZH Jul 12 '13 at 12:32
  • Got it to work with this: VarNamesDescription<-read.table(FilePathVariableDescription, sep="=",skip=0,col.names=c("a","b","c"),header=FALSE,fill=TRUE,stringsAsFactors=FALSE). Thx alot! – MichiZH Jul 12 '13 at 12:37