I'm trying to turn a sentence that my participants entered into something that looks like an actual sentence. Unfortunately, the software I am using records their exact response, key by key. This means that when they press "delete" it shows up as "backspace" instead of actually deleting the last item. Additionally, space shows up as "space". *
Here's an example of one of the responses:
sentence<-c("[g", "r", "a", "d", "u", "a", "t", "i", "o", "n", "space",
"f", "r", "o", "m", "space", "g", "o", "backspace", "backspace",
"c", "o", "l", "l", "e", "g", "e", "space]")
I need to create code that, when it sees a "backspace" deletes both the backspace command and the item before it.
Here is what I've tried:
letters <- NULL
for (j in 1:length(sentence)){
if (sentence[j] != "backspace"){
letters[j] = sentence[j]
}
if (sentence[j] == "backspace"){
letters[j] = letters[-j]
letters[j-1] = NA}``
This does not work, as it is only providing one output at a time, instead of recursively editing the entire vector. Any insight would be appreciated!
EDIT: The tricky part here is the case of double backspaces. How can I tell it to function essentially like a keyboard, and delete both the "g" and the "o" in the example? But have it be flexible enough to know that when it sees a single backspace, it should only delete the item before it (not two)?