0

I am reading lines in a file, where each line (length of 16000 characters) is delimited items by '\30', which can have 4500 items. I'm looking items that begin with the feature feat "hr:pos:" The mat is the match and I will identify characters after the match before the next '\30' delimiter.

feat <- "hr:pos:"
inst <- ffile[i,]
feats <- strsplit(inst,"\30")
mat <- grep(feat,feats[[1]])

While almost all lines will have the match, there are some that don't. This is actually the beginning of the line that has 22,000 characters (this goes really fast). I get a message

a la.p.oec.2(1a).4\302(1a)\30:af:ev:\30af:ah:abstract_entity\30h:ah:abstraction\30
Error in grep(feat, feats[[1]]) : object 'feat' not found

This is okay. I merely want to recognize this, so I can go to the next line.

oguz ismail
  • 1
  • 16
  • 47
  • 69
KenLit
  • 33
  • 6
  • Did you mean `grep("feat",feats[[1]])`? If you are looking for the string `"feat"` is needs to be in quotes. – MrFlick May 25 '21 at 19:28
  • **feat** has the value "hr:pos:" (an earlier line to this code). – KenLit May 25 '21 at 19:32
  • Then you shouldn't be getting that exact error message. Are you sure it's defined? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. Something that we can copy/paste into R to run ourselves. – MrFlick May 25 '21 at 19:35

1 Answers1

0

There doesn't seem to be a clear indication that there is an answer when the grep fails. Instead, we need to determine that the answer returned 0 results, and if so, I can go on to the next line of the file without bombing.

feat <- "hr:pos:"
inst <- ffile[i,]
feats <- strsplit(inst,"\30")
if(!length(mat <- grep(feat, feats[[1]])))
  next

This does have the benefit establish the value of mat (which I process later in the function).

KenLit
  • 33
  • 6