Question

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)
Was it helpful?

Solution

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   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top