0

is there a way to remove and replace texts that appear like this in a txt file

" C

o

m

p l i m 

e n

t a r y

I B

"

The document I am dealing with has multiple instances of it (it appears on each of the over 2,000 pages) and I am looking for a way to get rid of it.

  • 2
    Do you simply want to remove all white space? Or do you need something more complex which keeps white space between words? It would be helpful if you provide an example of the output you want. – user3283722 May 24 '18 at 18:15
  • Put this text example in an object (txt_to_remove), then use gsub(txt_to_remove, "", my_text) to remove all instances of this text in your text file – phiver May 24 '18 at 18:24
  • I actually want to delete the text. But the instances of white space makes it a bit complicated. Checking out the gsub thingy. –  May 24 '18 at 19:24

1 Answers1

0

Your test data:

t <- " C

o

m

p l i m 

e n

t a r y

I B

"

The question has been asked and answered elsewhere, but in short: the gsub function is what you need here... as shown will remove all types of whitespace, including newlines and tabs... if you read about regex and put some other strings in place of [[:space:]] you can vary the effect.

gsub("[[:space:]]", "", t)

"ComplimentaryIB"
J. Win.
  • 6,662
  • 7
  • 34
  • 52