Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Concatenate previous and latter words to a word that match a condition in R

$
0
0

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!


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>