3

I need to concatenate the previous and the latter words of a condition meeting word. Specifically, those who match the condition of having a comma.

vector <- c("Paulsen", "Kehr,", "Diego",    "Schalper", "Sepúlveda,", "Diego")

#I know how to get which elements meet my condition:

grepl(",", vector)
#[1] FALSE  TRUE FALSE FALSE  TRUE FALSE 

Desired output:

print(vector_ok)
#[1] "Paulsen Kehr, Diego", "Schalper Sepúlveda, Diego"

Thanks in advance!

M--
  • 25,431
  • 8
  • 61
  • 93
David Jorquera
  • 2,046
  • 12
  • 35
  • `strsplit(paste(vector), "[^,] ")` gives the desired output. It splits the vector after the second space where it is not preceded by a comma. – M-- Oct 25 '19 at 02:35
  • That just makes a list with each word as a separated vector... – David Jorquera Oct 25 '19 at 02:42
  • 1
    Look here https://stackoverflow.com/q/58560859/6461462 – M-- Oct 25 '19 at 15:26
  • Excelent, thank you very much for give this a try, the answer given there seems to work for every case of my list :D – David Jorquera Oct 25 '19 at 15:36
  • 1
    sure. I think the `regex` solution is more efficient as well. p.s. And I was very close, this would've worked: ```[^ ]+ [^ ]+, [^ ]+\\K( )``` – M-- Oct 25 '19 at 15:37

1 Answers1

1

You can use grep() to get the positions of the strings with a comma, expand these to a sequence +/- 1, and use this to index and then collapse the original vector.

idx <- grep(",", vector)
seqs <- Map(`:`, idx-1, idx+1)
sapply(seqs, function(x) paste(vector[x], collapse = " "))

[1] "Paulsen Kehr, Diego"       "Schalper Sepúlveda, Diego"
Ritchie Sacramento
  • 29,890
  • 4
  • 48
  • 56