-2

I have a vector of strings x with length 4 in my R code. I concatenated the first and second element, then removed the second element from the vector. How can I remove an element from the vector entirely so that it stays of length 4? Hope that makes sense!! Not sure I'm explaining well so included to the code below.

inning <- "Valente T. flied out to lf (1-0 B). Galazin grounded out to 2B (1-2 FBS). Shaw singled through the left side (0-0). Boselli Iii flied out to cf (0-2 KF)."

inn <- unlist(strsplit(inning, "[.]"))

for (i in 1:length(inn)){
ab_length <- sapply(strsplit(inn[i], " "), length)
if(ab_length<3){
    inn[i] <- paste(inn[i], inn[i+1])
    inn <- inn[-(i+1)]
    inn <- remove.na(inn)
} # end if
} # end for

num <- rep(0, length(inn))

It works for the above example, but let's say I change Galazin to Galazin B. Then it leaves the vector at 5 elements and doesn't concatenate Galazin B. with the remainder of the sentence. Thoughts?

  • 1
    Welcome to SO! This community has a few [rules](https://stackoverflow.com/help/on-topic) and [norms](https://stackoverflow.com/help/how-to-ask), and following them will help you get a good answer to your question. In particular, it's best to provide an [MCVE](https://stackoverflow.com/help/mcve) (a minimum, complete, and verifiable example). Check out [this page](https://stackoverflow.com/a/5963610/4573108) for tips regarding R-specific MCVEs. The problem is, i can't reproduce your mistake, the x[-2] should be sufficient to remove values without replacing it with NA. Thanks and good luck! – mischva11 Aug 25 '18 at 20:18
  • Welcome to Stack Overflow! Following on to @mischva11 's comment, it would help your question a lot for you to give us the value of x, preferably by typing `dput(x)` and pasting the result into your question. You then did two things to x (concatenation followed by removing). That is just a few lines of code. Please paste those few lines of code into your question. Then we will know _exactly_ what you did and can understand your problem. – G5W Aug 25 '18 at 20:59
  • I think you're looking for `na.omit(x)`. – Dan Aug 25 '18 at 21:16

1 Answers1

0

Slightly modify your code: it is better to count word, rather than split by " " and count items in split (since begining of sentence will be counted as additional item). I would rather use while since length of inn is changing by loop:

 library(stringr)

 inning <- "Valente T. flied out to lf (1-0 B). Galazin B. grounded out to 2B (1-2 FBS). Shaw singled through the left side (0-0). Boselli Iii flied out to cf (0-2 KF)."
 inn <- unlist(strsplit(inning, "[.]"))

  i<-1
  while (i < length(inn)){
   ab_length <- str_count(inn[i], '\\w+')

   if(ab_length<3){
     inn[i] <- paste(inn[i], inn[i+1])
     inn <- inn[-(i+1)]
   }# end if
   i <-i+1
 } # end 
Nar
  • 648
  • 4
  • 8